#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
const int inf = 0x3f3f3f3f;
const int N = 300000 + 11;
const int mod = 1e9 + 7;
long long fac[N], ifac[N];
int power(int a, int n) {
  int res = 1;
  while (n) {
    if (n & 1) res = ((long long)res * a) % mod;
    a = ((long long)a * a) % mod;
    n >>= 1;
  }
  return res;
}
void init() {
  int i;
  fac[0] = 1;
  for (i = 0; i < N - 1; i++) fac[i + 1] = ((long long)fac[i] * (i + 1)) % mod;
  ifac[N - 1] = power(fac[N - 1], mod - 2);
  for (i = N - 2; i >= 0; i--)
    ifac[i] = ((long long)ifac[i + 1] * (i + 1)) % mod;
}
int c(int n, int k) {
  return (((long long)fac[n] * ifac[k]) % mod * ifac[n - k]) % mod;
}
long long get(long long x, long long y) {
  return fac[x + y] * power(fac[x], mod - 2) % mod * power(fac[y], mod - 2) %
         mod;
}
bool f[N];
int a[N];
int b[N];
int n;
long long fk(int x) {
  long long res = 0;
  int l, r, i;
  for (i = 1; i <= x; i++) {
    l = i - 1;
    r = x - i;
    res += get(l, r);
    res %= mod;
  }
  return res;
}
long long gan() {
  int i, j;
  int l = -1, r = -1;
  int res = 0;
  bool tg = 0;
  long long ans = 1;
  for (i = 1; i <= n; i++) {
    if (b[i]) {
      if (l == -1) {
        l = r = b[i];
      } else {
        if (b[i] >= l && b[i] <= r) {
          return 0;
        }
        if (b[i] < l) {
          int tp = l - b[i] - 1;
          int fk = res - tp;
          if (fk < 0) {
            return 0;
          }
          ans *= get(tp, res - tp);
          ans %= mod;
          res = 0;
          l = b[i];
          r += fk;
        } else {
          int tp = b[i] - r - 1;
          int fk = res - tp;
          if (fk < 0) {
            return 0;
          }
          ans *= get(tp, res - tp);
          ans %= mod;
          res = 0;
          r = b[i];
          l -= fk;
        }
      }
    } else {
      res++;
    }
  }
  if (l != -1) {
    if (res != (l - 1 + n - r)) {
      return 0;
    }
    int tmp = l - 1;
    ans *= get(tmp, res - tmp);
    ans %= mod;
    return ans;
  } else {
    return fk(n);
  }
}
void work() {
  int i, j;
  cin >> n;
  int x;
  for (i = 1; i <= n; i++) {
    scanf("%d", &x);
    if (x) {
      f[x] = 1;
      a[i] = x;
      b[x] = i;
    }
  }
  if (b[1]) {
    int l = -1, r = -1;
    int res = 0;
    bool tg = 0;
    long long ans = 1;
    for (i = 1; i <= n; i++) {
      if (b[i]) {
        if (l == -1) {
          l = r = b[i];
        } else {
          if (b[i] >= l && b[i] <= r) {
            cout << "0";
            return;
          }
          if (b[i] < l) {
            int tp = l - b[i] - 1;
            int fk = res - tp;
            if (fk < 0) {
              cout << "0";
              return;
            }
            ans *= get(tp, res - tp);
            ans %= mod;
            res = 0;
            l = b[i];
            r += fk;
          } else {
            int tp = b[i] - r - 1;
            int fk = res - tp;
            if (fk < 0) {
              cout << "0";
              return;
            }
            ans *= get(tp, res - tp);
            ans %= mod;
            res = 0;
            r = b[i];
            l -= fk;
          }
        }
      } else {
        res++;
      }
    }
    if (l != -1) {
      if (res != (l - 1 + n - r)) {
        cout << "0";
        return;
      }
      int tmp = l - 1;
      ans *= get(tmp, res - tmp);
      ans %= mod;
      cout << ans << endl;
    } else {
      cout << fk(n) << endl;
    }
  } else {
    int tt = 0;
    for (i = 1; i <= n; i++) {
      if (b[i]) break;
      tt++;
    }
    if (tt == n) {
      cout << fk(n) << endl;
      return;
    }
    long long ans = 0;
    for (i = 1; i <= n; i++) {
      if (b[i]) {
        int st = b[i] - tt;
        if (st >= 1 && !a[st]) {
          b[1] = st;
          ans += gan() * fk(tt) % mod;
        }
        ans %= mod;
        st = b[i] + tt;
        if (st <= n && !a[st]) {
          b[1] = st;
          ans += gan() * fk(tt) % mod;
        }
        ans %= mod;
        break;
      }
    }
    cout << ans << endl;
  }
}
int main() {
  init();
  work();
  return 0;
}
