#include #include #include #include #include #define ll long long using namespace std; ll mpower(ll a,ll b,ll c){ int z; if(b==0){ z=1; z%=c; return z; } if(b==1){ z=a; z%=c; return z; } else{ return (((mpower(a,b/2,c))*(mpower(a,b/2,c))%c)*mpower(a,b%2,c)%c); } } int main() { ll a,b,c; cin>>a>>b>>c; ll ans=0; ll mod=0; for(ll i=0;i<=c;i++){ mod+=mpower(i,b,c); mod%=c; } ans+=mod*(a/c); ans%=c; for(ll i=0;i<=a%c;i++){ ans+=mpower(i,b,c); ans%=c; } cout<