#include <bits/stdc++.h>
using namespace std;
const int N = int(3e5), mod = int(1e9) + 7;
int p[N];
bool used[N];
bool flag;
int n;
int pos[N];
long long first[N], d[N];
long long modpow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
long long C(int n, int k) {
  if (k > n) return 0;
  long long res = first[n] * d[n - k] % mod;
  return res * d[k] % mod;
}
void print(long long d) {
  printf("%I64d", d);
  exit(0);
}
int main() {
  scanf("%d", &n);
  first[0] = 1;
  d[0] = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
    if (p[i]) flag = 1;
    pos[p[i]] = i;
    first[i] = first[i - 1] * i % mod;
    d[i] = modpow(first[i], mod - 2);
  }
  if (!flag) {
    print(modpow(2, n - 1));
  }
  int last = 0, fs = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (pos[i]) {
      fs = i;
      break;
    }
  }
  long long r1, r2;
  r1 = r2 = 0;
  bool ok = 1;
  int l = pos[fs] - 1 - fs + 2;
  if (l <= 0)
    ok = 0;
  else
    for (int i = pos[fs] - 1; i >= l; i--)
      if (p[i]) ok = 0;
  if (ok && fs != 1) {
    r1 = modpow(2, max(fs - 2, 0));
    int L = l, R = pos[fs];
    int last = fs;
    for (int i = fs + 1; i <= n; i++) {
      if (pos[i]) {
        if (L > pos[i]) {
          if (n - pos[i] < i - 1) {
            r1 = 0;
            break;
          }
          int k = L - pos[i] - 1;
          int v = i - 1 - (R - L + 1);
          r1 = r1 * C(v, k) % mod;
          L = pos[i];
          R = i + L - 1;
        } else {
          if (pos[i] < i) {
            r1 = 0;
            break;
          }
          int k = pos[i] - R - 1;
          int v = i - 1 - (R - L + 1);
          r1 = r1 * C(v, k) % mod;
          R = pos[i];
          L = R - i + 1;
        }
      }
    }
    if (!pos[n]) {
      int k = n - R;
      int v = n - (R - L + 1);
      r1 = r1 * C(v, k) % mod;
    }
  }
  int r = pos[fs] + fs - 1;
  ok = 1;
  if (r > n)
    ok = 1;
  else
    for (int i = pos[fs] + 1; i <= r; i++)
      if (p[i]) ok = 0;
  if (ok) {
    r2 = modpow(2, max(fs - 2, 0));
    int L = pos[fs], R = r;
    for (int i = fs + 1; i <= n; i++) {
      if (pos[i]) {
        if (L > pos[i]) {
          if (n - pos[i] < i - 1) {
            r1 = 0;
            break;
          }
          int k = L - pos[i] - 1;
          int v = i - 1 - (R - L + 1);
          r2 = r2 * C(v, k) % mod;
          L = pos[i];
          R = i + L - 1;
        } else {
          if (pos[i] < i) {
            r1 = 0;
            break;
          }
          int k = pos[i] - R - 1;
          int v = i - 1 - (R - L + 1);
          r2 = r2 * C(v, k) % mod;
          R = pos[i];
          L = R - i + 1;
        }
      }
    }
    if (!pos[n]) {
      int k = n - R;
      int v = n - (R - L + 1);
      r2 = r2 * C(v, k) % mod;
    }
  }
  printf("%I64d", (r1 + r2) % mod);
  return 0;
}
