Modular Exponentiation

Why is my code failing some test cases???
Is this approach wrong?

yes this approach is wrong for large values
because a^b may overflow
you have to take mod at every step

Reference Code