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