#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b) {
  if (b == 1) return a;
  if (b == 0) return 1;
  long long int t = power(a, b / 2);
  if (b % 2) {
    return (a * ((t * t) % 1000000007)) % 1000000007;
  }
  return (t * t) % 1000000007;
}
long long int fact[100005];
long long int C(long long int n, long long int r) {
  if (r == 0) return 1;
  if (n == r) return 1;
  long long int t = (fact[n] * power(fact[n - r], 1000000007 - 2)) % 1000000007;
  return (t * power(fact[r], 1000000007 - 2)) % 1000000007;
}
long long int posi[100005];
int main() {
  ios::sync_with_stdio(false);
  long long int n;
  cin >> n;
  int count = 0;
  int u = 0;
  fact[0] = 1;
  for (long long int i = 1; i < 100005; ++i)
    fact[i] = (fact[i - 1] * i) % 1000000007;
  long long int A[n + 1];
  for (long long int i = 1; i <= n; ++i) {
    cin >> A[i];
    if (A[i]) posi[A[i]] = i;
  }
  long long int mi = 1000000;
  int pos = -1;
  for (int i = 1; i <= n; ++i) {
    if (A[i]) {
      u++;
      mi = min(A[i], mi);
      if (mi == A[i]) pos = i;
    }
  }
  if (pos == -1) {
    cout << power(2, n - 1) << endl;
    return 0;
  }
  long long int ans = 1;
  long long int lindex = pos;
  long long int rindex = pos;
  long long int before = mi;
  if (mi == 1) {
    count = 1;
  }
  for (long long int i = mi + 1; i <= n; ++i) {
    if (posi[i]) {
      if (posi[i] > rindex) {
        long long int y = posi[i] - rindex;
        if (count == 0) {
          int piss = 0;
          if (y > mi - 1 && i - before - 1 >= y - 1 - mi + 1) {
            ans = (ans * power(2, mi - 2)) % 1000000007;
            ans = (ans * C(i - before - 1, y - 1 - mi + 1)) % 1000000007;
          } else {
            piss++;
          }
          int flag = 0;
          for (long long int j = 1; j <= mi - 1; ++j) {
            if (pos - j == 0) {
              flag = 1;
              break;
            }
            if (A[pos - j]) {
              flag = 1;
              break;
            }
          }
          if (!flag) {
            if (i - before >= y) {
              if (piss == 1) ans = 0;
              ans += (power(2, mi - 2) * C(i - before - 1, y - 1)) % 1000000007;
              ans = ans % 1000000007;
            } else
              piss++;
          } else {
            piss++;
          }
          if (piss == 2) {
            cout << "0" << endl;
            return 0;
          }
          rindex = posi[i];
          lindex -= (i - y - 1);
          if (lindex <= 0) {
            cout << "0" << endl;
            return 0;
          }
          before = i;
          count++;
          continue;
        }
        if (i - before - 1 >= y - 1)
          ans = (ans * C(i - before - 1, y - 1)) % 1000000007;
        else {
          cout << "0" << endl;
          return 0;
        }
        rindex = posi[i];
        lindex -= (i - before - y);
        if (lindex <= 0) {
          cout << "0" << endl;
          return 0;
        }
        before = i;
      } else if (posi[i] < lindex) {
        long long int y = lindex - posi[i];
        if (count == 0) {
          int piss = 0;
          if (y > mi - 1 && i - before - 1 >= y - 1 - mi + 1) {
            ans = (ans * power(2, mi - 2)) % 1000000007;
            ans = (ans * C(i - before - 1, y - 1 - mi + 1)) % 1000000007;
          } else {
            piss++;
          }
          int flag = 0;
          for (long long int j = 1; j <= mi - 1; ++j) {
            if (pos + j > n) {
              flag = 1;
              break;
            }
            if (A[pos + j]) {
              flag = 1;
              break;
            }
          }
          if (!flag) {
            if (i - before >= y) {
              if (piss == 1) ans = 0;
              ans += (power(2, mi - 2) * C(i - before - 1, y - 1)) % 1000000007;
              ans = ans % 1000000007;
            }
          } else {
            piss++;
          }
          if (piss == 2) {
            cout << "0" << endl;
            return 0;
          }
          lindex = posi[i];
          rindex += (i - y - 1);
          if (rindex > n) {
            cout << "0" << endl;
            return 0;
          }
          before = i;
          count++;
          continue;
        }
        if (i - before - 1 >= y - 1)
          ans = (ans * C(i - before - 1, y - 1)) % 1000000007;
        else {
          cout << "0" << endl;
          return 0;
        }
        lindex = posi[i];
        rindex += (i - before - y);
        if (rindex > n) {
          cout << "0" << endl;
          return 0;
        }
        before = i;
      } else if (posi[i] >= lindex && posi[i] <= rindex) {
        cout << "0" << endl;
        return 0;
      }
    }
  }
  if (lindex <= 0 || rindex > n) {
    cout << "0" << endl;
    return 0;
  }
  if (u == 1) {
    if (mi == 1) {
      cout << C(n - 1, n - rindex) << endl;
      return 0;
    }
    ans = 0;
    if (n - rindex >= mi - 1 && n - before >= n - rindex - mi + 1) {
      ans = 1;
      ans = (ans * power(2, mi - 2)) % 1000000007;
      ans = (ans * C(n - before, n - rindex - mi + 1)) % 1000000007;
    }
    if (lindex - 1 >= mi - 1 && n - before >= n - rindex) {
      ans += (power(2, mi - 2) * C(n - before, n - rindex)) % 1000000007;
      ans = ans % 1000000007;
    }
    cout << ans << endl;
    return 0;
  }
  cout << (ans * C(n - before, n - rindex)) % 1000000007 << endl;
  return 0;
}
