A, B, C = map(int, input().split()) num = [0]*C a, b = A//C, A%C for i in range(C): if i == 0 or i>b: num[i] = a else: num[i] = a+1 res = 0 for i in range(C): res += num[i]*pow(i, B, C) res %= C print(res)