A,B,C=map(int,input().split()) s=0 for i in range(1,C+1): s=(s+pow(i,B,C))%C ans=A//C*s%C A%=C for i in range(1,A+1): ans=(ans+pow(i,B,C))%C print(ans)