CF1909F2.Small Permutation Problem (Hard Version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
⠀
In the easy version, the ai are in the range [0,n] ; in the hard version, the ai are in the range [−1,n] and the definition of good permutation is slightly different. You can make hacks only if all versions of the problem are solved.
You are given an integer n and an array a1,a2,…,an of integers in the range [−1,n] .
A permutation p1,p2,…,pn of [1,2,…,n] is good if, for each i , the following condition is true:
- if ai=−1 , the number of values ≤i in [p1,p2,…,pi] is exactly ai .
Count the good permutations of [1,2,…,n] , modulo 998244353 .
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤104 ). The description of the test cases follows.
The first line of each test case contains a single integer n ( 1≤n≤2⋅105 ) — the length of the array a .
The second line of each test case contains n integers a1,a2,…,an ( −1≤ai≤n ), which describe the conditions for a good permutation.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
输出格式
For each test case, output a single line containing the number of good permutations, modulo 998244353 .
输入输出样例
输入#1
10 5 -1 -1 -1 -1 -1 5 1 2 3 4 5 6 0 2 2 2 -1 -1 6 -1 -1 -1 -1 -1 5 6 -1 -1 3 2 -1 -1 15 0 0 -1 -1 -1 2 2 -1 -1 -1 -1 9 11 13 15 6 0 2 2 2 4 6 6 0 1 3 4 5 5 6 1 2 3 2 4 6 15 0 0 1 1 1 2 3 4 5 6 7 9 11 13 15
输出#1
120 1 4 0 0 494403526 4 0 0 532305727
说明/提示
In the first test case, all the permutations of length 5 are good, so there are 120 good permutations.
In the second test case, the only good permutation is [1,2,3,4,5] .
In the third test case, there are 4 good permutations: [2,1,5,6,3,4] , [2,1,5,6,4,3] , [2,1,6,5,3,4] , [2,1,6,5,4,3] . For example, [2,1,5,6,3,4] is good because:
- a1=0 , and there are 0 values ≤1 in [p1]=[2] ;
- a2=2 , and there are 2 values ≤2 in [p1,p2]=[2,1] ;
- a3=2 , and there are 2 values ≤3 in [p1,p2,p3]=[2,1,5] ;
- a4=2 , and there are 2 values ≤4 in [p1,p2,p3,p4]=[2,1,5,6] ;
- a5=−1 , so there are no restrictions on [p1,p2,p3,p4,p5] ;
- a6=−1 , so there are no restrictions on [p1,p2,p3,p4,p5,p6] .