using s64 = longlong; using u64 = unsignedlonglong;
/* 取 min */ template <classT> inlinevoidchmin(T &x, const T &y){ if (x > y) { x = y; } } /* 取 max */ template <classT> inlinevoidchmax(T &x, const T &y){ if (x < y) { x = y; } }
/* ----- ----- ----- 正文 ----- ----- ----- */
constint mod = 1e9 + 7; // 模数需根据实际问题调整
/* 模意义下 加法 */ inlinevoidadd(int &x, constint &y){ x += y; if (x >= mod) { x -= mod; } } /* 模意义下 减法 */ inlinevoiddec(int &x, constint &y){ x -= y; if (x < 0) { x += mod; } } /* 模意义下 取反 */ inlinevoidneg(int &x){ if (x) { x = mod - x; } } /* 模意义下 乘法 */ inlinevoidmul(int &x, constint &y){ x = 1ll * x * y % mod; }
/* 模意义下 修正 */ inlineintnorm(int x){ x %= mod; return x < 0 ? x + mod : x; }
/* 快速幂 */ constexprintqpow(int a, int b, int p){ int ans = 1; for (; b; b >>= 1) { if (b & 1) ans = 1ll * ans * a % p; a = 1ll * a * a % p; } return ans; }
constint N = 400100;
int n; int a[N];
int f[N];
voidwork(){ std::cin >> n;
for (int i = 0; i <= n; i ++) { std::cin >> a[i]; } for (int i = 1; i <= n; i ++) { if (a[i] > n) { std::cout << 0 << '\n'; return; } if (a[i] == i || a[i] == 0 || a[i] == -1) { continue; } if (a[a[i]] == -1) { a[a[i]] = i; } elseif (a[a[i]] != i) { std::cout << 0 << '\n'; return; } }
int cnt = 0; for (int i = 1; i <= n; i ++) { if (a[i] == -1) { cnt ++; } }
f[0] = 1, f[1] = 2; for (int i = 2; i <= n; i ++) { f[i] = (2LL * f[i - 1] + 1LL * (i - 1) * f[i - 2]) % mod; }
int ans = 0; if (a[n] == -1) { ans = cnt ? (f[cnt - 1] + 1LL * (cnt - 1) * (cnt >= 2 ? f[cnt - 2] : 0)) % mod : 1; } else { ans = f[cnt]; } std::cout << ans << '\n'; }