#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)1e9 + 7;
int a[2005];
int isInEdge[2005];
long long C[2005][2005], fact[2005];
long long dpGood[2005], dpBad[2005], dpCycles[2005];
bool used[2005];
int n;
int ones, twos;
int getChain(int v) {
  int res = 1;
  used[v] = true;
  while (a[v] != -1) {
    v = a[v];
    used[v] = true;
    res++;
  }
  return res;
}
long long fPow(long long val, long long deg) {
  long long res = 1, mul = val;
  while (deg) {
    if (deg & 1) {
      res *= mul;
      res %= mod;
    }
    mul *= mul;
    mul %= mod;
    deg >>= 1;
  }
  return res;
}
long long rev(long long val) { return fPow(val, mod - 2); }
void calcC() {
  C[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    C[i][0] = 1;
    C[i][i] = 1;
    for (int j = 1; j < i; j++) {
      C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
    }
  }
}
void calcFact() {
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % mod;
  }
}
long long trans(long long val) {
  val %= mod;
  val += mod;
  val %= mod;
  return val;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] != -1) a[i]--;
    isInEdge[a[i]] = true;
  }
  twos = 0;
  ones = n;
  for (int i = 0; i < n; i++) {
    if (a[i] != -1 && !isInEdge[i]) {
      ones -= getChain(i);
      twos++;
    }
  }
  for (int i = 0; i < n; i++)
    if (!used[i] && a[i] != -1) ones--;
  calcC();
  calcFact();
  dpCycles[0] = 1;
  for (int i = 1; i <= n; i++) {
    dpCycles[i] = fact[i];
  }
  dpBad[0] = 0;
  dpGood[0] = dpCycles[twos];
  dpBad[1] = dpCycles[twos];
  dpGood[1] = trans(fact[twos + 1] - dpBad[1]);
  for (int i = 2; i <= ones; i++) {
    for (int j = 1; j <= i; j++) {
      long long cur = (C[i][j] * dpGood[i - j]) % mod;
      dpBad[i] += cur;
      dpBad[i] %= mod;
    }
    dpGood[i] = trans(fact[twos + i] - dpBad[i]);
  }
  printf("%I64d", dpGood[ones]);
  return 0;
}
