#include <bits/stdc++.h>
template <class T>
T read() {
  T x = 0, w = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') w = -w;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * w;
}
template <class T>
inline T read(T& x) {
  return x = read<T>();
}
using namespace std;
const int mod = 998244353;
inline int add(int a, int b) { return (a += b) >= mod ? a - mod : a; }
inline int mul(int a, int b) { return (long long)a * b % mod; }
inline int fpow(int a, int b) {
  int ans = 1;
  for (; b; b >>= 1, a = mul(a, a))
    if (b & 1) ans = mul(ans, a);
  return ans;
}
const int N = 2001;
int fac[N], ifac[N];
int C[N][N], S[N][N];
int n, a[N], b[N], ia[N], ib[N];
int cnt[3], tag[N], cir;
void dfs(int p, int fa) {
  tag[p] = 1;
  if (!b[p]) {
    if (!fa)
      ++cnt[2];
    else
      ++cnt[1];
    return;
  }
  if (ia[b[p]]) {
    if (tag[ia[b[p]]])
      ++cir;
    else
      dfs(ia[b[p]], fa);
  } else if (!fa)
    ++cnt[0];
}
int f[N], g[N], h[N], ans[N];
int main() {
  read(n);
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = mul(fac[i - 1], i);
  ifac[n] = fpow(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; --i) ifac[i] = mul(ifac[i + 1], i + 1);
  for (int i = 0; i <= n; ++i) {
    C[i][0] = C[i][i] = 1;
    for (int j = 1; j < i; ++j) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]);
  }
  S[0][0] = 1;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= i; ++j)
      S[i][j] = add(S[i - 1][j - 1], mul(i - 1, S[i - 1][j]));
  for (int i = 1; i <= n; ++i)
    if (read(a[i])) ia[a[i]] = i;
  for (int i = 1; i <= n; ++i)
    if (read(b[i])) ib[b[i]] = i;
  for (int i = 1; i <= n; ++i) {
    if (a[i] && ib[a[i]]) continue;
    dfs(i, a[i]);
  }
  for (int i = 1; i <= n; ++i)
    if (!tag[i]) dfs(i, a[i]);
  for (int i = 0; i <= cnt[0]; ++i)
    for (int j = i; j <= cnt[0]; ++j)
      f[i] = add(
          f[i], mul(C[cnt[0]][j],
                    mul(S[j][i], mul(fac[cnt[0] - j + cnt[2]], ifac[cnt[2]]))));
  for (int i = cnt[0]; i >= 0; --i)
    for (int j = i + 1; j <= cnt[0]; ++j)
      f[i] = add(f[i], mod - mul(C[j][i], f[j]));
  for (int i = 0; i <= cnt[1]; ++i)
    for (int j = i; j <= cnt[1]; ++j)
      g[i] = add(
          g[i], mul(C[cnt[1]][j],
                    mul(S[j][i], mul(fac[cnt[1] - j + cnt[2]], ifac[cnt[2]]))));
  for (int i = cnt[1]; i >= 0; --i)
    for (int j = i + 1; j <= cnt[1]; ++j)
      g[i] = add(g[i], mod - mul(C[j][i], g[j]));
  for (int i = 0; i <= n; ++i)
    for (int j = 0; j <= i; ++j) h[i] = add(h[i], mul(f[j], g[i - j]));
  for (int i = 0; i <= n; ++i)
    for (int j = 0; j <= i; ++j)
      ans[i] = add(ans[i], mul(h[j], mul(S[cnt[2]][i - j], fac[cnt[2]])));
  for (int i = n; i >= 1; --i) {
    if (i >= cir)
      ans[i] = ans[i - cir];
    else
      ans[i] = 0;
  }
  for (int i = n; i >= 1; --i) printf("%d ", ans[i]);
  return 0;
}
