Can someone please explain why my code is giving wrong answer for Math day problem
def func(a,n,mod):
res = 1
while n>0:
if n%2==1:
res = (res * a)%mod
a = (a * a)%mod
n >>= 1
return res%mod
for _ in range(int(input())):
a,n,p = map(int,input().split())
ans = a%p
for i in range(2,n+1):
ans = func(ans,i,p)
print(ans)