// Wrongri-La Shower #include typedef long long ll; int phi[2001]; ll A, N, M; int expt(int a, int n){ int res = 1; while(n > 0){ if(n & 1){res *= a;} a *= a; n >>= 1; } return res; } ll mod_pow(ll a, ll n, ll m){ if(m == 1ll){return 0ll;} ll res = 1ll; while(n > 0ll){ if(n & 1ll){res = res * a % m;} a = a * a % m; n >>= 1; } return res; } ll tower(ll a, ll n, ll m){ // printf("%lld, %lld, %lld\n", A, N, M); if(n == 0ll){return 1ll;} if(m == 1ll){return 0ll;} return mod_pow(a, tower(a, n-1, phi[m]), m); } int main(){ phi[1] = 1; for(int i=2;i<=2000;i++){ phi[i] = 1; int x = i; for(int j=2;j*j<=x;j++){ int count = 0; while(x % j == 0){ x /= j; count += 1; } if(count == 0){continue;} phi[i] *= expt(j, count) - expt(j, count-1); } if(x > 1){phi[i] *= x - 1;} } scanf("%lld %lld %lld", &A, &N, &M); printf("%lld\n", tower(A, N, M)); }