311000: CF1919E. Counting Prefixes

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. Counting Prefixestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

There is a hidden array $a$ of size $n$ consisting of only $1$ and $-1$. Let $p$ be the prefix sums of array $a$. More formally, $p$ is an array of length $n$ defined as $p_i = a_1 + a_2 + \ldots + a_i$. Afterwards, array $p$ is sorted in non-decreasing order. For example, if $a = [1, -1, -1, 1, 1]$, then $p = [1, 0, -1, 0, 1]$ before sorting and $p = [-1, 0, 0, 1, 1]$ after sorting.

You are given the prefix sum array $p$ after sorting, but you do not know what array $a$ is. Your task is to count the number of initial arrays $a$ such that the above process results in the given sorted prefix sum array $p$. As this number can be large, you are only required to find it modulo $998\,244\,353$.

Input

Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 5000$) — the size of the hidden array $a$.

The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ ($|p_i| \le n$) — the $n$ prefix sums of $a$ sorted in non-decreasing order.

It is guaranteed that $p_1 \le p_2 \le \ldots \le p_n$.

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

Output

For each test case, output the answer modulo $998\,244\,353$.

ExampleInput
5
1
0
1
1
3
-1 1 2
5
-1 0 0 1 1
5
-4 -3 -3 -2 -1
Output
0
1
0
3
1
Note

In the first two test cases, the only possible arrays $a$ for $n = 1$ are $a = [1]$ and $a = [-1]$. Their respective sorted prefix sum arrays $p$ are $p = [1]$ and $p = [-1]$. Hence, there is no array $a$ that can result in the sorted prefix sum array $p = [0]$ and there is exactly $1$ array $a$ that can result in the sorted prefix sum array $p = [1]$.

In the third test case, it can be proven that there is no array $a$ that could result in the sorted prefix sum array $p = [-1, 1, 2]$.

In the fourth test case, the $3$ possible arrays $a$ that could result in the sorted prefix sum array $p = [-1, 0, 0, 1, 1]$ are:

  • $a = [1, -1, 1, -1, -1]$. The prefix sum array before sorting is $p = [1, 0, 1, 0, -1]$, which after sorting gives $p = [-1, 0, 0, 1, 1]$.
  • $a = [1, -1, -1, 1, 1]$. The prefix sum array before sorting is $p = [1, 0, -1, 0, 1]$, which after sorting gives $p = [-1, 0, 0, 1, 1]$.
  • $a = [-1, 1, 1, -1, 1]$. The prefix sum array before sorting is $p = [-1, 0, 1, 0, 1]$, which after sorting gives $p = [-1, 0, 0, 1, 1]$.

For the fifth test case, the only possible array $a$ that could result in the sorted prefix sum array $p = [-4, -3, -3, -2, -1]$ is $a = [-1, -1, -1, -1, 1]$.

Output

题目大意:
存在一个大小为n的隐藏数组a,只包含1和-1。设p为a的前缀和数组。更正式地说,p是一个长度为n的数组,定义为p_i = a_1 + a_2 + ... + a_i。之后,数组p按非递减顺序排序。例如,如果a = [1, -1, -1, 1, 1],那么p在排序前为[1, 0, -1, 0, 1],排序后为[-1, 0, 0, 1, 1]。

你得到了排序后的前缀和数组p,但你不知道数组a是什么。你的任务是计算使得上述过程得到给定排序前缀和数组p的初始数组a的数量。由于这个数字可能很大,你只需要找到它模998,244,353的结果。

输入输出数据格式:
每个测试包含多个测试用例。第一行包含一个整数t(1 ≤ t ≤ 1000)——测试用例的数量。接下来是测试用例的描述。

每个测试用例的第一行包含一个整数n(1 ≤ n ≤ 5000)——隐藏数组a的大小。

每个测试用例的第二行包含n个整数p_1, p_2, ..., p_n(|p_i| ≤ n)——a的n个前缀和,按非递减顺序排序。

保证p_1 ≤ p_2 ≤ ... ≤ p_n。

保证所有测试用例的n之和不超过5000。

对于每个测试用例,输出答案模998,244,353的结果。题目大意: 存在一个大小为n的隐藏数组a,只包含1和-1。设p为a的前缀和数组。更正式地说,p是一个长度为n的数组,定义为p_i = a_1 + a_2 + ... + a_i。之后,数组p按非递减顺序排序。例如,如果a = [1, -1, -1, 1, 1],那么p在排序前为[1, 0, -1, 0, 1],排序后为[-1, 0, 0, 1, 1]。 你得到了排序后的前缀和数组p,但你不知道数组a是什么。你的任务是计算使得上述过程得到给定排序前缀和数组p的初始数组a的数量。由于这个数字可能很大,你只需要找到它模998,244,353的结果。 输入输出数据格式: 每个测试包含多个测试用例。第一行包含一个整数t(1 ≤ t ≤ 1000)——测试用例的数量。接下来是测试用例的描述。 每个测试用例的第一行包含一个整数n(1 ≤ n ≤ 5000)——隐藏数组a的大小。 每个测试用例的第二行包含n个整数p_1, p_2, ..., p_n(|p_i| ≤ n)——a的n个前缀和,按非递减顺序排序。 保证p_1 ≤ p_2 ≤ ... ≤ p_n。 保证所有测试用例的n之和不超过5000。 对于每个测试用例,输出答案模998,244,353的结果。

加入题单

上一题 下一题 算法标签: