#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005, mod = 1000000007;
int n, m, k, res;
int a[maxn], b[maxn], f[maxn], g[maxn];
int d[maxn], fac[maxn], inv_fac[maxn];
int dp[maxn][maxn];
int fpm(int a, int b, int p) {
  int res = 1;
  while (b) {
    if (b & 1) res = 1ll * res * a % p;
    a = 1ll * a * a % p;
    b >>= 1;
  }
  return res;
}
void init() {
  d[0] = 1;
  d[1] = 0;
  for (int i = 2; i < maxn; i++)
    d[i] = 1ll * (i - 1) * (d[i - 2] + d[i - 1]) % mod;
  fac[0] = 1;
  for (int i = 1; i < maxn; i++) fac[i] = 1ll * i * fac[i - 1] % mod;
  for (int i = 0; i < maxn; i++) inv_fac[i] = fpm(fac[i], mod - 2, mod);
}
int get(int i) {
  if (f[i] == i) return i;
  return (f[i] = get(f[i]));
}
int A(int a, int b) { return 1ll * fac[a] * inv_fac[a - b] % mod; }
void upd(int &a, int b) {
  a += b;
  if (a >= mod) a -= mod;
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] != -1)
      if (a[i] == i || b[a[i]] != 0) {
        puts("0");
        return 0;
      }
    b[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    f[i] = i;
    g[i] = 1;
  }
  for (int i = 1; i <= n; i++)
    if (a[i] != -1) {
      if (get(i) != get(a[i])) {
        g[get(a[i])] += g[get(i)];
        f[get(i)] = f[get(a[i])];
      } else
        g[get(i)] = 0;
    }
  for (int i = 1; i <= n; i++) {
    if (g[get(i)] == 1) m++;
    if (g[get(i)] > 1 && i == get(i)) k++;
  }
  dp[0][0] = 1;
  for (int i = 1; i <= k; i++) {
    int t = 0;
    for (int j = 0; j <= m; j++) {
      upd(t, 1ll * dp[i - 1][j] * fac[m - j] % mod);
      dp[i][j] = 1ll * t * inv_fac[m - j] % mod;
    }
  }
  for (int j = 0; j <= m; j++) upd(res, 1ll * dp[k][j] * d[m - j] % mod);
  res = 1ll * res * fac[k] % mod;
  printf("%d\n", res);
  return 0;
}
