n,k = map(int,input().split()) A = list(reversed(list(map(int,input().split())))) B = sum(A) C = 0 for i in range(n): C += k ** i * A[i] print(C % B)