A,B,C=map(int,input().split())
X=[0]*(C+1)

for i in range(1,C+1):
    X[i]=(X[i-1]+pow(i,B,C))%C

Q,R=divmod(A,C)
print((Q*X[-1]+X[A])%C)