Codeforces Round 1060 (Div. 2)
F2
Bombing (Hard Version)
Codeforces
3300
原题
← F1→
翻译中…

This is the hard version of the problem. The difference between the versions is that in this version, $n \le 10^6$. You can hack only if you solved all versions of this problem.

翻译中…

A permutation$^{\text{∗}}$ $b$ is considered a riffle shuffle of a permutation $a$ if $|a| = |b|$ and there exists $k$ where $1 \le k \lt |a|$ such that $a_1,a_2,\ldots,a_k$ and $a_{k + 1},a_{k + 2},\ldots,a_{|a|}$ are both subsequences$^{\text{†}}$ of $b$.

翻译中…

For example, $[\color{red}{1}, \color{blue}{4}, \color{blue}{5}, \color{red}{2}, \color{red}{3}, \color{blue}{6}]$ is a riffle shuffle of $[\color{red}{1}, \color{red}{2}, \color{red}{3}, \color{blue}{4}, \color{blue}{5}, \color{blue}{6}]$ because we can select $k = 3$ and both $[\color{red}{1}, \color{red}{2}, \color{red}{3}]$ and $[\color{blue}{4}, \color{blue}{5}, \color{blue}{6}]$ are subsequences.

翻译中…

You are given a permutation $p$ of length $n$ where some values are replaced with $-1$. Determine the number of ways to replace each $-1$ with an integer such that $p$ becomes a riffle shuffle of $[1,2,\ldots,n]$ (the sorted permutation).

翻译中…

The number of ways could be gargantuan, so output it modulo $998\,244\,353$.

翻译中…

$^{\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{†}}$A sequence $c$ is a subsequence of a sequence $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) element from arbitrary positions.

翻译中…

Input

翻译中…

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.

翻译中…

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

翻译中…

The second line of each test case contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1 \le p_i \le n$ or $p_i = -1$) — the elements of $p$. All elements of $p$ that are not $-1$ are distinct.

翻译中…

The sum of $n$ across all test cases does not exceed $10^6$.

翻译中…

Output

翻译中…

For each testcase, output the number of ways to fill $p$ so that it is a riffle shuffle of $[1,2,\ldots,n]$ modulo $998\,244\,353$.

翻译中…

Note

翻译中…

The possible permutations for the third test case are as follows:

翻译中…
  • $[\color{red}1, \color{blue}3, \color{blue}4, \color{red}2, \color{blue}5]$,
  • $[\color{red}1, \color{blue}4, \color{blue}5, \color{red}2, \color{red}3]$,
  • $[\color{blue}3, \color{red}1, \color{blue}4, \color{red}2, \color{blue}5]$,
  • $[\color{blue}3, \color{blue}4, \color{red}1, \color{red}2, \color{blue}5]$,
  • $[\color{blue}4, \color{red}1, \color{blue}5, \color{red}2, \color{red}3]$,
  • $[\color{blue}4, \color{blue}5, \color{red}1, \color{red}2, \color{red}3]$.
翻译中…

样例

Input #1
7
5
-1 -1 -1 -1 -1
4
1 2 3 4
5
-1 -1 -1 2 -1
6
-1 3 2 1 -1 -1
18
11 -1 2 -1 -1 -1 -1 6 -1 -1 14 8 9 15 -1 -1 -1 -1
6
-1 3 -1 4 -1 5
3
-1 2 1
Output #1
27
1
6
0
32
0
0
InputCopy OutputCopy
题目信息
题号
2154F2
时间限制
2 s
内存限制
512 MB
标签
组合数学
动态规划

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