How to rmove the error in the code and if this solution is not as the question is asking then what to do in another approach?
Hey you need to create function for exponentiation, that would take modulo of the number in every step. Using in built function will cause value overflow which will result in WA.
Create expo function utilizing
a^2m=(((a^m)%mod)((a^m)%mod))%mod when exponent is even
a^(2m+1) = (((a^m)%mod)((a^m)%mod)*a)%mod when exponent odd.