a,b,c=map(int,input().split()) ans=0 for i in range(1,c+1): x=a//c if i<=a%c: x+=1 ans+=pow(i,b,c)*x ans%=c print(ans)