#include <bits/stdc++.h>
using namespace std;
const int N = 2000005, P = 1000000007;
long long h[N];
int n, m, t, a[N];
bool b[N];
void read(int &x) {
  char ch;
  x = 0;
  bool y = 0;
  for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') y = 1;
  for (; ch >= '0' && ch <= '9'; x = x * 10 + ch - 48, ch = getchar())
    ;
  if (y) x = -x;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) read(a[i]), m += a[i] < 0;
  t = n, n = 0;
  for (int i = 1; i <= t; ++i)
    if (!b[i] && a[i] >= 0) {
      int j = i;
      for (; !b[j] && a[j] >= 0; j = a[j]) b[j] = 1;
      if (a[j] < 0) ++n, --m;
    }
  h[0] = 1;
  for (int i = 1; i <= n; ++i) h[0] = h[0] * i % P;
  for (int i = 1; i <= m; ++i)
    h[i] = ((h[i - 1] + h[i - 2]) % P * (i - 1) + h[i - 1] * n) % P;
  printf("%d\n", h[m]);
  return 0;
}
