Math Day(two test case fail)

package competitive;

import java.util.*;

public class MathDay {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Scanner sc = new Scanner(System.in);
	int t = sc.nextInt();
	while (t-- > 0) {
		long a = sc.nextLong();
		long n = sc.nextLong();
		long p = sc.nextLong();
		
			long ans = a%p;
			for (int i = 2; i <= n; i++) {
				ans = power(ans, i, p) ;
				ans%=p;
			}
			System.out.println(ans);

	}

}

public static long power(long ans, long b, long p) {
	if (b == 0) {
		return 1;
	}
	if ((b & 1) != 0) {
		return (ans * (power(ans, b / 2, p)))%p ;
	} else {
		long s =  power(ans, b / 2, p)%p ;
		return (s*s)%p;
	}
}

}

please help me. to solve this problem

Just do this -:

int ans = findpower(a,n);

reverse loop from n-1 to 1:
{
	ans = findpower(ans,i)%p;
	ans %= p;
}

print ans

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.