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