#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 101010;
const int MOD = 1000000007;
int ni[MAX_N], fact[MAX_N], ni_fact[MAX_N];
void init() {
  ni[1] = 1;
  for (int i = 2; i < MAX_N; i++) {
    int t = MOD / i;
    int k = MOD % i;
    t = (long long)t * t % MOD;
    k = (long long)ni[k] * ni[k] % MOD;
    ni[i] = (long long)i * t % MOD * k % MOD;
  }
  fact[0] = 1, ni_fact[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    fact[i] = (long long)fact[i - 1] * i % MOD;
    ni_fact[i] = (long long)ni_fact[i - 1] * ni[i] % MOD;
  }
}
inline int C(int n, int k) {
  if (n < k) return 0;
  return (int)((long long)fact[n] * ni_fact[k] % MOD * ni_fact[n - k] % MOD);
}
inline int fpm(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;
}
int n, a[MAX_N], pn;
pair<int, int> p[MAX_N];
int gao(int lnow, int rnow, int plow, int phigh, int mm) {
  int tmp = 1;
  while (tmp && !(plow == 0 && phigh == pn - 1)) {
    if (plow == 0) {
      ++phigh;
      int N = p[phigh].first - mm - 1;
      if (N > p[phigh].second - 1 - rnow) tmp = 0;
      tmp = (long long)tmp * C(N, p[phigh].second - 1 - rnow) % MOD;
      rnow = p[phigh].second;
      lnow = 0;
      mm = p[phigh].first;
    } else if (phigh == pn - 1) {
      --plow;
      int N = p[plow].first - mm - 1;
      if (N > lnow - 1 - p[plow].second) tmp = 0;
      tmp = (long long)tmp * C(N, lnow - p[plow].second - 1) % MOD;
      lnow = p[plow].second;
      rnow = 0;
      mm = p[plow].first;
    } else if (p[plow - 1].first < p[phigh + 1].first) {
      int N = p[plow - 1].first - mm - 1;
      if (N > p[phigh + 1].second - 1 - rnow + lnow - 1 - p[plow - 1].second)
        tmp = 0;
      --plow;
      tmp = (long long)tmp * C(N, lnow - p[plow].second - 1) % MOD;
      rnow = rnow + N - (lnow - p[plow].second - 1);
      lnow = p[plow].second;
      mm = p[plow].first;
    } else {
      int N = p[phigh + 1].first - mm - 1;
      if (N > p[phigh + 1].second - 1 - rnow + lnow - 1 - p[plow - 1].second)
        tmp = 0;
      ++phigh;
      tmp = (long long)tmp * C(N, p[phigh].second - 1 - rnow) % MOD;
      lnow = lnow - (N - (p[phigh].second - rnow - 1));
      rnow = p[phigh].second;
      mm = p[phigh].first;
    }
  }
  return tmp;
}
int main() {
  init();
  while (scanf("%d", &n) == 1) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", a + i);
    }
    a[0] = n + 1, a[n + 1] = n + 2;
    pair<int, int> sml = make_pair(MOD, MOD);
    int pl = 0;
    pn = 0;
    for (int i = 0; i <= n + 1; i++) {
      if (a[i]) {
        if (sml.first > a[i]) {
          sml = make_pair(a[i], i);
          pl = pn;
        }
        p[pn++] = make_pair(a[i], i);
      }
    }
    bool ok = true;
    for (int i = 0; i < pn - 1; i++) {
      if (p[i].second < sml.second - 1) {
        if (p[i].first < p[i + 1].first) {
          ok = false;
        }
      } else if (p[i].second >= sml.second) {
        if (p[i].first > p[i + 1].first) {
          ok = false;
        }
      }
    }
    if (!ok) {
      printf("0\n");
      continue;
    }
    int ans = 0;
    if (sml.first == 1) {
      ans += gao(sml.second, sml.second, pl, pl, sml.first);
    } else {
      if (pl != 0 && sml.second - p[pl - 1].second >= sml.first) {
        ans += (long long)fpm(2, sml.first - 2) *
               gao(sml.second - sml.first + 1, sml.second, pl, pl, sml.first) %
               MOD;
      }
      if (pl != pn - 1 && p[pl + 1].second - sml.second >= sml.first) {
        ans += (long long)fpm(2, sml.first - 2) *
               gao(sml.second, sml.second + sml.first - 1, pl, pl, sml.first) %
               MOD;
      }
    }
    printf("%d\n", ans % MOD);
  }
  return 0;
}
