Domain: Algorithms & Data Structures

You are given an integer sequence `a = [a1, a2, …, an]`, where each value represents the height of a structure placed in a line.

### Visibility rule (record highs)
Define two collections derived from a sequence `x`:

- **Left-visible heights** `L(x)`: scan `x` from left to right and write down a height whenever it is **strictly greater** than every height seen earlier in the scan.
- **Right-visible heights** `R(x)`: scan `x` from right to left and write down a height whenever it is **strictly greater** than every height seen earlier in that right-to-left scan (equivalently: strictly greater than all elements to its right).

These collections are treated as **sets of height values** (duplicates are irrelevant).

#### Example
For `x = [2, 4, 4, 1, 5, 3]`:
- `L(x) = {2, 4, 5}`
- `R(x) = {3, 5}`

### What you must count
A **subsequence** `a'` is obtained by choosing a subset of indices `1 ≤ i1 < i2 < … < ik ≤ n` and taking:
`a' = [ai1, ai2, …, aik]`.

Count how many subsequences `a'` satisfy **both** of the following:
- `L(a') = L(a)`
- `R(a') = R(a)`

Two subsequences are considered different if they are formed from different index sets, even if their values are identical.

Because the answer can be large, output it modulo:

`998244353`.

---

## Input
The first line contains an integer `t` — the number of test cases.

For each test case:
- The first line contains an integer `n` (`1 ≤ n ≤ 5000`)
- The second line contains `n` integers `a1, a2, …, an` (`1 ≤ ai ≤ 10^9`)

Additional constraint:
- The sum of `n` over all test cases does not exceed `5000`.

---

## Output
For each test case, print a single integer: the number of subsequences `a'` such that  
`L(a') = L(a)` and `R(a') = R(a)`, modulo `998244353`.


Grading rubric:
criterion: Does the answer define L(a) as the record-high values when scanning left to right using a strict 'greater than all previous' rule?
weight: 1
criterion: Does the answer state that when processing a non-maximum element x with x <= the last visible value of the current prefix, the DP value for that prefix is multiplied by 2 (take/skip)?
weight: 1
criterion: Does the answer specify dpL[i] for positions with a[i] = m as the number of subsequences that match L(a) excluding its last element (expressed as dpL[i] = c[k-1] or equivalent)?
weight: 1
criterion: Does the answer give the final sum formula over i <= j with a[i] = a[j] = m as dpL[i] * dpR[j] * 2^(j-i-1) modulo 998244353?
weight: 1
criterion: Does the answer state that the final result is computed modulo 998244353?
weight: 1
criterion: Does the answer state that when processing a non-maximum element x that equals the next required visible value L[p], the transition adds c[p] into c[p+1]?
weight: 1
criterion: Does the answer state that the computed right-DP results are converted back to the original indexing?
weight: 1
criterion: Does the answer define m as max(a) (the maximum element of the sequence)?
weight: 1
criterion: Does the answer state the DP initialization c[0] = 1 (with other c values initially 0)?
weight: 1
criterion: Does the answer define R(a) as the record-high values when scanning right to left using a strict 'greater than all after' rule?
weight: 1

Answer:
### Overview
We need to count subsequences `a'` whose **left-visible set** and **right-visible set** match those of the original sequence `a`.

Let:
- `L(a)` be the (strictly increasing) sequence of record highs seen from the left.
- `R(a)` be the (strictly increasing, in right-to-left scan order) record highs seen from the right.
- `m = max(a)`. The value `m` must appear in any valid subsequence and is visible from both sides.

The main idea is to **fix the first and last occurrence of `m` inside the subsequence**, then multiply independent counts for:
1) building the required left view up to the first `m`,
2) building the required right view from the last `m`,
3) freely choosing elements strictly between those two maxima.

All arithmetic is done modulo `998244353`.

---

### Step 1: Compute `L(a)` and `R(a)`
Scan:
- From left to right, append `a[i]` to `L` if it is strictly greater than all previous.
- From right to left, append `a[i]` to `R` if it is strictly greater than all to its right.

Let `L` have length `k`. Note that `L[k-1] = m`.

---

### Step 2: Count valid prefixes ending at each maximum (`dpL`)
For each position `i` with `a[i] = m`, define:

- `dpL[i]` = number of subsequences formed using indices `≤ i`
  such that their left-visible sequence equals `L` **without its last element** (i.e., the first `k-1` record highs),
  and additionally **no `m` is chosen before position `i`**.

Then, if we append `a[i]=m` as the first maximum, the left view becomes exactly `L(a)`.

#### How to compute `dpL`
Maintain an array `c[0..k-1]` where:
- `c[p]` = number of subsequences whose left-visible sequence equals the first `p` elements of `L`
  (so `p` ranges from `0` to `k-1`, since we do *not* want to place `m` yet).

Initialization:
- `c[0] = 1`, all other `c[p] = 0`.

Process elements from left to right. For each `x = a[i]`:
- If `x == m`:
  - set `dpL[i] = c[k-1]` (these are exactly the subsequences that already match the first `k-1` visible heights),
  - and **do not** allow selecting `x` into `c` (we skip it in the DP so that future maxima can still be “first maximum” candidates).

- Otherwise, update `c` to reflect the choice “take `x`” or “skip `x`”, under the constraint that the left-visible sequence must remain a prefix of `L`:

For each prefix length `p`:
- Let `last = (p == 0 ? -∞ : L[p-1])`.
- If `x <= last`, then taking or skipping `x` does not change the visible record highs:
  - `c[p] := 2 * c[p]`.
- Else (`x > last`), the only way to take `x` without breaking the required left-visible sequence is if it equals the *next* needed visible height:
  - if `p < k-1` and `x == L[p]`, then we can extend:
    - `c[p+1] += c[p]` (take `x`),
    - and also keep `c[p]` as-is (skip `x`).
  - if `x > last` but `x != L[p]`, we cannot take `x` (only skipping is allowed), so `c[p]` stays unchanged.

This DP runs in `O(n * k)`, and `k ≤ n`, so `O(n^2)`.

---

### Step 3: Count valid suffixes starting at each maximum (`dpR`)
Similarly define for each position `j` with `a[j] = m`:

- `dpR[j]` = number of subsequences using indices `≥ j`
  such that their right-visible sequence equals `R(a)` without its last element,
  and **no `m` is chosen after position `j`**.

Compute `dpR` by running the same DP logic on the reversed sequence (and using the record highs of `R(a)` in the appropriate order), then map results back to original indices.

---

### Step 4: Combine by fixing first/last maximum
Fix positions `i ≤ j` with `a[i] = a[j] = m`, where:
- `i` is the first chosen maximum in the subsequence,
- `j` is the last chosen maximum in the subsequence.

Elements strictly between `i` and `j` can be chosen arbitrarily: they are “sandwiched” between maxima and cannot affect visibility from either side.

Let `x = j - i - 1` be the number of elements between them. Then the number of choices is `2^x`.

Contribution of pair `(i, j)`:
- `dpL[i] * dpR[j] * 2^(j-i-1)`.

So the final answer is:
\[
\text{ans} = \sum_{\substack{i \le j \\ a[i]=a[j]=m}} dpL[i] \cdot dpR[j] \cdot 2^{(j-i-1)} \pmod{998244353}.
\]

Precompute powers of two up to `n` modulo `998244353`.

---

### Complexity
- Computing `L(a)` and `R(a)`: `O(n)`
- Computing `dpL` and `dpR`: `O(n^2)`
- Summing over maximum pairs: `O(n^2)` in the worst case

Total per test file fits the constraints because `∑n ≤ 5000`.

---