#include <bits/stdc++.h>
using namespace std;
long long dp[3000][3000];
long long a[3000];
long long bak[3000];
const long long mod = 1000 * 1000 * 1000 + 7;
long long calc(long long a, long long b) {
  if (a == 1 && b == 0) return 1;
  if (dp[a][b] >= 0) return dp[a][b];
  dp[a][b] = 0;
  if (a == 0) {
    if (b == 1)
      dp[a][b] = 0;
    else if (b == 2)
      dp[a][b] = 1;
    else
      dp[a][b] = (b - 1) * (calc(a, b - 1) + calc(a, b - 2)) % mod;
  } else {
    dp[a][b] = (calc(a - 1, b) + calc(a - 1, b + 1)) % mod;
  }
  dp[a][b] %= mod;
  return dp[a][b];
}
int main() {
  memset(dp, -1, sizeof dp);
  memset(bak, -1, sizeof bak);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0) {
      a[i]--;
      bak[a[i]] = i;
    }
  }
  long long loop = 0;
  long long neg_one = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i] == -1 && bak[i] == -1) {
      neg_one++;
      continue;
    }
    if (a[i] >= 0) {
      long long t = i;
      for (int ii = 0; ii < n; ii++) {
        if (bak[t] >= 0) t = bak[t];
      }
      while (a[t] >= 0) {
        long long nt = a[t];
        a[t] = -2;
        t = nt;
      }
      if (a[t] == -2)
        ;
      else
        loop++;
    }
  }
  cerr << loop << " " << neg_one << endl;
  cout << calc(loop, neg_one) << endl;
}
