n, k = map(int, input().split()) a = list(map(int, input().split())) mod = sum(a) t = 1 ans = 0 for ai in reversed(a): ans = (ans + ai * t) % mod t = t * k % mod print(ans)