Domain: Algorithms & Data Structures

You are given multiple test cases. In each test case, you are given an array `p` of length `n` describing a permutation of `{1,2,…,n}` with missing elements: some `p[i] = -1`, and all non-`-1` values in `p` are **distinct**.

A permutation `b` of length `n` is called a **riffle shuffle** of the sorted permutation `[1,2,…,n]` if there exists an integer `k` with `1 ≤ k < n` such that:
- the sequence `1,2,…,k` appears as a subsequence of `b`, and
- the sequence `k+1,k+2,…,n` appears as a subsequence of `b`.

Your task: for each test case, count the number of distinct ways to replace every `-1` in `p` with the missing integers (so that the result is a full permutation of `1..n`) such that the resulting permutation is a riffle shuffle of `[1..n]`.

Output the answer modulo `998244353`.

### Input format
- First line: integer `t` (number of test cases).
For each test case:
- Line 1: integer `n`.
- Line 2: `n` integers `p[1..n]` (each is `-1` or in `[1..n]`), with all non-`-1` distinct.

### Output format
For each test case, output one integer: the number of valid completions modulo `998244353`.


Grading rubric:
criterion: For a known value v at position i with v<=k, the answer states the exact prefix constraint B[i]=v (i.e., exactly v BLUE positions among indices 1..i).
weight: 10
criterion: For a known value v at position i with v>k, the answer states the exact prefix constraint B[i]=i-(v-k).
weight: 10
criterion: The answer explicitly states that if two constraints assign different required values to the same prefix length j, then that split k contributes 0 (is discarded).
weight: 10
criterion: The answer computes the number of valid colorings in a feasible block as exactly the binomial coefficient C(len, need).
weight: 10
criterion: The answer includes an explicit correction for the sorted completion case (when compatible with the partial p) to avoid overcounting across k (e.g., subtracting n-2 modulo 998244353).
weight: 10
criterion: The answer reformulates the problem for a fixed split k by coloring values v<=k as BLUE and values v>k as RED.
weight: 10
criterion: For a known value v at position i with v<=k, the answer states the exact constraint B[i-1]=v-1 (forcing position i itself to be BLUE).
weight: 10
criterion: The answer states the feasibility condition between consecutive constrained prefixes: for a block with len=jb-ja and need=Bb-Ba, it must hold that 0<=need<=len.
weight: 10
criterion: The answer includes an aggregation step that sums count(k) over all k from 1 to n-1 to form the total.
weight: 10
criterion: For a known value v at position i with v>k, the answer states the exact constraint B[i-1]=B[i] (forcing position i itself to be RED).
weight: 10

Answer:
Let MOD = 998244353. Precompute factorials `fact[i]` and inverse factorials `invfact[i]` up to the maximum `n` across all test cases, so `C(n,r) = fact[n]*invfact[r]*invfact[n-r] mod MOD`.

Key uniqueness fact (split-point identification):
- For any riffle shuffle permutation `q` that is **not** fully sorted, the split value `k` is uniquely determined as the smallest integer such that `k` appears after `k+1` in `q`.
- The only riffle shuffle permutation where no such `k` exists is the sorted permutation `[1,2,…,n]`, which is valid for every `k` and therefore must be handled to avoid overcounting.

Fix a split value `k` (1 ≤ k < n). Color values `1..k` as BLUE and `k+1..n` as RED. A completion is valid for this `k` iff the BLUE values appear in increasing order and the RED values appear in increasing order; once the BLUE/RED pattern of positions is fixed, the final permutation is uniquely determined by scanning left-to-right and assigning the next unused BLUE number or RED number.

Prefix-count reformulation (explicit constraints):
Define `B[j]` = number of BLUE positions among the first `j` positions (prefix length `j`), for `j = 0..n`.
Then necessarily:
- `B[0] = 0`
- `B[n] = k`
- For every position `i` with known value `v = p[i] != -1`:

  1) If `v ≤ k` (BLUE value), then:
     - `B[i] = v`  (exactly `v` blues in positions `1..i`)
     - `B[i-1] = v-1` (position `i` itself must be BLUE)

  2) If `v > k` (RED value), then the number of reds in `1..i` must be `v-k`, so:
     - `B[i] = i - (v-k)`  (since blues = i − reds)
     - `B[i-1] = B[i]` (position `i` itself must be RED)

If any prefix length `j` gets two different required values, this `k` is impossible.

Counting for a fixed k using binomials:
Let `req[j]` store the required value of `B[j]` (or “unset”). Initialize:
- `req[0]=0`, `req[n]=k`, and add the above constraints from every known `p[i]`.

Collect all prefix indices `j` where `req[j]` is set, sort them increasingly: `j0 < j1 < ... < jm`.
For each consecutive pair `(ja, Ba=req[ja])` and `(jb, Bb=req[jb])`:
- `len = jb - ja` (number of positions in this block)
- `need = Bb - Ba` (number of BLUE positions in that block)
Feasibility condition (must hold):
- `0 ≤ need ≤ len`
If not, this `k` is impossible.
Otherwise, the number of ways to choose which positions in this block are BLUE is exactly:
- `C(len, need)`
Multiply these contributions over all blocks to get `count(k)`.

Sorted permutation special case:
Check whether the fully sorted permutation `[1..n]` is compatible with the partial input: i.e., for every `i`, if `p[i] != -1` then `p[i] == i`. If compatible, there is exactly one “sorted completion”; call this `sorted_ok = 1`, else `0`.

Final aggregation:
Compute `S = sum_{k=1..n-1} count(k) mod MOD`.
If `sorted_ok = 1`, the sorted completion was counted once for every `k` (i.e., `n-1` times), but must be counted once total, so subtract `(n-2)`:
- `answer = (S - (n-2)) mod MOD`
Else:
- `answer = S`
Output `answer` modulo MOD (normalized to [0, MOD)).

(Implementation note: `C(len, need)` is computed in O(1) using factorials and inverse factorials.)