#include #include using namespace std; long long modpow(long long a, long long b, int MOD){ long long ans = 1; while (b > 0){ if (b % 2 == 1){ ans *= a; ans %= MOD; } a *= a; a %= MOD; b /= 2; } return ans; } int main(){ long long A, B; int C; cin >> A >> B >> C; A++; vector P(C); for (int i = 0; i < C; i++){ P[i] = modpow(i, B, C); } vector S(C + 1); S[0] = 0; for (int i = 0; i < C; i++){ S[i + 1] = (S[i] + P[i]) % C; } cout << (S[C] * (A / C) + S[A % C]) % C << endl; }