#include <bits/stdc++.h>
static const long long p = 1000000007;
static long long a[110000], invp[110000];
using namespace std;
static long long ans = 1, mlt1 = 0, mlt2 = 0;
long long combo(int k, int n) {
  long long ret = 1;
  if (n < k) return 0;
  if (k > n - k) k = n - k;
  for (int i = 1; i <= k; i++) {
    ret *= invp[i] * (n - i + 1) % p;
    ret %= p;
  }
  return ret;
}
int main() {
  int n, i, j, bred = 0, previous = (int)p, l = -1, r = -1, restL, restR, all;
  cin >> n;
  invp[1] = 1;
  for (i = 2; i < 100500; i++) {
    invp[i] = (p - (p / i) * invp[p % i] % p) % p;
  }
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] != 0) {
      if (bred == 0) l = i;
      r = i;
      if (bred % 2 == 0) {
        if (previous > a[i]) bred++;
      }
      if (bred % 2 == 1) {
        if (previous < a[i]) bred++;
      }
      previous = a[i];
    }
  }
  if (bred > 2) {
    cout << 0;
    return 0;
  }
  restL = 0;
  restR = n - 1;
  all = n;
  while (l != r) {
    if (a[l] > a[r]) {
      ans *= combo(l - restL, all - a[l]);
      ans %= p;
      restR += (l - restL - all + a[l]);
      restL = l + 1;
      if (restR < r) {
        cout << 0;
        return 0;
      }
      while (true) {
        l++;
        if (a[l] != 0) break;
      }
    } else {
      ans *= combo(restR - r, all - a[r]);
      ans %= p;
      restL -= (restR - r - all + a[r]);
      restR = r - 1;
      if (restL > l) {
        cout << 0;
        return 0;
      }
      while (true) {
        r--;
        if (a[r] != 0) break;
      }
    }
    all = restR - restL + 1;
  }
  if (l == -1) {
    for (i = 1; i < n; i++) {
      ans *= 2;
      ans %= p;
    }
    cout << ans;
    return 0;
  }
  if (a[l] == 1) {
    ans *= combo(l - restL, all - 1);
    ans %= p;
    cout << ans;
    return 0;
  }
  int nrestL = restL, nall = all, nrestR = restR;
  mlt1 += combo(l - nrestL, nall - a[l]);
  mlt1 %= p;
  nrestR += (l - nrestL - nall + a[l]);
  nrestL = l + 1;
  if (nrestR < r) {
    mlt1 = 0;
  }
  nall = nrestR - nrestL + 1;
  for (i = 1; i < nall; i++) {
    mlt1 *= 2;
    mlt1 %= p;
  }
  mlt2 += combo(restR - r, all - a[r]);
  mlt2 %= p;
  restL -= (restR - r - all + a[r]);
  restR = r - 1;
  if (restL > l) {
    mlt2 = 0;
  }
  all = restR - restL + 1;
  for (i = 1; i < all; i++) {
    mlt2 *= 2;
    mlt2 %= p;
  }
  ans *= (mlt1 + mlt2);
  ans %= p;
  cout << ans;
  return 0;
}
