#yuki1161 a,b,c=map(int,input().split()) res=0 for i in range(c): res+=pow(i,b,c)*((a-i)//c+1) res%=c print(res)