A,B,C=map(int,input().split()) print(sum(pow(i,B,C)*(A//C+(i<=A%C))for i in range(C))%C)