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