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]*pow(K, i, S) AA %= S print(AA)