import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes") no = lambda :print("no");No = lambda :print("No") ####################################################################### mod = 998244353 for _ in range(ni()): n, m = na() x = na() m2 = pow(m-2, mod-2, mod) ss = 0 for i in range(n): if x[i] != -1: ss -= x[i] ss %= m if ss != 0: dpc = [1, 0, 0] dps = [0, 0, 0] s = 0 for i in range(n): ndps, ndpc = [0, 0, 0], [0, 0, 0] dps, ndps = ndps, dps dpc, ndpc = ndpc, dpc if x[i] != -1: if s == 0: dps[0] += ndps[0] else: dps[0] += ndpc[0] + ndps[0] if (s + ss) % m == 0: dps[1] += ndps[1] else: dps[1] += ndpc[1] + ndps[1] if s == 0 or (s + ss) % m == 0: dps[2] += ndpc[2] + ndps[2] else: dps[2] += ndpc[2] * (m-3) % mod * m2 + ndps[2] dpc[0] = ndpc[0] dpc[1] = ndpc[1] dpc[2] = ndpc[2] else: if s == 0: dps[0] += ndps[0] dps[1] += ndps[0] dps[2] += ndps[0] else: dps[0] += ndpc[0] + ndps[0] dps[1] += ndpc[0] + ndps[0] dps[2] += ndpc[0] + ndps[0] if (s + ss) % m == 0: dps[0] += ndps[1] dps[1] += ndps[1] dps[2] += ndps[1] else: dps[0] += ndpc[1] + ndps[1] dps[1] += ndpc[1] + ndps[1] dps[2] += ndpc[1] + ndps[1] if s == 0 or (s + ss) % m == 0: dps[0] += ndpc[2] * (m-2) + ndps[2] * (m-2) dps[1] += ndps[2] * (m-2) + ndpc[2] * (m-2) dps[2] += ndps[2] * (m-2) + ndpc[2] * (m-2) else: dps[0] += ndpc[2] * (m-3) + ndps[2] * (m-2) dps[1] += ndpc[2] * (m-3) + ndps[2] * (m-2) dps[2] += ndpc[2] * (m-3) + ndps[2] * (m-2) dps[0] %= mod dps[1] %= mod dps[2] %= mod dpc[0] = ndpc[0] + ndpc[1] + ndpc[2] * (m-2) dpc[1] = ndpc[0] + ndpc[1] + ndpc[2] * (m-2) dpc[2] = ndpc[0] + ndpc[1] + ndpc[2] * (m-2) dpc[0] %= mod dpc[1] %= mod dpc[2] %= mod if x[i] != -1: s += x[i] s %= m #print(s, dps, dpc) ans = dps[1] else: m1 = pow(m-1, mod-2, mod) dpc = [1, 0] dps = [0, 0] s = 0 for i in range(n): ndps, ndpc = [0, 0], [0, 0] dps, ndps = ndps, dps dpc, ndpc = ndpc, dpc if x[i] != -1: if s == 0: dps[0] += ndps[0] else: dps[0] += ndpc[0] + ndps[0] if s == 0: dps[1] += ndpc[1] + ndps[1] else: dps[1] += ndpc[1] * (m-2) % mod * m1 + ndps[1] dpc[0] = ndpc[0] dpc[1] = ndpc[1] else: if s == 0: dps[0] += ndps[0] dps[1] += ndps[0] else: dps[0] += ndpc[0] + ndps[0] dps[1] += ndpc[0] + ndps[0] if s == 0: dps[0] += ndpc[1] * (m-1) + ndps[1] * (m-1) dps[1] += ndpc[1] * (m-1) + ndps[1] * (m-1) else: dps[0] += ndpc[1] * (m-2) + ndps[1] * (m-1) dps[1] += ndpc[1] * (m-2) + ndps[1] * (m-1) dps[0] %= mod dps[1] %= mod dpc[0] = ndpc[0] + ndpc[1] * (m-1) dpc[1] = ndpc[0] + ndpc[1] * (m-1) dpc[0] %= mod dpc[1] %= mod if x[i] != -1: s += x[i] s %= m #print(dps, dpc) ans = dps[0] print(ans)