#codefestival2016qualCe. [codefestival_2016_qualC_e]Encyclopedia of Permutations

[codefestival_2016_qualC_e]Encyclopedia of Permutations

Problem Statement

One day Mr. Takahashi picked up a dictionary containing all of the NN! permutations of integers 11 through NN. The dictionary has NN! pages, and page ii (1iN!1 ≤ i ≤ N!) contains the ii-th permutation in the lexicographical order.

Mr. Takahashi wanted to look up a certain permutation of length NN in this dictionary, but he forgot some part of it.

His memory of the permutation is described by a sequence P1,P2,...,PNP_1, P_2, ..., P_N. If Pi=0P_i = 0, it means that he forgot the ii-th element of the permutation; otherwise, it means that he remembered the ii-th element of the permutation and it is PiP_i.

He decided to look up all the possible permutations in the dictionary. Compute the sum of the page numbers of the pages he has to check, modulo 109+710^9 + 7.

Constraints

  • 1N5000001 ≤ N ≤ 500000
  • 0PiN0 ≤ P_i ≤ N
  • PiPjP_i ≠ P_j if iji ≠ j (1i,jN1 ≤ i, j ≤ N), Pi0P_i ≠ 0 and Pj0P_j ≠ 0.

Partial Score

  • In test cases worth 500500 points, 1N30001 ≤ N ≤ 3000.

Input

The input is given from Standard Input in the following format:

NN P1P_1 P2P_2 ...... PNP_N

Output

Print the sum of the page numbers of the pages he has to check, as modulo 109+710^9 + 7.


Sample Input 1

4
0 2 3 0

Sample Output 1

23

The possible permutations are [11, 22, 33, 44] and [44, 22, 33, 11]. Since the former is on page 11 and the latter is on page 2222, the answer is 2323.


Sample Input 2

3
0 0 0

Sample Output 2

21

Since all permutations of length 33 are possible, the answer is 1+2+3+4+5+6=211 + 2 + 3 + 4 + 5 + 6 = 21.


Sample Input 3

5
1 2 3 5 4

Sample Output 3

2

Mr. Takahashi remembered all the elements of the permutation.


Sample Input 4

1
0

Sample Output 4

1

The dictionary consists of one page.


Sample Input 5

10
0 3 0 0 1 0 4 0 0 0

Sample Output 5

953330050