#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[131072], *head = buf, *tail = buf;
  return ((head == tail) &&
              (tail = (head = buf) + fread(buf, 1, 131072, stdin)),
          head == tail)
             ? EOF
             : *head++;
}
inline int read() {
  int ret = 0;
  char ch = gc();
  while (ch < '0' || ch > '9') ch = gc();
  while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = gc();
  return ret;
}
long long exp(long long a, long long b) {
  if (!b) return 1;
  long long k = exp(a, b / 2);
  if (b & 1) return k * k % 1000000007 * a % 1000000007;
  return k * k % 1000000007;
}
int n, a[1000010], pos[1000010], lz[1000010], rz[1000010], lv[1000010],
    rv[1000010];
int cur = 0;
long long fac[1000010], invfac[1000010];
void init() {
  int lst = 0;
  for (int i = 1; i <= n; i++) {
    lv[i] = a[lst];
    if (!a[i]) {
      if (!a[i - 1])
        lz[i] = lz[i - 1] + 1;
      else
        lz[i] = 1;
    } else {
      lst = i;
      if (!a[i - 1])
        lz[i] = lz[i - 1];
      else
        lz[i] = 0;
    }
  }
  lst = 0;
  for (int i = n; i >= 1; i--) {
    rv[i] = a[lst];
    if (!a[i]) {
      if (!a[i + 1])
        rz[i] = rz[i + 1] + 1;
      else
        rz[i] = 1;
    } else {
      lst = i;
      if (!a[i + 1])
        rz[i] = rz[i + 1];
      else
        rz[i] = 0;
    }
  }
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % 1000000007;
  invfac[n] = exp(fac[n], 1000000007 - 2);
  for (int i = n - 1; i >= 0; i--)
    invfac[i] = invfac[i + 1] * (i + 1) % 1000000007;
}
long long C(int a, int b) {
  return fac[a] * invfac[b] % 1000000007 * invfac[a - b] % 1000000007;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    if (a[i]) pos[a[i]] = i;
  }
  init();
  cur = n;
  int l = 1, r = n;
  long long ans = 1;
  while (cur != 1) {
    if (!pos[cur]) {
      if (a[l] && a[r]) {
        cout << 0 << endl;
        return 0;
      }
      if (a[l])
        cur--, r--;
      else if (a[r])
        cur--, l++;
      else {
        int cntl = rz[l], cntr = lz[r];
        int vall = rv[l], valr = lv[r];
        if (cntl + cntr > r - l + 1) {
          (ans *= exp(2, r - l)) %= 1000000007;
          cur = 1;
          continue;
        }
        if (vall == valr && vall > 1) {
          long long tmp = 0;
          if (cntl <= cur - vall) (tmp += C(cur - vall, cntl)) %= 1000000007;
          if (cntr <= cur - vall) (tmp += C(cur - vall, cntr)) %= 1000000007;
          (tmp *= exp(2, r - l - (cur - vall) - 1)) %= 1000000007;
          (ans *= tmp) %= 1000000007;
          cur = 1;
          continue;
        }
        cerr << "HERE2 " << cntl << ' ' << vall << ' ' << cntr << ' ' << valr
             << endl;
        if (!rv[l]) {
          (ans *= exp(2, cur - 1)) %= 1000000007;
          cur = 1;
          continue;
        }
        if (vall > valr) {
          (ans *= C(cur - vall, cntl)) %= 1000000007;
          l += cntl, r -= (cur - vall) - cntl;
          cur = vall;
        } else {
          (ans *= C(cur - valr, cntr)) %= 1000000007;
          l += (cur - valr) - cntr, r -= cntr;
          cur = valr;
        }
      }
    } else if (pos[cur] != l && pos[cur] != r) {
      cout << 0 << endl;
      return 0;
    } else {
      if (pos[cur] == l)
        l++;
      else
        r--;
      cur--;
    }
  }
  cout << ans << endl;
  return 0;
}
