#include #include #include #include #include #include #include #include #include static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template constexpr T INF = ::numeric_limits::max()/32*15+208; template T pow_ (T x, T n, T M){ uint64_t u = 1, xx = x; while (n > 0){ if (n&1) u = u * xx % M; xx = xx * xx % M; n >>= 1; } return static_cast(u); }; int main() { ll a, b, c; cin >> a >> b >> c; ll ans = 0; for (ll i = 1; i <= c; ++i) { ans += (pow_(i, b, c)*(a/c + (i <= a%c)))%c; if(ans >= c) ans -= c; } cout << ans << "\n"; return 0; }