#include <bits/stdc++.h>
using namespace std;
const long long inf = 4e18;
const int N = 5000000;
const int MOD = 1e9 + 7;
int n, a[N], b[N], f[N];
bool check(int l1, int r1, int l2, int r2) {
  if (l2 > r2) return true;
  if (l2 <= 0) return false;
  if (l2 > n) return false;
  if (r2 <= 0) return false;
  if (r2 > n) return false;
  if (l1 == -1 && r1 == 0) return true;
  return ((l1 >= l2) && (r1 <= r2));
}
int power(int x, int p) {
  int base = x, res = 1;
  while (p) {
    if (p & 1) res = (1ll * res * base) % MOD;
    base = (1ll * base * base) % MOD;
    p >>= 1;
  }
  return res;
}
int C(int n, int k) {
  if (n < k) return 0;
  int p1 = power(f[k], MOD - 2);
  int p2 = power(f[n - k], MOD - 2);
  int p = (1ll * p1 * p2) % MOD;
  int res = (1ll * f[n] * p) % MOD;
  return res;
}
int solve(int l, int r, int i) {
  if (i > n) return 1;
  if (b[i] == 0) {
    if (i == n) {
      if (l == -1 && r == 0)
        return power(2, n - 1);
      else
        return C(n - r + l - 1, l - 1);
    } else {
      return solve(l, r, i + 1);
    }
  }
  int l1 = b[i] - i + 1;
  int r1 = b[i] - 1;
  int l2 = b[i] + 1;
  int r2 = b[i] + i - 1;
  int res1 = 0, res2 = 0;
  if (l == -1 && r == 0) {
    if (check(l, r, l1, r1))
      res1 = (1ll * power(2, max(0, i - 2)) * solve(l1, b[i], i + 1)) % MOD;
    if (check(l, r, l2, r2) && i != 1)
      res2 = (1ll * power(2, max(0, i - 2)) * solve(b[i], r2, i + 1)) % MOD;
  } else {
    if (check(l, r, l1, r1))
      res1 = (1ll * C(i - r + l - 2, l - l1) * solve(l1, b[i], i + 1)) % MOD;
    if (check(l, r, l2, r2))
      res2 = (1ll * C(i - r + l - 2, l - l2) * solve(b[i], r2, i + 1)) % MOD;
  }
  int res = (res1 + res2) % MOD;
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    if (a[i] != 0) b[a[i]] = i;
  f[0] = 1;
  for (int i = 1; i <= 3 * n; i++) f[i] = (1ll * f[i - 1] * i) % MOD;
  cout << solve(-1, 0, 1);
  return 0;
}
