P22342 [HackerRank] Cards permutation
提高
题目描述

Alice was given the $n$ integers from $1$ to $n$. She wrote all possible permutations in increasing lexicographical order, and wrote each permutation in a new line. For example, for $n=3$, there are $\boldsymbol{6}$ possible permutations:

$[1,2,3]$
$[1,3,2]$
$[2,1,3]$
$[2,3,1]$
$[3,1,2]$
$[3,2,1]$

She then chose one permutation among them as her favorite permutation.

After some time, she forgot some elements of her favorite permutation. Nevertheless, she still tried to write down its elements. She wrote a $0$ in every position where she forgot the true value.

She wants to know the sum of the line numbers of the permutations which could possibly be her favorite permutation, i.e., permutations which can be obtained by replacing the $0$s. Can you help her out?

Since the sum can be large, find it modulo $10^9+7$.

Input Format

The first line contains a single integer $n$.

The next line contains $n$ space-separated integers $a_1,a_2,\ldots,a_n$ denoting Alice's favorite permutation with some positions replaced by $0$.

Constraints

$1\leq n\leq3\cdot10^5$
$0\leq a_i\leq n$
The positive values appearing in $[a_1,\ldots,a_n]$ are distinct.

Subtask

For ~33% of the total points, $n\leq5000$

Output Format

Print a single line containing a single integer denoting the sum of the line numbers of the permutations which could possibly be Alice's favorite permutation.

Sample Input 0
4
0 2 3 0

Sample Output 0
23

Explanation 0

The possible permutations are $[1,2,3,4]$ and $[4,2,3,1]$. The permutation $[1,2,3,4]$ occurs on line $1$ and the permutation $[4,2,3,1]$ occurs on line $22$. Therefore the sum is $1+22=23$.

Sample Input 1
4
4 3 2 1

Sample Output 1
24

Explanation 1

There is no missing number in the permutation. Therefore, the only possible permutation is $[4,3,2,1]$, and it occurs on line $24$. Therefore the sum is $24$.

输入格式

见题面

输出格式

见题面

样例输入
4 0 2 3 0
样例输出
23