#include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } // [ DUAL SEGMENT TREE ] BY SHOBON // REFERENCE --- ATCODER LIBRARY // VERSION 1.0 (2022/08/28) int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } template struct dual_segtree{ public: dual_segtree() : dual_segtree(0) {} explicit dual_segtree(int n) : dual_segtree(std::vector(n, id())) {} explicit dual_segtree(const std::vector& v) : _n(int(v.size())) { log = ceil_pow2(_n); size = 1 << log; lz = std::vector(2 * size, id()); for (int i = 0; i < _n; i++) lz[size + i] = v[i]; } void apply(int l, int r, F f){ assert (0 <= l && l <= r && r <= _n); if (l == r) return; l += size; r += size; for (int i = log; i >= 1; i--){ if (((l >> i) << i) != l) push(l >> i); if (((r >> i) << i) != r) push((r - 1) >> i); } while (l < r){ if (l & 1) all_apply(l++, f); if (r & 1) all_apply(--r, f); l >>= 1; r >>= 1; } } F get(int p) { assert(0 <= p && p < _n); p += size; for (int i = log; i >= 1; i--) push(p >> i); return lz[p]; } private: int _n, size, log; std::vector lz; void all_apply(int k, F f){ lz[k] = composition(f, lz[k]); } void push(int k){ all_apply(2 * k, lz[k]); all_apply(2 * k + 1, lz[k]); lz[k] = id(); } }; // ---- END : DUAL SEGMENT TREE ---- struct F{ mint b; mint c; }; F composition(F g, F f){ return {f.b * g.b, f.c * g.b + g.c}; } F id(){ return {1, 0}; } // defcomp template vector compress(vector &X) { vector vals = X; sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); return vals; } // ----- // importbisect template int bisect_left(vector &X, T v){ return lower_bound(X.begin(), X.end(), v) - X.begin(); } template int bisect_right(vector &X, T v){ return upper_bound(X.begin(), X.end(), v) - X.begin(); } // ----- void solve(){ int n, m; cin >> n >> m; vector x(n); vector battle(n + 1); ll tmp = 0; rep(i,0,n){ cin >> x[i]; if (x[i] >= 0){ tmp += x[i]; tmp %= m; } battle[i+1] = tmp; } int cnt = 0; rep(i,0,n){ if (x[i] == -1) cnt++; } if (cnt == 0){ ll tmp = 0; ll ans = 0; rep(i,0,n){ tmp += x[i]; tmp %= m; if (tmp != 0){ ans++; } } if (tmp != 0){ ans = 0; } cout << ans << '\n'; return; } mint seg1 = 0; mint seg2 = 0; mint new11 = 0; mint new12 = 0; map new01; map new02; new01[0] = 1; vector dp1(n + 1); vector dp2(n + 1); dp1[0] = 1; dp2[0] = 0; rep(i,0,n){ if (x[i] == -1){ seg1 *= m - 1; seg1 += new11 * (m - 1); seg2 *= m - 1; seg2 += new12 * (m - 1); new11 = 0; new12 = 0; for (auto [f, j]: new01){ new11 += j; } for (auto [f, j]: new02){ new12 += j; } new01.clear(); new02.clear(); } dp1[i+1] = new01[battle[i+1]] + new11 + seg1; dp2[i+1] = new02[battle[i+1]] + new12 + seg2; dp2[i+1] += dp1[i+1]; new01[battle[i+1]] += dp1[i+1]; new02[battle[i+1]] += dp2[i+1]; } //cout << dp1[n].val() << ' ' << mint(m).pow(cnt-1).val() << '\n'; mint ans = mint(m).pow(cnt - 1) * n - dp2[n]; cout << ans.val() << '\n'; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t--) solve(); }