#include using namespace std; using ll=long long; ll mod_pow(ll a,ll n,ll mod){ ll ret=1%mod; a=a%mod; while(n){ if(n&1)ret=ret*a%mod; a=a*a%mod; n>>=1; } return ret; } signed main(){ cin.tie(0)->sync_with_stdio(0); ll A,B,C;cin>>A>>B>>C; ll ans=0; for(ll i=1;i<=min(A,C-1);++i){ // 1<=i+kC<=A // 1-i<=kC<=A-i // (1-i)/C<=k<=(A-i)/C ll cnt=(A-i)/C+1; cnt%=C; ans+=mod_pow(i,B,C)*cnt%C; if(C<=ans)ans-=C; } cout<