Problem p03991 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 200000 + 10;
const int MOD = 924844033;
const int root = 44009197;    // root ^ (2^21) % MOD = 1
const int root_1 = 921713838; // root * root_1 = 1
const int root_pw = 1 << 21;  // 998244353 = 119 * 2^23 + 1
LL mpow(LL a, LL x) {
  if (x == 0)
    return 1;
  LL t = mpow(a, x >> 1);
  if (x % 2 == 0)
    return t * t % MOD;
  return t * t % MOD * a % MOD;
}
LL modinv(LL a) {
  LL x0 = 0, x1 = 1, r0 = MOD, r1 = a;
  while (r1) {
    LL q = r0 / r1;
    x0 -= q * x1;
    swap(x0, x1);
    r0 -= q * r1;
    swap(r0, r1);
  }
  return x0 < 0 ? x0 + MOD : x0;
}
void fft(vector<int> &a, bool inv) { // a.size()不为2的幂会原地爆炸。
  int n = (int)a.size();
  // 迷 の butterfly操作
  for (int i = 1, j = 0; i < n; i++) {
    int bit = n >> 1;
    for (; j >= bit; bit >>= 1)
      j -= bit;
    j += bit;
    if (i < j)
      swap(a[i], a[j]);
  }
  // 施展 DFT
  for (int len = 2; len <= n; len <<= 1) {
    int wlen = inv ? root_1 : root;
    for (int i = len; i < root_pw; i <<= 1)
      wlen = wlen * 1LL * wlen % MOD;
    for (int i = 0; i < n; i += len) {
      int w = 1;
      for (int j = 0; j < len / 2; j++) {
        int u = a[i + j], v = int(a[i + j + len / 2] * 1LL * w % MOD);
        a[i + j] = (u + v < MOD) ? u + v : u + v - MOD;
        a[i + j + len / 2] = (u - v >= 0) ? u - v : u - v + MOD;
        w = w * 1LL * wlen % MOD;
      }
    }
  }
  if (inv) {
    int nrev = modinv(n);
    for (int i = 0; i < n; i++)
      a[i] = a[i] * 1LL * nrev % MOD;
  }
}
void pro(const vector<int> &a, const vector<int> &b, vector<int> &res) {
  vector<int> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  int n = 1;
  while (n < (int)max(a.size(), b.size()))
    n <<= 1;
  n <<= 1;
  fa.resize(n), fb.resize(n);
  fft(fa, false), fft(fb, false);
  for (int i = 0; i < n; i++)
    fa[i] = 1LL * fa[i] * fb[i] % MOD;
  fft(fa, true);
  res = fa;
}
LL inv[N], fac[N];
void init() {
  inv[1] = 1;
  for (int i = 2; i < N; i++) {
    inv[i] = 1LL * (MOD - (MOD / i)) * inv[MOD % i] % MOD;
  }
  inv[0] = fac[0] = 1;
  for (int i = 1; i < N; i++) {
    inv[i] = inv[i - 1] * inv[i] % MOD;
    fac[i] = fac[i - 1] * i % MOD;
  }
}

vector<int> g[N];
int n, sz[N];
void dfs(int u, int p) {
  sz[u] = 1;
  for (auto v : g[u]) {
    if (v == p)
      continue;
    dfs(v, u);
    sz[u] += sz[v];
  }
}
int c[N];
int comb(int x, int y) {
  return 1LL * fac[x] * inv[y] % MOD * inv[x - y] % MOD;
}
int ans[N];
void getAns(vector<int> vec) {
  memset(c, 0, sizeof(c));
  for (auto x : vec)
    c[x]++;
  vector<int> res;
  vector<int> p1, p2, p;
  p1.push_back(0);
  for (int i = 1; i < n; i++) {
    p1.push_back(1LL * c[i] * fac[i] % MOD);
  }
  for (int i = 0; i <= n; i++) {
    p2.push_back(inv[i]);
  }
  reverse(p2.begin(), p2.end());
  pro(p1, p2, p);
  for (int i = p2.size(); i < p.size(); i++) {
    p[i] = p[i] * inv[i - (int)p2.size() + 1] % MOD;
    ans[i - (int)p2.size()] -= p[i];
    ans[i - (int)p2.size()] = ((ans[i - (int)p2.size()] % MOD) + MOD) % MOD;
  }
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 1);
  for (int i = 0; i < n; i++) {
    ans[i] = 1LL * comb(n, i + 1) * n % MOD;
  }
  vector<int> v;
  for (int i = 1; i <= n; i++)
    v.push_back(sz[i]);
  getAns(v);
  v.clear();
  for (int i = 1; i <= n; i++)
    v.push_back(n - sz[i]);
  getAns(v);
  for (int i = 0; i < n; i++)
    printf("%d\n", ans[i]);
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 400000 + 10;
const int MOD = 924844033;
const int root = 44009197;    // root ^ (2^21) % MOD = 1
const int root_1 = 921713838; // root * root_1 = 1
const int root_pw = 1 << 21;  // 998244353 = 119 * 2^23 + 1
LL mpow(LL a, LL x) {
  if (x == 0)
    return 1;
  LL t = mpow(a, x >> 1);
  if (x % 2 == 0)
    return t * t % MOD;
  return t * t % MOD * a % MOD;
}
LL modinv(LL a) {
  LL x0 = 0, x1 = 1, r0 = MOD, r1 = a;
  while (r1) {
    LL q = r0 / r1;
    x0 -= q * x1;
    swap(x0, x1);
    r0 -= q * r1;
    swap(r0, r1);
  }
  return x0 < 0 ? x0 + MOD : x0;
}
void fft(vector<int> &a, bool inv) { // a.size()不为2的幂会原地爆炸。
  int n = (int)a.size();
  // 迷 の butterfly操作
  for (int i = 1, j = 0; i < n; i++) {
    int bit = n >> 1;
    for (; j >= bit; bit >>= 1)
      j -= bit;
    j += bit;
    if (i < j)
      swap(a[i], a[j]);
  }
  // 施展 DFT
  for (int len = 2; len <= n; len <<= 1) {
    int wlen = inv ? root_1 : root;
    for (int i = len; i < root_pw; i <<= 1)
      wlen = wlen * 1LL * wlen % MOD;
    for (int i = 0; i < n; i += len) {
      int w = 1;
      for (int j = 0; j < len / 2; j++) {
        int u = a[i + j], v = int(a[i + j + len / 2] * 1LL * w % MOD);
        a[i + j] = (u + v < MOD) ? u + v : u + v - MOD;
        a[i + j + len / 2] = (u - v >= 0) ? u - v : u - v + MOD;
        w = w * 1LL * wlen % MOD;
      }
    }
  }
  if (inv) {
    int nrev = modinv(n);
    for (int i = 0; i < n; i++)
      a[i] = a[i] * 1LL * nrev % MOD;
  }
}
void pro(const vector<int> &a, const vector<int> &b, vector<int> &res) {
  vector<int> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  int n = 1;
  while (n < (int)max(a.size(), b.size()))
    n <<= 1;
  n <<= 1;
  fa.resize(n), fb.resize(n);
  fft(fa, false), fft(fb, false);
  for (int i = 0; i < n; i++)
    fa[i] = 1LL * fa[i] * fb[i] % MOD;
  fft(fa, true);
  res = fa;
}
LL inv[N], fac[N];
void init() {
  inv[1] = 1;
  for (int i = 2; i < N; i++) {
    inv[i] = 1LL * (MOD - (MOD / i)) * inv[MOD % i] % MOD;
  }
  inv[0] = fac[0] = 1;
  for (int i = 1; i < N; i++) {
    inv[i] = inv[i - 1] * inv[i] % MOD;
    fac[i] = fac[i - 1] * i % MOD;
  }
}

vector<int> g[N];
int n, sz[N];
void dfs(int u, int p) {
  sz[u] = 1;
  for (auto v : g[u]) {
    if (v == p)
      continue;
    dfs(v, u);
    sz[u] += sz[v];
  }
}
int c[N];
int comb(int x, int y) {
  return 1LL * fac[x] * inv[y] % MOD * inv[x - y] % MOD;
}
int ans[N];
void getAns(vector<int> vec) {
  memset(c, 0, sizeof(c));
  for (auto x : vec)
    c[x]++;
  vector<int> res;
  vector<int> p1, p2, p;
  p1.push_back(0);
  for (int i = 1; i < n; i++) {
    p1.push_back(1LL * c[i] * fac[i] % MOD);
  }
  for (int i = 0; i <= n; i++) {
    p2.push_back(inv[i]);
  }
  reverse(p2.begin(), p2.end());
  pro(p1, p2, p);
  for (int i = p2.size(); i < p.size(); i++) {
    p[i] = p[i] * inv[i - (int)p2.size() + 1] % MOD;
    ans[i - (int)p2.size()] -= p[i];
    ans[i - (int)p2.size()] = ((ans[i - (int)p2.size()] % MOD) + MOD) % MOD;
  }
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 1);
  for (int i = 0; i < n; i++) {
    ans[i] = 1LL * comb(n, i + 1) * n % MOD;
  }
  vector<int> v;
  for (int i = 1; i <= n; i++)
    v.push_back(sz[i]);
  getAns(v);
  v.clear();
  for (int i = 1; i <= n; i++)
    v.push_back(n - sz[i]);
  getAns(v);
  for (int i = 0; i < n; i++)
    printf("%d\n", ans[i]);
}