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

using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

using namespace std;
typedef long long ll;

#ifdef LOCAL
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__);
#else
#define dbg(...) 17;
#endif

template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; }
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) { bool f = true; os << "{"; for (const auto &x : c) { if (!f) os << ", "; f = false; os << x; } return os << "}"; }
template<typename T> void debug(string s, T x) { cerr << s << " = " << x << "\n"; }
template<typename T, typename... Args> void debug(string s, T x, Args... args) { cerr << s.substr(0, s.find(',')) << " = " << x << " | "; debug(s.substr(s.find(',') + 2), args...); }

const int MOD = 1e9 + 7;
typedef decay<decltype(MOD)>::type T;
struct mi {
    T val;
    explicit operator T() const { return val; }
    mi() { val = 0; }
    mi(const long long& v) {
        val = (-MOD <= v && v <= MOD) ? v : v % MOD;
        if (val < 0) val += MOD; }
    friend ostream& operator<<(ostream& os, const mi& a) { return os << a.val; }
    friend bool operator==(const mi& a, const mi& b) { return a.val == b.val; }
    friend bool operator!=(const mi& a, const mi& b) { return !(a == b); }
    friend bool operator<(const mi& a, const mi& b) { return a.val < b.val; }
    mi operator-() const { return mi(-val); }
    mi& operator+=(const mi& m) {
        if ((val += m.val) >= MOD) val -= MOD;
        return *this; }
    mi& operator-=(const mi& m) {
        if ((val -= m.val) < 0) val += MOD;
        return *this; }
    mi& operator*=(const mi& m) { val = (long long) val * m.val % MOD;
        return *this; }
    friend mi pow(mi a, long long p) {
        mi ans = 1; assert(p >= 0);
        for (; p; p /= 2, a *= a) if (p & 1) ans *= a;
        return ans; }
    friend mi inv(const mi& a) { assert(a != 0); return pow(a, MOD - 2); }
    mi& operator/=(const mi& m) { return (*this) *= inv(m); }
    friend mi operator+(mi a, const mi& b) { return a += b; }
    friend mi operator-(mi a, const mi& b) { return a -= b; }
    friend mi operator*(mi a, const mi& b) { return a *= b; }
    friend mi operator/(mi a, const mi& b) { return a /= b; }
};

int main() {
    int n; cin >> n;
    vector<mi> fact(n + 1);
    fact[0] = 1;
    for (int i = 1; i <= n; i++) {
        fact[i] = fact[i - 1] * i;
    }
    vector<int> p(n);
    set<int> used;
    for (int i = 0; i < n; i++) {
        cin >> p[i]; p[i]--;
        if (p[i] != -1) {
            used.insert(p[i]);
        }
    }
    vector<int> unused;
    for (int i = 0; i < n; i++) {
        if (used.count(i) == 0) {
            unused.push_back(i);
        }
    }
    sort(unused.begin(), unused.end());
    if (unused.size() == 1) {
        for (int i = 0; i < n; i++) {
            if (p[i] == -1) {
                p[i] = unused.back();
            }
        }
        unused.pop_back();
    }
    mi ans = fact[unused.size()];
    Tree<int> vals;
    mi sum = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (p[i] == -1) {
            continue;
        }
        sum += vals.order_of_key(p[i]) * fact[n - 1 - i];
        vals.insert(p[i]);
    }
    ans += sum * fact[unused.size()];
    if (unused.size() == 0) {
        cout << ans << '\n';
        return 0;
    }
    vals.clear();
    for (int x: unused) {
        vals.insert(x);
    }
    vector<mi> facts;
    for (int i = 0; i < n; i++) {
        if (p[i] == -1) {
            facts.push_back(fact[n - 1 - i]);
        }
    }
    sum = 0;
    for (int j = 0; j < facts.size(); j++) {
        sum += facts[j] * (facts.size() - j - 1);
    }
    sum *= fact[vals.size()] / 2;
    ans += sum;
    mi bef = 0;
    for (int i = 0; i < n; i++) {
        if (p[i] == -1) {
            bef += fact[n - 1 - i];
        } else {
            int big = (int) vals.size() - vals.order_of_key(p[i]);
            ans += bef * big * fact[vals.size() - 1];
        }
    }
    mi aft = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (p[i] == -1) {
            aft += 1;
        } else {
            int small = vals.order_of_key(p[i]);
            ans += aft * fact[n - 1 - i] * small * fact[vals.size() - 1];
        }
    }
    cout << ans << '\n';
    return 0;
}


Measured memory_bytes: 53960