///#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
///#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,popcnt,avx,avx2,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

/*
    zet:
        find_by_order(k): k-stat [k > 0]
        order_of_key(k) : {el < k}

    gp_hash_table<key, val, custom_hash>

    rope<T>:
        iterators: mutable_begin, ..
        methods  : erase, insert, substr, ..
*/

#define all(x) (x).begin(), (x).end()
#define size(x) (int)((x).size())
#define em_back emplace_back

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<typename T>
using zet = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

mt19937 eng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 5e5 + 4;
const int mod = 1e9 + 7;

struct Node {
    int l, r;
    int c;

    Node* lc;
    Node* rc;

    Node(int l_, int r_) : l(l_), r(r_) {
        c = 0;
        if (l == r) {
            lc = rc = nullptr;
            return;
        }
        int m = (l + r) >> 1;
        lc = new Node(l, m);
        rc = new Node(m + 1, r);
    }

    void modify(int pos) {
        if (l == r) {
            c = 1;
            return;
        }
        int m = (l + r) >> 1;
        if (pos > m) {
            rc->modify(pos);
        } else {
            lc->modify(pos);
        }
        c = lc->c + rc->c;
    }

    int compute(int l_, int r_) {
        if (l == l_ && r == r_) {
            return c;
        }
        int m = (l + r) >> 1;
        if (r_ <= m) {
            return lc->compute(l_, r_);
        } else if (l_ > m) {
            return rc->compute(l_, r_);
        } else {
            return lc->compute(l_, m) + rc->compute(m + 1, r_);
        }
    }
};

inline int sum(int x, int y) {
    x += y;
    return x - (x >= mod ? mod : 0);
}

inline int diff(int x, int y) {
    x -= y;
    return x + (x < 0 ? mod : 0);
}

inline int mult(int x, int y) {
    return (ll)x * y % mod;
}

int c2(int x) {
    if (x & 1) {
        return mult(x, (x - 1) / 2);
    } else {
        return mult(x / 2, x - 1);
    }
}

int n, m, p[N], qsum;
int fact[N], c0[N];
bool us[N];
vector<int> q;

int seg[N];
int cntMax[N];

Node* root;

int cntMin(int pi) {
    if (size(q) == 0) {
        return 0;
    }
    return lower_bound(all(q), pi) - q.begin();
}

void prepare() {
    fact[0] = 1;
    for (int i = 1; i < N; ++i) {
        fact[i] = mult(fact[i - 1], i);
    }
    for (int i = 1; i <= n; ++i) {
        c0[i] = c0[i - 1] + (p[i] == 0);
    }
    m = c0[n];

    for (int i = 1; i <= n; ++i) if (!us[i]) {
        q.em_back(i);
        qsum = sum(qsum, i - 1);
    }

    root = new Node(0, N);
    for (int i = 1; i <= n; ++i) {
        if (i > 1 && p[i] > 1) {
            seg[i] = root->compute(1, p[i] - 1);
        }
        root->modify(p[i]);
    }

    for (int i = 1; i <= n; ++i) {
        cntMax[i] = cntMax[i - 1];
        if (p[i]) {
            cntMax[i] = sum(cntMax[i], size(q) - cntMin(p[i]));
        }
    }
}

int h(int i) {
    if (p[i]) {
        return mult(fact[m], p[i] - 1);
    } else {
        return mult(fact[m - 1], qsum);
    }
}

int g(int i) {
    if (p[i] == 0) {
        return sum(mult(c2(m), mult(c0[i - 1], fact[m - 2])), mult(fact[m - 1], cntMax[i]));
    } else {
        return sum(mult(fact[m], seg[i]), mult(fact[m - 1], mult(c0[i - 1], cntMin(p[i]))));
    }
}

int f(int i) {
    return diff(h(i), g(i));
}

void calc() {
    int ans = fact[m];
    for (int i = 1; i <= n; ++i) {
        ans = sum(ans, mult(f(i), fact[n - i]));
    }
    cout << ans << "\n";
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> p[i];
        us[p[i]] = 1;
    }
    prepare();
    calc();
}

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

    int z = 1;
    /// cin >> z;

    while (z--) {
        solve();
    }

    return 0;
}


Measured memory_bytes: 62164