因為(A*B)%C = ((A%C) * (B%C))%C
所以 B^P % M = (B^(P/2)(B^(P/2)))%M = (((B^(P/2))%M)((B^(P/2))%M))%M
unsigned long long ans;
unsigned long long solution(int b,int p,int m){
if(p == 1){
return (b%m);
}else if(p == 0){
return 1;
}
if(p%2 == 0){
ans = solution(b,p/2,m);
ans = (ans*ans)%m;
}
else {
ans = solution(b,(p-1)/2,m);
ans = ((b%m)*ans*ans)%m;
}
return ans;
}
int main(int argc, char const* argv[])
{
int b,p,m;
while(~scanf("%d %d %d",&b,&p,&m)){
printf("%llu\n",solution(b,p,m));
}
return 0;
}