RUN ERROR IN CODE

can you tell me if my approach is wrong

yeah sure, but only if you post your strategy

i iterated from 1 to n to calculate a^n!.

and took mod with P at every iteration. Power was calculated using O(logn)

that is wrong. first of all for this question i suggest you read about euler theorem on the internet and when you are done understanding it, please comment and i’ll help you out further.

i read the euler theorem. but i don’t understand how i can apply that here

yeah so first of all calculate the euler totient function of the P and then start iterating from 1 to n and keep multiplying the answer by i mod totient value. This is the power then use binary exponentiation to calculate the final answer

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.