N, K = map(int, input().split()) assert(1 <= N and N <= 100000) assert(2 <= K and K <= 10000) A = list(map(int, input().split())) A.reverse() mod = sum(A) digit = 1 ans = 0 for x in A: ans += (digit * x) % mod; digit = (digit * K) % mod; print(ans)