#include using namespace std; using ll = long long; const ll mod = 1000000007; const int INF = 1001001001; ll modpow(ll a, ll p, ll mod){ ll res = 1; while(p){ if(p & 1) res = res * a % mod; a = a * a % mod; p >>= 1; } return res; } int main(){ ll a, b; cin >> a >> b; ll c; cin >> c; ll cnt = a / c; ll res = a % c; ll ans = 0; for(int i = 1; i <= c; i++){ if(i <= res) ans += modpow(i, b, c) * (cnt + 1); else ans += modpow(i, b, c) * cnt; ans %= c; } cout << ans << endl; }