Expected Inversions
View as PDFJosh is given an array  of length 
, where every element is in the range 
 inclusive. However, some elements are missing and are denoted with the value 
.
Josh, being the innovator he is, decides to randomly assign the missing elements so that the resulting array is a permutation of the first  positive integers. Your task is to find the expected number of inversions after Josh has finished filling in missing values!
An inversion is defined to be a pair of indices  such that 
 and 
.
Note: It is guaranteed that there will be at least  way Josh can fill in the missing elements to result in a permutation of the first 
 positive integers.
Constraints
Subtask 1 [30%]
Subtask 2 [70%]
No additional constraints.
Input Specification
The first line will contain , the length of Josh's array.
The next line will contain  space-separated integers in the range 
, with a value of 
 at position 
 meaning that the 
 element is missing.
Output Specification
If the answer can be expressed in the form , where 
 and 
 are relatively prime, output 
, where 
 is the modular inverse of 
. It is guaranteed that the answer is rational.
Sample Input 1
5
4 3 0 2 1
Sample Output 1
8
Explanation for Sample Output 1
There is only  way Josh can assign values to missing elements.
The resulting array is  and there are 
 inversions.
Sample Input 2
2
0 0
Sample Output 2
500000004
Explanation for Sample Output 2
There are  valid arrays, 
 and 
.
The expected number of inversions is . The answer is 
, the modular inverse of 
 is 
, so the answer is 
.
Sample Input 3
6
5 0 2 0 1 0
Sample Output 3
500000013
Explanation for Sample Output 3
There are  valid arrays, 
, 
, 
, 
, 
, 
.
The expected number of inversions is . The answer is 
, the modular inverse of 
 is 
, so the answer is 
.
Comments
There are testcases without any missing elements.
Correct. The problem doesn't guarantee that there are missing numbers. Your code should cover the case.