n,k = map(int,input().split()) a = list(map(int,input().split())) s = sum(a) ans = 0 for i in range(n): ans += a[n-1-i]*(k**i) print(ans%s)