#P6035. Ryoku 的逆序对

Ryoku 的逆序对

Description

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

She tells you a sequence B={bi}B = \{b_i\}. For each number aia_i, among all j>ij>i, there are exactly bib_i numbers that can form an inversion pair with aia_i (an inversion pair is defined as follows: a pair (ai,aj)(a_i, a_j) that satisfies i<ji<j and ai>aja_i>a_j is called an inversion pair).

Unfortunately, some positions in the sequence BB Ryoku gave you are damaged. You want to know how many possible permutations AA can satisfy the condition.

Please output the answer and construct the lexicographically smallest permutation AA (for permutations A={ai}A = \{a_i\} and A={ai}A' = \{a'_i\}, if there exists a position ii such that j<i,aj=aj\forall j < i, a_j = a'_j and ai<aia_i < a'_i, then AA is lexicographically smaller than AA').

Input Format

The input contains two lines.
The first line contains an integer nn.
The second line contains nn integers, which form the sequence BB. If bi=1b_i = -1, it means this position is damaged.

Output Format

The output contains two lines.
The first line contains an integer, the number of possible permutations AA, taken modulo 109+710^9 + 7.
The second line contains nn integers, the lexicographically smallest permutation that satisfies the condition. If the answer in the first line is 00, then the second line does not need to be output.

5
0 3 0 0 0

1
1 5 2 3 4
5
0 3 -1 0 0

3
1 5 2 3 4
5
0 3 -1 0 1

0

Hint

[Sample 1 Explanation]

For 55, there are inversion pairs (5,2),(5,3),(5,4)(5,2),(5,3),(5,4), a total of three pairs.

[Sample 2 Explanation]

The permutations that satisfy the condition are: $\{1, 5, 4, 2, 3\}, \{1, 5, 3, 2, 4\}, \{1, 5, 2, 3, 4\}$. There are three in total, and the lexicographically smallest is {1,5,2,3,4}\{1, 5, 2, 3, 4\}.


[Constraints]

For 10%10\% of the testdata, bi1b_i \neq -1.
For another 10%10\% of the testdata, n10n \le 10.
For another 10%10\% of the testdata, bi=1b_i = -1.
For another 30%30\% of the testdata, n103n \le 10^3.
For another 30%30\% of the testdata, n105n \le 10^5.
For 100%100\% of the testdata, 0<n1060< n \le 10^6, 1bin-1 \le b_i \le n.

Translated by ChatGPT 5