#include <bits/stdc++.h>
using namespace std;
const int N = 2005, mod = 998244353;
int fexp(int a, int b) {
  int x = 1;
  for (; b; b >>= 1, a = 1ll * a * a % mod)
    if (b & 1) x = 1ll * x * a % mod;
  return x;
}
int n, a, b, c, d, px[N], py[N], to[N], du[N], vis[N];
vector<int> start;
int fac[N], ifac[N], s1[N][N], f[N], g[N], h[N];
int C(int a, int b) {
  if (a < b || a < 0 || b < 0) return 0;
  return 1ll * fac[a] * ifac[b] % mod * ifac[a - b] % mod;
}
int D(int a, int b) {
  if (a < b || a < 0 || b < 0) return 0;
  return 1ll * fac[a] * ifac[a - b] % mod;
}
void work() {
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod;
  ifac[n] = fexp(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; --i) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod;
  s1[0][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= i; ++j)
      s1[i][j] = (1ll * (i - 1) * s1[i - 1][j] + s1[i - 1][j - 1]) % mod;
  if (c) {
    for (int j = 0; j <= a; ++j)
      for (int i = j; i <= a; ++i)
        f[j] =
            (f[j] + 1ll * C(a, i) * s1[i][j] % mod * D(a - i + c - 1, a - i)) %
            mod;
    for (int j = 0; j <= b; ++j)
      for (int i = j; i <= b; ++i)
        g[j] =
            (g[j] + 1ll * C(b, i) * s1[i][j] % mod * D(b - i + c - 1, b - i)) %
            mod;
    for (int j = 0; j <= c; ++j) h[j] = 1ll * s1[c][j] * fac[c] % mod;
  } else {
    for (int j = 0; j <= a; ++j) f[j] = s1[a][j];
    for (int j = 0; j <= b; ++j) g[j] = s1[b][j];
    h[0] = 1;
  }
  for (int i = n; i >= 0; --i) {
    int sum = 0;
    for (int j = 0; j <= b; ++j) sum = (sum + 1ll * g[j] * f[i - j]) % mod;
    f[i] = sum;
  }
  for (int i = n; i >= 0; --i) {
    int sum = 0;
    for (int j = 0; j <= c; ++j) sum = (sum + 1ll * h[j] * f[i - j]) % mod;
    f[i] = sum;
  }
  for (int i = 0; i < n; ++i) printf("%d ", n - i - d >= 0 ? f[n - i - d] : 0);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", px + i);
  fill(to, to + n + 1, -1);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", py + i);
    if (!px[i] && py[i]) start.push_back(py[i]);
    if (px[i]) to[px[i]] = py[i], ++du[py[i]];
    if (!px[i] && !py[i]) ++c;
  }
  for (auto &s : start)
    if (!vis[s]) {
      int x = s;
      for (; x > 0; x = to[x]) vis[x] = 1;
      if (x == -1)
        ++a;
      else
        ++c;
    }
  for (int s = 1; s <= n; ++s)
    if (!vis[s] && !du[s]) {
      int x = s;
      for (; x > 0 && !vis[x]; x = to[x]) vis[x] = 1;
      if (!x) ++b;
    }
  for (int s = 1; s <= n; ++s)
    if (!vis[s] && du[s]) {
      int x = s;
      for (; x > 0 && !vis[x]; x = to[x]) vis[x] = 1;
      assert(x == s);
      ++d;
    }
  work();
  return 0;
}
