Teza Round 1 (Codeforces Round 1015, Div. 1 + Div. 2)
E
Blossom
Codeforces
2400
原题
← DF →
翻译中…

You are given a permutation $a$ of length $n$$^{\text{∗}}$ where some elements are missing and represented by $-1$.

翻译中…

Define the value of a permutation as the sum of the MEX$^{\text{†}}$ of all its non-empty subsegments$^{\text{‡}}$.

翻译中…

Find the sum of the value of all possible valid permutations that can be formed by filling in the missing elements of $a$ modulo $10^9 + 7$.

翻译中…

$^{\text{∗}}$ A permutation of length $n$ is an array consisting of $n$ distinct integers from $\bf{0}$ to $\bf{n - 1}$ in arbitrary order. For example, $[1,2,0,4,3]$ is a permutation, but $[0,1,1]$ is not a permutation ($1$ appears twice in the array), and $[0,2,3]$ is also not a permutation ($n=3$ but there is $3$ in the array). $^{\text{†}}$The minimum excluded (MEX) of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest non-negative integer $x$ which does not occur in the collection $c$. $^{\text{‡}}$A sequence $a$ is a subsegment of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). 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 second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \le a_i \lt n$).

翻译中…

It is guaranteed that the elements of $a$ that are not $-1$ are distinct.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, output a single integer — the sum of the value of all possible valid permutations modulo $10^9 + 7$.

翻译中…

Note

翻译中…

In the first test case, the only valid permutation is $[0, 1]$, and the value of $[0, 1]$ is $3$ since:

翻译中…

$$\operatorname{mex}([0]) + \operatorname{mex}([1]) + \operatorname{mex}([0, 1]) = 1 + 0 + 2 = 3$$

翻译中…

So the answer is $3$.

翻译中…

In the second test case, there are two valid permutations: $[0, 1]$ and $[1, 0]$. The value of $[0, 1]$ and the value of $[1, 0]$ is $3$, so the answer is $3 + 3 = 6$.

翻译中…

In the fourth test case, there are two valid permutations: $[0, 2, 1]$ and $[1, 2, 0]$. The value of $[0, 2, 1]$ is $5$ since:

翻译中…

$$\operatorname{mex}([0]) + \operatorname{mex}([2]) + \operatorname{mex}([1]) + \operatorname{mex}([0, 2]) + \operatorname{mex}([2, 1]) + \operatorname{mex}([0, 2, 1]) = 1 + 0 + 0 + 1 + 0 + 3 = 5$$

翻译中…

And the value of $[1, 2, 0]$ is $5$ since:

翻译中…

$$\operatorname{mex}([1]) + \operatorname{mex}([2]) + \operatorname{mex}([0]) + \operatorname{mex}([1, 2]) + \operatorname{mex}([2, 0]) + \operatorname{mex}([1, 2, 0]) = 0 + 0 + 1 + 0 + 1 + 3 = 5$$$$

翻译中…

So the answer is $5 + 5 = 10$$$.

翻译中…

样例

Input #1
5
2
0 -1
2
-1 -1
3
2 0 1
3
-1 2 -1
5
-1 0 -1 2 -1
Output #1
3
6
7
10
104
InputCopy OutputCopy
题目信息
题号
2084E
时间限制
4 s
内存限制
512 MB
标签
二分
组合数学
动态规划
模拟
数学
双指针

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