P29298 [洛谷] Ryoku's Inversions
提高
题目描述

Problem Description

Ryoku has a permutation $A = \{a_i\}$ of the positive integers $\{1, 2, \cdots, n\}$.

She gives you a sequence $B = \{b_i\}$, which indicates that for each number $a_i$, there are $b_i$ numbers that can form an inversion pair with $a_i$ for all $j > i$ (An inversion pair is defined as a pair $(a_i, a_j)$ such that $i > j$ and $a_i < a_j$).

Unfortunately, the sequence $B$ given by Ryoku has some positions that are corrupted. You want to know how many possible permutations $A$ can satisfy the conditions.

Please output the number of possible permutations $A$ and construct the lexicographically smallest permutation $A$ (For permutations $A = \{a_i\}$ and $A' = \{a'_i\}$, if there exists a position $i$ such that $\forall j < i, a_j = a'_j$ and $a_i < a'_i$, then $A$ is lexicographically smaller than $A'$).

Input Format

The input contains two lines.
The first line contains an integer $n$.
The second line contains $n$ integers, which are the sequence $B$. If $b_i = -1$, it indicates that this position is corrupted.

Output Format

The output contains two lines.
The first line contains an integer, which is the number of possible permutations $A$ modulo $10^9 + 7$.
The second line contains $n$ integers, which are the lexicographically smallest permutation that meets the conditions. If the answer on the first line is $0$, the second line does not need to be output.

Sample Input and Output

Sample Input #1

5
0 3 0 0 0

Sample Output #1

1
1 5 2 3 4

Sample Input #2

5
0 3 -1 0 0

Sample Output #2

3
1 5 2 3 4

Sample Input #3

5
0 3 -1 0 1

Sample Output #3

0

Notes

[Sample 1 Explanation]

For $5$, there are inversion pairs $(5, 2), (5, 3), (5, 4)$, totaling three pairs.

[Sample 2 Explanation]

The permutations that meet the conditions are: $\{1, 5, 4, 2, 3\}, \{1, 5, 3, 2, 4\}, \{1, 5, 2, 3, 4\}$. There are three in total, with the lexicographically smallest being $\{1, 5, 2, 3, 4\}$.


[Data Range and Constraints]

For $10\%$ of the data, $b_i \neq -1$.
For another $10\%$ of the data, $n \le 10$.
For another $10\%$ of the data, $b_i = -1$.
For another $30\%$ of the data, $n \le 10^3$.
For another $30\%$ of the data, $n \le 10^5$.
For $100\%$ of the data, $0 < n \le 10^6$, $-1 \le b_i \le n$.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
5 0 3 0 0 0
样例输出
1 1 5 2 3 4