/*
注意P有可能不是质数,不要用欧拉函数那一套,正解可以倍增,就是等比数列和的性质,注意n是否为奇数
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <memory.h>
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
using namespace std;
int n,m,k;
const int MAXN = ;
long long qpow(long long a,long long b,long long MOD) {
if (b == ) return ;
if (b == ) return a % MOD;
long long res = qpow(a, b / ,MOD);
res = (res * res) % MOD;
res = (res * qpow(a,b % ,MOD)) % MOD;
return res;
}
long long query(int x,int t,int MOD) {
if (x == ) return ;
long long res = query(x / , t, MOD);
res = (res + qpow(t, x / , MOD) * res) % MOD;
if (x % ) res = (res + qpow(t,x - ,MOD)) % MOD;
return res;
}
int main() {
freopen("sunshine.in","r",stdin);
freopen("sunshine.out","w",stdout);
scanf("%d%d%d",&n,&m,&k);
printf(LL "\n",query(n,m,k));
return ;
}