n, k = map(int, input().split()) A = list(map(int, input().split())) A = A[::-1] ans = 0 mod = sum(A) for i in range(n): ans += A[i] * pow(k, i, mod) print(ans % mod)