Spectral::Cup 2026 Round 3 (Codeforces Round 1110, Div. 1 + Div. 2)
F
熟悉吗?
Familiar?
Codeforces
2800
原题
← EG →

熟悉吗?AI (CI翻译)

Note the unusual memory limit.

请注意不寻常的内存限制。AI (CI翻译)

Consider the following pseudocode that processes a permutation $p$ of length $n$$^{\text{∗}}$:

考虑以下处理长度为 $n$$^{\text{∗}}$ 的排列 $p$ 的伪代码:AI (CI翻译)

function mystery(p):
    n = length of p
    st = an empty stack
    count = an array of length n filled with 0s
    for i from 1 to n:
        while st is not empty and p[i] < top of st:
            pop from st
            count[i] = count[i] + 1
        push p[i] into st
    return count

function mystery(p):
    n = length of p
    st = an empty stack
    count = an array of length n filled with 0s
    for i from 1 to n:
        while st is not empty and p[i] < top of st:
            pop from st
            count[i] = count[i] + 1
        push p[i] into st
    return count
AI (CI翻译)

You are given an array $a$ of length $n$. Your task is to count the number of permutations $p$ of length $n$ such that, if $b$ is the array returned by mystery(p), the condition $a_i = b_i$ holds for all $i$ ($1 \le i \le n$) where $a_i \neq -1$.

给定一个长度为 $n$ 的数组 $a$。你的任务是计算长度为 $n$ 的排列 $p$ 的数量,使得当 $b$ 为 mystery(p) 返回的数组时,对于所有满足 $a_i \neq -1$ 的 $i$($1 \le i \le n$),均有 $a_i = b_i$。AI (CI翻译)

Since the answer can be very large, output it 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 an integer $n$ ($1 \le n \le 500$), representing the length of $a$.

每个测试用例的第一行包含一个整数 $n$($1 \le n \le 500$),表示 $a$ 的长度。AI (CI翻译)

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($-1 \le a_i \le n$), representing the elements in $a$.

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

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

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

Output

输出格式

AI (CI翻译)

For each test case, output an integer representing the number of permutations, modulo $998\,244\,353$.

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

Note

说明

AI (CI翻译)

In the first and second test cases, the only valid permutation is $[1]$.

在第一个和第二个测试用例中,唯一有效的排列是 $[1]$。AI (CI翻译)

In the fourth test case, the valid permutations are $[1,2,4,3]$, $[3,4,2,1]$, and $[1,4,3,2]$.

在第四个测试用例中,有效的排列为 $[1,2,4,3]$、$[3,4,2,1]$ 和 $[1,4,3,2]$。AI (CI翻译)

In the fifth test case, all permutations of length $6$ are valid.

在第五个测试用例中,长度为 $6$ 的所有排列都有效。AI (CI翻译)

样例

Input #1
7
1
0
1
-1
1
1
4
0 0 -1 1
6
-1 -1 -1 -1 -1 -1
3
0 1 0
7
0 -1 0 -1 2 1 -1
Output #1
1
1
0
3
720
2
77
InputCopy OutputCopy
题目信息
题号
2245F
时间限制
5 s
内存限制
128 MB
标签
暴力
组合数学
动态规划

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