#include #include using namespace std; using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i; using vvi = vector>; template inline void vin(vector& v) { rep(i, v.size()) cin >> v.at(i); } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline void drop(T x) { cout << x << endl; exit(0); } template void vout(vector v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; using mint = modint998244353; using vm = vector; int main() { lint N, M; cin >> N >> M; vi v(N); vin(v); mint a=0, b=0, c=0; lint x=0, y=0, z=0; rep(i, N) { if (i%2) y += v[i]; else z += v[i]; } if (abs(y-z)%M != 0) drop(-1); vm dpo(3), dpe(3); rep(i, N) { z = v[i]; if (i%2 == 0) { y = x+v[i]; a = dpo[0]+dpo[1]+dpo[2]; dpe[0] = a-dpo[1]; dpe[2] = a-dpo[0]; dpe[1] = a*(M-2)-dpo[2]; if ((x <= 0 && 0 < y) || (x <= M && M < y)) dpe[0]++, z--; if ((x <= M-1 && M-1 < y) || (x <= M+M-1 && M+M-1 < y)) dpe[2]++, z--; dpe[1] += z; x += v[i]; } else { y = x-v[i]; a = dpe[0]+dpe[1]+dpe[2]; dpo[0] = a-dpe[2]; dpo[1] = a-dpe[0]; dpo[2] = a*(M-2)-dpe[1]; if ((y < 0 && 0 <= x) || (y < M && M <= x)) dpo[0]++, z--; if ((y < 1 && 1 <= x) || (y < M+1 && M+1 <= x)) dpo[1]++, z--; dpo[2] += z; x -= v[i]; } x = (x+M)%M; // list(dpe[0].val()) // list(dpe[1].val()) // list(dpe[2].val()) // list(dpo[0].val()) // list(dpo[1].val()) // show(dpo[2].val()) } if (M%2 == 1) std::cout << dpe[0].val()+1 << '\n'; if (M%2 == 0) std::cout << dpo[0].val()+1 << '\n'; }