#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define sz(s) ((int) ((s).size()))
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

typedef long long ll;
typedef long double ld;

const int inf = (int) 1e9 + 100;
const ld eps = 1e-11;
const ld pi = acos(-1.0L);

mt19937 mrand(random_device{} ());
int rnd(int x) {
  return mrand() % x;
}

const int mod = (int) 1e9 + 7;

void add(int & a, int b) {
  if ((a += b) >= mod) {
    a -= mod;
  }
}

int mult(int a, int b) {
  return (ll) a * b % mod;
}

int power(int a, int p) {
  int res = 1;
  while (p) {
    if (p & 1) {
      res = mult(res, a);
    }
    a = mult(a, a);
    p >>= 1;
  }
  return res;
}

const int maxn = 6e5 + 100;

int fact[maxn], invfact[maxn];

void precalc() {
  fact[0] = invfact[0] = 1;
  for (int i = 1; i < maxn; i++) {
    fact[i] = mult(fact[i - 1], i);
    invfact[i] = power(fact[i], mod - 2);
  }
}

int getbinom(int a, int b) {
  return mult(mult(invfact[b], invfact[a - b]), fact[a]);
}

int fenw[maxn];

void fenwadd(int i) {
  for (; i < maxn; i |= i + 1) {
    fenw[i]++;
  }
}

int get(int i) {
  int res = 0;
  for (; i >= 0; i = (i & (i + 1)) - 1) {
    res += fenw[i];
  }
  return res;
}

int n;
int p[maxn];

bool read() {
  if (scanf("%d", &n) < 1) {
    return false;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d\n", p + i);
  }
  return true;
}

int used[maxn];
int cntless[maxn];

void solve() {
  memset(fenw, 0, sizeof(fenw));
  memset(used, 0, sizeof(used));
  int allzero = 0;
  for (int i = 0; i < n; i++) {
    if (p[i]) {
      used[p[i]] = 1;
    }
  }
  memset(cntless, 0, sizeof(cntless));
  for (int i = 1; i <= n; i++) {
    cntless[i] = cntless[i - 1] + !used[i];
  }
  for (int i = 0; i < n; i++) {
    allzero += (p[i] == 0);
  }
  int cntzero = 0;
  int res = 0;
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    if (cntless[i] > cntless[i - 1]) {
      add(sum, i - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (p[i]) {
      int l = cntless[p[i] - 1];
      int r = allzero - l;
      int toadd = 0;
      int tomult = mult(fact[cntzero], fact[allzero - cntzero]);
      if (cntzero) {
        add(toadd, mult(l, mult(getbinom(l + r - 1, cntzero - 1), tomult)));
      }
      add(toadd, mult(get(p[i] - 1), fact[allzero]));
      int ntoadd = mult(fact[allzero], p[i] - 1);
      add(ntoadd, mod - toadd);
      add(res, mult(fact[n - i - 1], ntoadd));
      fenwadd(p[i]);
      add(sum, mod - (allzero - cntless[p[i]]));
    } else {
      int free = cntless[n];
      cntzero++;
      int toadd = mult((mod + 1) / 2, cntzero - 1);
      int ntoadd = mult(sum, power(free, mod - 2));
      add(ntoadd, mod - toadd);
      ntoadd = mult(ntoadd, fact[allzero]);
      add(res, mult(ntoadd, fact[n - i - 1]));
    }
  }
  add(res, fact[cntzero]);
  printf("%d\n", res);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen("text.in", "r", stdin));
  assert(freopen("text.out", "w", stdout));
#endif

  while (true) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time: %.18f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}

Measured memory_bytes: 14080