#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "functional"

using namespace std;

//constexpr long long int MOD = 1000000007;
constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

long long int power(long long int x, long long int n, long long int M) {
	long long int ret = 1;
	long long int by = x;
	while (n) {
		if (n & 1) {
			ret *= by;
			ret %= M;
		}
		by *= by;
		by %= M;
		n >>= 1;
	}
	return ret;
}

class Add_Segment_Tree {
	vector<long long int>v;
	vector<int>l;
	vector<int>r;
	long long int ret;
	int num;
	long long int Update(int place) {
		if (place >= v.size() / 2) {
			return v[place];
		}
		v[place] = Update(place * 2) + Update(place * 2 + 1);
		return v[place];
	}
	void Sum(int a, int b, int place) {
		if (l[place] >= a && r[place] <= b) {
			ret += v[place];
			return;
		}
		if (l[place] > b || r[place] < a) return;
		Sum(a, b, place * 2);
		Sum(a, b, place * 2 + 1);
		return;
	}
public:
	void Left(int place) {
		if (place >= v.size() / 2) {
			l[place] = place - v.size() / 2;
			return;
		}
		Left(place * 2);
		Left(place * 2 + 1);
		l[place] = l[place * 2];
		return;
	}
	void Right(int place) {
		if (place >= v.size() / 2) {
			r[place] = place - v.size() / 2;
			return;
		}
		Right(place * 2);
		Right(place * 2 + 1);
		r[place] = r[place * 2 + 1];
		return;
	}
	Add_Segment_Tree(int n) {
		n++;
		num = 1;
		while (num < n * 2)num *= 2;
		l.resize(num);
		r.resize(num);
		v.resize(num, 0);
		Left(1);
		Right(1);
	}
	void Add(int place, long long int num, bool update) {
		place += v.size() / 2;
		v[place] += num;
		if (!update)return;
		place /= 2;
		while (place) {
			v[place] = v[place * 2] + v[place * 2 + 1];
			place /= 2;
		}
	}
	void TopDown() {
		Update(1);
	}
	long long int Sum(int a, int b) {
		ret = 0;
		Sum(a, b, 1);
		return ret;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<long long int>v(N);
	for (auto &i : v)cin >> i;
	vector<long long int>by(N + 1, 1);
	for (int i = 2; i <= N; i++) {
		by[i] = by[i - 1] * i;
		by[i] %= MOD;
	}
	int zero = 0;
	for (auto i : v)zero += !i;
	long long int ans = by[zero];
	int cnt = 0;
	for (int i = N - 1; i >= 0; i--) {
		if (v[i])continue;
		long long int add = (long long int)cnt * zero*(zero - 1)/2;
		add %= MOD;
		add *= by[N - 1 - i];
		add %= MOD;
		add *= by[zero - 2];
		add %= MOD;
		ans += add;
		ans %= MOD;
		cnt++;
	}
	vector<long long int>sum(N + 1,1);
	sum[0] = 0;
	for (auto i : v) {
		if (i)sum[i]--;
	}
	for (int i = 1; i <= N; i++)sum[i] += sum[i - 1];
	int space = 0;
	//cout << ans << endl;
	vector<long long int>box(N);
	long long int bag = 0;
	for (int i = N - 1; i >= 0; i--) {
		if (!v[i]) {
			space++;
			ans += bag * by[N - i - 1];
			ans %= MOD;
			continue;
		}
		long long int add = by[zero - 1];
		add *= by[N - i - 1];
		add %= MOD;
		add *= space;
		add %= MOD;
		add *= sum[v[i] - 1];
		add %= MOD;
		bag += (zero - sum[v[i] - 1])*by[zero-1];
		bag %= MOD;
		ans += add;
		ans %= MOD;

		//cout << add << endl;
	}
	//cout << ans << endl;
	Add_Segment_Tree asg(N);
	for (int i = N - 1; i >= 0; i--) {
		if (!v[i])continue;
		long long int add = by[zero];
		add *= by[N - i - 1];
		add %= MOD;
		add *= asg.Sum(1, v[i]);
		add %= MOD;
		ans += add;
		ans %= MOD;
		asg.Add(v[i], 1, true);
	}
	cout << ans << endl;
}

Measured memory_bytes: 32384