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