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