Do you know what is the maximum value of n? I am asking you this because you have used pow function to calculate pow(2,i) where i is as high as n (5e5). So its causing wrong answer as you are requesting to calculate pow(2,500000).
Try to use modular exponentiation to calculate powers modulo any number.
2 Likes