#include #include #define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i) using namespace std; using namespace atcoder; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int Q; cin >> Q; while (Q--) { int n, m; cin >> n >> m; using mint = modint; mint::set_mod(m); using mint998 = modint998244353; vector a(n + 1); vector b(n + 1); reps(i, n) { int x; cin >> x; if (x != -1) a[i] = x; else b[i] = 1; } auto ca = a; auto cb = b; reps(i, n) ca[i] += ca[i - 1]; reps(i, n) cb[i] += cb[i - 1]; mint998 ans = ((cb[n] == 0) ? (ca[n] == 0) : mint998(m).pow(cb[n] - 1)) * (n - 1); reps(i, n - 1) { mint lSum = ca[i], rSum = ca[n] - lSum; int lCnt = cb[i], rCnt = cb[n] - lCnt; mint998 lNum = (lCnt == 0) ? (lSum == 0) : mint998(m).pow(lCnt - 1); mint998 rNum = (rCnt == 0) ? (rSum == 0) : mint998(m).pow(rCnt - 1); ans -= lNum * rNum; } cout << ans.val() << endl; } return 0; }