Codeforces Round 1112 (Div. 2)
D
排列切分
Permutation Cuts
Codeforces
未评级
原题
← CE →

排列切分AI (CI翻译)

You are given an integer $n$ and an array $a$ of length $n-1$.

你需要给定一个整数 $n$ 和一个长度为 $n-1$ 的数组 $a$。AI (CI翻译)

For a permutation$^{\text{∗}}$ $p$ of length $n$, define

对于一个长度为 $n$ 的排列$^{\text{∗}}$ $p$,定义AI (CI翻译)

$$ v_i=\min\left(\max_{1\le j\le i}p_j, \max_{i+1\le j\le n}p_j\right), $$

$$ v_i=\min\left(\max_{1\le j\le i}p_j, \max_{i+1\le j\le n}p_j\right), $$AI (CI翻译)

where $1\le i\le n-1$.

其中 $1\le i\le n-1$。AI (CI翻译)

In other words, cut the permutation between positions $i$ and $i+1$, take the maximum element on each side, and let $v_i$ be the smaller of these two values.

换句话说,在排列的第 $i$ 个位置和第 $i+1$ 个位置之间进行切分,分别取两侧的最大元素,并令 $v_i$ 为这两个值中较小的一个。AI (CI翻译)

Your task is to count the number of permutations $p$ of length $n$ such that $v_i=a_i$ for each $1\le i\le n-1$.

你的任务是计算长度为 $n$ 的排列 $p$ 的数量,使得对于每个 $1\le i\le n-1$,都有 $v_i=a_i$。AI (CI翻译)

Output the answer modulo $998\,244\,353$.

请将答案对 $998\,244\,353$ 取模后输出。AI (CI翻译)

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

$^{\text{∗}}$长度为 $n$ 的排列是一个由 $1$ 到 $n$ 的 $n$ 个互不相同的整数以任意顺序组成的数组。例如,$[2,3,1,5,4]$ 是一个排列,但 $[1,2,2]$ 不是排列(数组中 $2$ 出现了两次),$[1,3,4]$ 也不是排列($n=3$,但数组中出现了 $4$)。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多组测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来描述各个测试用例。AI (CI翻译)

The first line of each test case contains one integer $n$ ($2\le n\le 10^6$) — the length of $p$.

每个测试用例的第一行包含一个整数 $n$($2\le n\le 10^6$)——$p$ 的长度。AI (CI翻译)

The second line contains $n-1$ integers $a_1,a_2,\ldots,a_{n-1}$ ($1\le a_i\le n$) — the elements of $a$.

第二行包含 $n-1$ 个整数 $a_1,a_2,\ldots,a_{n-1}$($1\le a_i\le n$)——数组 $a$ 的元素。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.

保证所有测试用例的 $n$ 之和不超过 $10^6$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output one integer — the number of suitable permutations, modulo $998\,244\,353$.

对于每个测试用例,输出一个整数——满足条件的排列数量,答案对 $998\,244\,353$ 取模。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, both permutations $[1,2]$ and $[2,1]$ satisfy $v_1=1$.

在第一个测试用例中,排列 $[1,2]$ 和 $[2,1]$ 都满足 $v_1=1$。AI (CI翻译)

In the second test case, the only suitable permutations are $[2,1,3]$ and $[3,1,2]$.

在第二个测试用例中,唯一满足条件的排列是 $[2,1,3]$ 和 $[3,1,2]$。AI (CI翻译)

In the third test case, no suitable permutation exists.

在第三个测试用例中,不存在满足条件的排列。AI (CI翻译)

样例

Input #1
11
2
1
3
2 2
3
1 1
4
2 3 2
5
3 3 4 2
2
2
3
1 2
4
3 3 3
5
4 4 4 4
4
2 1 2
6
3 3 5 5 5
Output #1
2
2
0
0
2
0
2
4
12
0
8
InputCopy OutputCopy
题目信息
题号
2250D
时间限制
4 s
内存限制
256 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。