a, b, c = [int(i) for i in input().split()] ans = 0 for i in range(c): ans += pow(i, b, c)*((a-i)//c+1) ans %= c print(ans)