MOD = 998244353 def solve(): n = int(input()) A = list(map(int, input().split()))[::-1] rem = [False] * n cc = 0 for i in range(n): if A[i] == -1: continue A[i] -= 1 rem[A[i]] = True cc += 1 dp1 = [0] * n dp2 = [0] * n if A[0] == -1: for i in range(n): if not rem[i]: dp1[i] = 1 else: dp1[A[0]] = 1 cc -= 1 rem[A[0]] = False for i, a in enumerate(A[1:], 1): ndp1 = [0] * n ndp2 = [0] * n if a != -1: tot = 0 for j in range(a + 1, n): tot += dp1[j] + dp2[j] ndp1[a] = tot % MOD for j in range(a): ndp2[j] = dp1[j] rem[a] = False cc -= 1 else: c2 = cc for j in range(n): if rem[j]: c2 -= 1 continue x = n - 1 - j - (i - 1) - c2 if x < 0: continue ndp2[j] = dp1[j] * x % MOD tot = 0 for j in range(n - 1, -1, -1): if rem[j]: continue ndp1[j] = tot tot = (tot + dp1[j] + dp2[j]) % MOD dp1 = ndp1 dp2 = ndp2 print((dp1[0] + dp2[0]) % MOD) for _ in range(int(input())): solve()