#P9493. 「SFCOI-3」进行一个列的排

    ID: 9457 远端评测题 1500ms 32MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划 DP2023洛谷原创O2优化区间 DP

「SFCOI-3」进行一个列的排

Background

(In fact, this problem was originally called I must say No, but for some obvious reasons the title was changed /kk.)

You must say Yes.

(The background image comes from Phigros artwork. If there is any infringement, please inform the problem setter.)

Problem Description

Little R has a permutation p1pnp_1 \dots p_n of length nn. In other words, p1pnp_1 \dots p_n contains the numbers from 00 to (n1)(n - 1), and among these nn numbers, each number appears in pp exactly once.

Little R has nn constraints. The ii-th constraint (0in1)(0 \le i \le n - 1) is described by apositive integer LiL_i, meaning that there exists at least one interval [l,r][l, r] of length LiL_i (i.e. rl+1=Lir - l + 1 = L_i) such that mexk=lrpk=i\operatorname{mex}_{k=l}^r p_k = i.

Little R has lost the permutation p1pnp_1 \dots p_n, but fortunately she still remembers these nn constraints. Please help her find how many initial permutations are valid. Output the answer modulo 998244353998244353.

Input Format

This problem has multiple test cases in each test point.

The first line contains an integer TT, the number of test cases. For each test case:

  • The first line contains an integer nn.
  • The second line contains nn integers, representing L0Ln1L_0 \dots L_{n-1} in order.

Output Format

Output TT lines. Each line contains one integer, the answer.

4
4
1 1 3 3
5
2 1 3 3 4
6
1 1 2 5 4 5
10
3 2 3 4 7 6 8 8 8 9
4
12
8
96

Hint

Definitions

  • The mex\operatorname{mex} of a sequence is the smallest non-negative integer that does not appear in it. For example, mex{1,3,4}=0\operatorname{mex}\{1, 3, 4\} = 0, mex{0,1,1,2,5}=3\operatorname{mex}\{0, 1, 1, 2, 5\} = 3, mex{3,1,0,2}=4\operatorname{mex}\{3, 1, 0, 2\} = 4.

Constraints

  • Subtask 0 (10 pts): n10n \le 10.
  • Subtask 1 (30 pts): n18n \le 18.
  • Subtask 2 (15 pts): n300n \le 300.
  • Subtask 3 (45 pts): no special constraints.

For all testdata, 1T101 \le T \le 10, 1n5×1031 \le n \le 5 \times 10^3, 1Lin1 \le L_i \le n.

Translated by ChatGPT 5