#include using namespace std; int main(){ int A, N, M; cin >> A >> N >> M; function rec = [&](int V, int N, int MOD){ if(N == 0)return 1; if(MOD == 1)return 1000000; vector order, tb(MOD, -1); int val = 1, tim = 0; while(tb[val] == -1){ tb[val] = tim++; order.push_back(val); val *= V % MOD; val %= MOD; } int EXP = rec(V, N - 1, (tim - tb[val])); int idx = (EXP - tb[val]) % (tim - tb[val]) + tb[val]; return order[idx] + 1000 * MOD; }; cout << rec(A, N, M) % M << endl; }