#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const long long Inf = 1e10;
const int P = 998244353;
const int N = 100005;
inline long long IN() {
  long long x = 0;
  int ch = 0, f = 0;
  for (ch = getchar(); ch != -1 && (ch < 48 || ch > 57); ch = getchar())
    f = (ch == '-');
  for (; ch >= 48 && ch <= 57; ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  return f ? (-x) : x;
}
template <typename T>
inline int chkmin(T &a, const T &b) {
  if (b < a) return a = b, 1;
  return 0;
}
template <typename T>
inline int chkmax(T &a, const T &b) {
  if (b > a) return a = b, 1;
  return 0;
}
void renew(int &x, const int &y) {
  x += y;
  if (x >= P) x -= P;
  if (x < 0) x += P;
}
int Pow(int x, int y, int p) {
  int a = 1;
  for (; y; y >>= 1, x = (long long)x * x % p)
    if (y & 1) a = (long long)a * x % p;
  return a;
}
int dd[N];
int vis[N];
int a[N], b[N], sp;
vector<int> e[N], seq;
int n, m, cc;
int ans[2055];
int f[2055][2055];
int v[2055], vc[2055];
int ct[2055];
void dfs(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  seq.push_back(x);
  for (int i = 0; i < e[x].size(); i++) dfs(e[x][i]);
}
int main() {
  scanf("%d", &n);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", a + i);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", b + i);
  for (int i = (int)(1); i <= (int)(n); i++) {
    if (a[i] && !b[i]) {
      v[a[i]] |= 1;
    }
    if (!a[i] && b[i]) {
      v[b[i]] |= 2;
    }
    if (a[i] && b[i]) {
      e[a[i]].push_back(b[i]);
      dd[b[i]] = 1;
    }
    if (!a[i] && !b[i]) ++sp;
  }
  for (int i = (int)(1); i <= (int)(n); i++)
    if (!vis[i] && !dd[i]) {
      seq.clear();
      dfs(i);
      m++;
      if (v[seq.back()] & 1) vc[m] |= 1;
      if (v[seq[0]] & 2) vc[m] |= 2;
      ct[vc[m]]++;
    }
  for (int i = (int)(1); i <= (int)(n); i++)
    if (!vis[i]) {
      dfs(i);
      cc++;
    }
  f[0][0] = 1;
  for (int i = (int)(1); i <= (int)(ct[0]); i++)
    for (int j = (int)(0); j <= (int)(i); j++) {
      renew(f[i][j + 1], f[i - 1][j]);
      renew(f[i][j], 1LL * f[i - 1][j] * (i - 1) % P);
    }
  for (int i = (int)(ct[0] + 1); i <= (int)(ct[1] + ct[0]); i++)
    for (int j = (int)(0); j <= (int)(i); j++) {
      renew(f[i][j + 1], f[i - 1][j]);
      renew(f[i][j], 1LL * f[i - 1][j] * (i - 1) % P);
    }
  for (int i = (int)(ct[0] + ct[1] + 1); i <= (int)(ct[0] + ct[1] + ct[2]); i++)
    for (int j = (int)(0); j <= (int)(i); j++) {
      renew(f[i][j + 1], f[i - 1][j]);
      renew(f[i][j], 1LL * f[i - 1][j] * (i - 1 - ct[1]) % P);
    }
  int sum = ct[0] + ct[1] + ct[2];
  for (int i = (int)(ct[0] + ct[1] + ct[2] + 1);
       i <= (int)(ct[0] + ct[1] + ct[2] + ct[3]); i++)
    for (int j = (int)(0); j <= (int)(i); j++) {
      renew(f[i][j], 1LL * f[i - 1][j] *
                         max(0, (i - 1 - (i - sum - 1) * 2 - ct[1] - ct[2])) %
                         P);
    }
  for (int i = (int)(max(0, 1 - cc)); i <= (int)(n - cc); i++)
    ans[n - (i + cc)] = f[m][i];
  for (int i = (int)(0); i < (int)(n); i++)
    for (int j = (int)(1); j <= (int)(sp); j++) ans[i] = 1LL * ans[i] * j % P;
  for (int i = (int)(0); i < (int)(n); i++) printf("%d ", ans[i]);
  puts("");
  return 0;
}
