#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define N_MAX 500002

using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
typedef pair<ll, ll> P;


ll inv[N_MAX],fac[N_MAX],finv[N_MAX];

void init(){
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    for(int i=2;i<N_MAX;i++){
        inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
        fac[i]=fac[i-1]*(ll) i%MOD;
        finv[i]=finv[i-1]*inv[i]%MOD;
    }
}

ll inv_(ll n){
    if(n == 1) return 1;
    else return MOD-inv_(MOD%n)*(MOD/n)%MOD;
}

ll comb(ll n, ll r){
  ll ans;
  if(n < r){
      ans = 0;
  }else{
      ans = (fac[n]*finv[r])%MOD;
      ans = (ans*finv[n-r])%MOD;
      ans = (ans+MOD)%MOD;
  }
  return ans;
}

P add(P a, P b){
    P ans;
    ans.first = (a.first*b.second+a.second*b.first)%MOD;
    ans.second = (a.second*b.second)%MOD;
    return ans;
}

P sub(P a, P b){
    P ans;
    ans.first = ((a.first*b.second-a.second*b.first)%MOD+MOD)%MOD;
    ans.second = (a.second*b.second)%MOD;
    return ans;
}

P mul(P a, P b){
    P ans;
    ans.first = (a.first*b.first)%MOD;
    ans.second = (a.second*b.second)%MOD;
    return ans;
}

//1-indexed
template <typename T>
struct bit{
    int n;
    vector<T> data;

    bit(int n_){
        n = 1;
        while(n < n_) n *= 2;
        data = vector<T>(n+1);
        for(int i = 0; i <= n; i++) data[i] = 0;
    }
    
    T sum(int i){
        T ret = 0;
        while(i > 0){
            ret += data[i];
            i -= i&-i;
        }
        return ret;
    }

    void add(int i, T x){
        while(i <= n){
            data[i] += x;
            i += i&-i;
        }
    }
};

int N;
int p[500000];
ll cnt_inv[500000];
bool used[500001];
vector<int> v;
int cnt_unused;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    init();
    cin >> N;
    for(int i = 0; i < N; i++) {
        cin >> p[i];
        used[p[i]] = true;
    }
    for(int i = 1; i <= N; i++){
        if(!used[i]) {
            v.push_back(i);
            cnt_unused++;
        }
    }
    bit<int> bt(N);
    for(int i = N-1; i >= 0; i--){
        if(p[i] != 0){
            cnt_inv[i] = bt.sum(p[i]);
            bt.add(p[i], 1);
        }
    }
    P inv_exp = P(0, 1);
    ll ans = 0;
    int cnt0 = 0;
    for(int i = N-1; i >= 0; i--){
        if(p[i] == 0){
            P inv = P(0, 1);
            inv = add(inv, P(cnt0, 2));
            inv = add(inv, inv_exp);
            ll tmp = (fac[cnt_unused]*inv.first)%MOD;
            tmp = (tmp*inv_(inv.second))%MOD;
            tmp = (tmp*fac[N-i-1])%MOD;
            // cout << inv.first << ' ' << inv.second << ' ' << tmp << endl;
            ans += tmp;
            ans %= MOD;
            cnt0++;
        }else{
            P inv = P(cnt_inv[i], 1);
            auto ptr = lower_bound(v.begin(), v.end(), p[i]);
            int m = ptr-v.begin();
            P cur_inv = v.size() == 0 ? P(0, 1) : P(m, v.size());
            inv = add(inv, mul(cur_inv, P(cnt0, 1)));
            inv_exp = add(inv_exp, sub(P(1, 1), cur_inv));
            ll tmp = (fac[cnt_unused]*inv.first)%MOD;
            tmp = (tmp*inv_(inv.second))%MOD;
            tmp = (tmp*fac[N-i-1])%MOD;
            // cout << inv.first << ' ' << inv.second << ' ' << tmp << endl;
            ans += tmp;
            ans %= MOD;
        }
        // cout << ans << endl;
    }
    ans += fac[cnt0];
    ans %= MOD;
    cout << ans << endl;
}

Measured memory_bytes: 22256