#include <bits/stdc++.h>
using namespace std;
int gi() {
  int res = 0, s = 1;
  char ch;
  for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-') s = -1, ch = getchar();
  for (; ch >= '0' && ch <= '9'; ch = getchar()) res = res * 10 + ch - 48;
  return res * s;
}
const int mo = 998244353;
int n, A[2005], B[2005], posa[2005], posb[2005];
int nex[2005], du[2005], vis[2005], c, cx, cy, dp[2005][2005], circ;
int dfs(int p) {
  vis[p] = 1;
  if (!nex[p]) return A[p];
  return dfs(nex[p]);
}
int main() {
  n = gi();
  for (int i = 1; i <= n; ++i) A[i] = gi(), posa[A[i]] = i;
  for (int i = 1; i <= n; ++i) B[i] = gi(), posb[B[i]] = i;
  for (int i = 1; i <= n; ++i)
    if (A[i] && posb[A[i]]) nex[i] = posb[A[i]], du[nex[i]]++;
  for (int i = 1; i <= n; ++i) {
    if (!du[i]) {
      int tmp = dfs(i);
      if (B[i] == 0 && tmp == 0)
        c++;
      else if (B[i] && tmp == 0)
        cy++;
      else if (B[i] == 0 && tmp)
        cx++;
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (vis[i]) continue;
    circ++;
    vis[i] = 1;
    for (int p = nex[i]; p; p = nex[p]) {
      if (vis[p]) break;
      vis[p] = 1;
    }
  }
  dp[0][0] = 1;
  for (int i = 1; i <= c + cx; ++i)
    for (int j = 1; j <= i; ++j)
      dp[i][j] =
          (dp[i - 1][j - 1] + (long long)dp[i - 1][j] * (i - 1) % mo) % mo;
  for (int i = c + cx + 1; i <= c + cx + cy; ++i)
    for (int j = 1; j <= i; ++j)
      dp[i][j] = (dp[i - 1][j - 1] +
                  (long long)dp[i - 1][j] * (c + i - c - cx - 1) % mo) %
                 mo;
  int jc = 1;
  for (int i = 1; i <= c; ++i) jc = (long long)jc * i % mo;
  for (int i = 0; i < n; ++i) {
    if (n - circ - i >= 0)
      printf("%I64d ", (long long)dp[c + cx + cy][n - circ - i] * jc % mo);
    else
      printf("0 ");
  }
  return 0;
}
