Problem:
Score : 1200 points

Problem Statement

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

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

His memory of the permutation is described by a sequence P_1, P_2, ..., P_N.
If P_i = 0, it means that he forgot the i-th element of the permutation; otherwise, it means that he remembered the i-th element of the permutation and it is P_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 10^9 + 7.

Constraints

1 ≤ N ≤ 500000

0 ≤ P_i ≤ N

P_i ≠ P_j if i ≠ j (1 ≤ i, j ≤ N), P_i ≠ 0 and P_j ≠ 0.

Partial Score

In test cases worth 500 points, 1 ≤ N ≤ 3000.

Input

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

N
P_1 P_2 ... P_N

Output

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

Sample Input 1

4
0 2 3 0

Sample Output 1

23

The possible permutations are [1, 2, 3, 4] and [4, 2, 3, 1].
Since the former is on page 1 and the latter is on page 22, the answer is 23.

Sample Input 2

3
0 0 0

Sample Output 2

21

Since all permutations of length 3 are possible, the answer is 1 + 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

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;
import java.io.*;

public class Main {

	long mod = 1000000007;
	int MAX = 600000;
	long[] fact = new long[MAX];

	void precalc() {
		fact[0] = 1;
		for (int i = 1; i < MAX; i++) {
			fact[i] = fact[i - 1] * i % mod;
		}
	}

	void solve() {
		precalc();
		int n = in.nextInt();
		int[] p = new int[n];
		TreeSet<Integer> free = new TreeSet<>();
		for (int i = 0; i < n; i++) {
			free.add(i);
		}
		for (int i = 0; i < n; i++) {
			p[i] = in.nextInt() - 1;
			if (p[i] != -1) {
				free.remove(p[i]);
			}
		}

		int[] all = new int[free.size()];
		int numFree = 0;
		for (int i : free) {
			all[numFree++] = i;
		}
		
		long result = 0;
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (p[i] == -1 && p[j] == -1) {
					long add = 1L * numFree * (numFree - 1) / 2 % mod * fact[n - i - 1] % mod * fact[numFree - 2] % mod;
					result = (result + add) % mod;
				} else if (p[i] == -1) {
					long add = 1L * (numFree - findLess(all, p[j])) * fact[n - i - 1] * fact[numFree - 1] % mod;
					result = (result + add) % mod;
				} else if (p[j] == -1) {
					long add = 1L * findLess(all, p[i]) * fact[n - i - 1] % mod * fact[numFree - 1] % mod;
					result = (result + add) % mod;
				} else {
					if (p[i] > p[j]) {
						result = (result + fact[n - i - 1] * fact[numFree]) % mod;
					}
				}
			}
		}
		out.println((result + fact[numFree]) % mod);
	}

	int findLess(int[] a, int i) {
		int pos = Arrays.binarySearch(a, i);
//		System.err.println(i + " " + pos);
		return -pos - 1;
	}

	FastScanner in;
	PrintWriter out;

	void run() {
		in = new FastScanner();
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		public FastScanner(String s) {
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		public String nextToken() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
				}
			}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(nextToken());
		}

		public long nextLong() {
			return Long.parseLong(nextToken());
		}

		public double nextDouble() {
			return Double.parseDouble(nextToken());
		}
	}

	public static void main(String[] args) {
		new Main().run();
	}
}