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