#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

constexpr ll mod = 1e9+7;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

template<typename Monoid,typename F>
class SegmentTree{
private:
    int sz;
    vector<Monoid> seg;
    const F op;
    const Monoid e;
public:
    SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){
        sz = 1;
        while(sz<=n) sz <<= 1;
        seg.assign(2*sz,e);
    }
    void set(int k, const Monoid &x){
        seg[k+sz] = x;
    }
    void build(){
        for(int i=sz-1;i>0;i--){
            seg[i] = op(seg[2*i],seg[2*i+1]);
        }
    }
    void update(int k,const Monoid &x){
        k += sz;
        seg[k] = x;
        while(k>>=1){
            seg[k] = op(seg[2*k],seg[2*k+1]);
        }
    }
    Monoid query(int l,int r){
        Monoid L = e,R = e;
        for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){
            if(l&1) L = op(L,seg[l++]);
            if(r&1) R = op(seg[--r],R);
        }
        return op(L,R);
    }
    Monoid operator[](const int &k)const{
        return seg[k+sz];
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<int> P(N);
    vec<int> A(N);
    auto op = [](ll a,ll b){return a+b;};
    SegmentTree<int,decltype(op)> seg(N,op,0);
    for(int i=0;i<N;i++){
        cin >> P[i];
        P[i]--;
        if(P[i]>=0){
            A[P[i]]++;
        }
    }
    seg.build();
    vec<int> v;
    ll cnt = 0,sum = 0;
    for(int i=0;i<N;i++) if(!A[i]){
        sum += i;
        v.push_back(i);
    }
    ll n = v.size();
    vec<mint> fact(N+1,1);
    for(int i=1;i<=N;i++) fact[i] = fact[i-1]*i;

    mint ans = 0;
    ll space = 0;
    for(int i=0;i<N;i++){
        if(P[i]==-1){
            mint now = fact[n-1]*sum;
            now -= fact[n-1]*cnt;
            if(space) now -= fact[n-2]*n*(n-1)/2*space;
            space++;
            ans += now*fact[N-i-1];
        }else{
            mint now = fact[n]*P[i];
            ll s = seg.query(0,P[i]);
            now -= fact[n]*s;
            ll id = lower_bound(v.begin(),v.end(),P[i])-v.begin();
            if(n) now -= fact[n-1]*space*id;
            ans += now*fact[N-i-1];
            cnt += (n-id);
            seg.update(P[i],1);
        }
    }
    cout << ans+fact[n] << "\n";
}

Measured memory_bytes: 14320