MATH DAY problems

how to solve it (A^N!) %P
i can not find out logic
i already try many time

the logic can be build as;-
ans=(((ans^n)^n-1)^n-2)…1;
where ^ is raise to the power.