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