#include using i64 = long long; #include "atcoder/modint" using Fp = atcoder::modint998244353; int main() { int N, M; std::cin >> N >> M; std::vector A(N); for (auto &e : A) { std::cin >> e; } // judge i64 sum = 0; for (int i = N - 1; i >= 0; --i) { sum += A[i] * (i % 2 == (N - 1) % 2 ? -1 : 1); } if (sum % M != 0) { std::cout << -1 << std::endl; return 0; } std::vector r1(N + 1); r1[0] = 1; for (int i = 0; i < N; ++i) { r1[i + 1] = r1[i] * (M - 1); if (i % 2 == 0) { r1[i + 1] -= M - 2; } } auto calc = [&](int l, Fp g, bool x) { if (x) { if (l % 2 == 0) { if (g == 0) { return r1[l]; } else { return r1[l] - 1; } } else { if (g == M - 1) { return r1[l] - 1; } else { return r1[l]; } } } else { if (l % 2 == 0) { if (g == 0) { return r1[l]; } else { return r1[l] - 1; } } else { if (g == 1) { return r1[l] - 1; } else { return r1[l]; } } } }; auto calc2 = [&](int len, int l, int r, bool x) { Fp res = 0; int w = 0; if (l <= r) { w = r - l + 1; } else { w = M - l + r + 1; } if (x) { if (len % 2 == 0) { res = (r1[len] - 1) * w; if (l == 0 or l > r) { res += 1; } } else { res = r1[len] * w; if (l == M - 1 or l > r) { res -= 1; } } } else { if (len % 2 == 0) { res = (r1[len] - 1) * w; if (l == 0 or l > r) { res += 1; } } else { res = r1[len] * w; if ((l <= 1 and r >= 1) or (l > r and r >= 1)) { res -= 1; } } } return res; }; int s = 0; Fp answer = 0; for (int i = 0; i < N; ++i) { bool x = i % 2 == (N - 1) % 2 ? true : false; if (A[i] != 0) { int l = M - s, r = M - s; if (x) { l -= A[i] - 1; } else { r += A[i] - 1; } l = atcoder::internal::safe_mod(l, M); r = atcoder::internal::safe_mod(r, M); answer += calc2(N - i - 1, l, r, not x); /* for (int j = 0; j < M; ++j) { if (l <= r) { if (l <= j and j <= r) { answer += calc(N - i - 1, j, not x); } } else { if (l <= j or j <= r) { answer += calc(N - i - 1, j, not x); } } } */ } if (x) { s += A[i]; } else { s -= A[i]; } s = atcoder::internal::safe_mod(s, M); } answer += 1; std::cout << answer.val() << std::endl; }