#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int m,k,n;
void mul(int c[2],int a[2],int b[2][2])
{
int temp[2]={0};
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
temp[i]=(temp[i]+(LL)a[j]*b[j][i])%m;
temp[i]=(temp[i]+m)%m;
}
}
memcpy(c,temp,sizeof temp);
}
void mul(int c[2][2],int a[2][2],int b[2][2])
{
int temp[2][2]={0};
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
for(int k=0;k<2;k++)
{
temp[i][j]=(temp[i][j]+(LL)a[i][k]*b[k][j])%m;
temp[i][j]=(temp[i][j]+m)%m;
}
}
}
memcpy(c,temp,sizeof temp);
}
int main()
{
cin>>m>>k>>n;
int t=((LL)(k)*k-2)%m;
int f1[2]={k,t};
int a[2][2]=
{
{0,-1},
{1,k}
};
if(n==0) cout<<2<<endl;
else
{
n--;
while(n)
{
if(n&1) mul(f1,f1,a);
n>>=1;
mul(a,a,a);
}
cout<<f1[0]<<endl;
}
return 0;
}