#include #define REP(i,n) for(int i=0,i##_len=int(n);i 0 ) { if(p&1) res *= x; x = x*x%mod; res %= mod; p >>= 1; } return res; } int main(){ ll a,n,m; cin>>a>>n>>m; vector A(m+1,1); cerr << 0 << " " << 1 << endl; int cyc = 0, offset = 0; for(int i = 0; i < m; i++) { A[i+1] = A[i]==0 ?0:mpow(a, A[i], m); std::cerr << i+1 << " " << A[i+1] <= offset ? (offset + (n-offset)%cyc):n]%m << endl; }