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