#include <bits/stdc++.h>
const int maxn = 2000 + 10;
const int mod = 998244353;
int to[maxn];
int a[maxn];
int b[maxn];
int wha[maxn];
int whb[maxn];
int dp[maxn];
int bj[maxn];
int vis[maxn];
int ans[maxn];
int Dfs(int x) {
  vis[x] = 1;
  if (to[x]) return Dfs(to[x]);
  return x;
}
void Dfs2(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  Dfs2(to[x]);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i]) wha[a[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (b[i]) whb[b[i]] = i;
  }
  int tmp = n;
  for (int i = 1; i <= n; i++) {
    if (wha[i] || whb[i]) tmp--;
    if (wha[i] && whb[i]) {
      to[wha[i]] = whb[i];
      bj[whb[i]] = 1;
    }
  }
  int a0 = 0;
  int w1 = 0;
  int t1 = 0;
  int a1 = 0;
  for (int i = 1; i <= n; i++)
    if (!bj[i]) {
      int tmp = Dfs(i);
      if (b[i] == 0 && a[tmp] == 0)
        a0++;
      else if (b[i] == 0 && a[tmp])
        w1++;
      else if (b[i] && a[tmp] == 0)
        t1++;
      else
        a1++;
    }
  int yha = 0;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      yha++;
      Dfs2(i);
    }
  dp[0] = 1;
  for (int i = 1; i <= a0; i++) {
    for (int j = n; j >= 0; j--) {
      if (j)
        dp[j] = (1ll * dp[j] * (i - 1) + dp[j - 1]) % mod;
      else
        dp[j] = 1ll * dp[j] * (i - 1) % mod;
    }
  }
  for (int i = 1; i <= w1; i++) {
    for (int j = n; j >= 0; j--) {
      if (j)
        dp[j] = (1ll * dp[j] * (a0 + i - 1) + dp[j - 1]) % mod;
      else
        dp[j] = 1ll * dp[j] * (a0 + i - 1) % mod;
    }
  }
  for (int i = 1; i <= t1; i++) {
    for (int j = n; j >= 0; j--) {
      if (j)
        dp[j] = (1ll * dp[j] * (a0 + i - 1) + dp[j - 1]) % mod;
      else
        dp[j] = 1ll * dp[j] * (a0 + i - 1) % mod;
    }
  }
  for (int i = 1; i <= a1; i++)
    for (int j = n; j >= 0; j--) dp[j] = 1ll * dp[j] * (a0 - (i - 1)) % mod;
  int jc = 1;
  for (int i = 1; i <= tmp; i++) jc = 1ll * jc * i % mod;
  for (int i = 1; i <= n; i++) ans[n - i] = i >= yha ? dp[i - yha] : 0;
  for (int i = 0; i <= n - 1; i++) printf("%lld ", 1ll * ans[i] * jc % mod);
  return 0;
}
