Calculate A^N!%B need help

for _ in range(int(input())):
x,y,mod=map(int,input().split())
ans=x
def exponentiation(x,y,mod):
result=1

	while(y):
	   
		if y%2==1:
			result=(result*x)%mod
		if y==1:
		    break
		x=(x*x)%mod
		y=y>>1
	return result
for i in range(2,y+1):
	ans=(ans*exponentiation(x,i,mod))%mod
print(ans%mod)

I am getting tle in test 3 and test 4 and WA in test 1 and test 2. Please help.

@govilayush refer this for its calculation

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.