Problem p02728 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
const int N = 2e5 + 444;
int re() {
  int x;
  scanf("%lld", &x);
  return x;
}
int val[N];
int tot;
int ans[N];
int fir[N], nxt[N], to[N];
int siz[N];
int jie[N];
int c[N], d[N];
void addedge(int x, int y) {
  nxt[++tot] = fir[x];
  fir[x] = tot;
  to[tot] = y;
  swap(x, y);
  nxt[++tot] = fir[x];
  fir[x] = tot;
  to[tot] = y;
}
int quick(int a, int b) {
  int ha = 1;
  while (b) {
    if (b & 1)
      ha = ha * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return ha;
}
int ni(int a) { return quick(a, mod - 2); }
int cata(int n, int m) {
  if (n < m)
    return 0;
  return c[n] * d[m] % mod * d[n - m] % mod;
}
void Dfs1(int x, int fa) {
  val[x] = 1;
  siz[x] = 1;
  for (int e = fir[x]; e; e = nxt[e]) {
    int y = to[e];
    if (y == fa)
      continue;
    Dfs1(y, x);
    siz[x] += siz[y];
    val[x] = val[x] * val[y] % mod;
  }
  val[x] = val[x] * c[siz[x] - 1] % mod;
  for (int e = fir[x]; e; e = nxt[e]) {
    int y = to[e];
    if (y == fa)
      continue;
    val[x] = val[x] * d[siz[y]] % mod;
  }
}
int n;
void Dfs2(int x, int fa, int value) {
  if (x == 1) {
    ans[x] = val[x];
    value = value * d[siz[x] - 1] % mod * val[x] % mod;
    for (int e = fir[x]; e; e = nxt[e]) {
      int y = to[e];
      Dfs2(y, x,
           value * c[siz[y]] % mod * (c[siz[x] - 1 - siz[y]]) % mod *
               ni(val[y]) % mod);
    }
  } else {
    ans[x] = val[x] * d[siz[x] - 1] % mod * d[n - siz[x]] % mod * c[n - 1] %
             mod * value % mod;
    value = value * val[x] % mod * d[siz[x] - 1] % mod * d[n - siz[x]] % mod;
    for (int e = fir[x]; e; e = nxt[e]) {
      int y = to[e];
      if (y == fa)
        continue;
      Dfs2(y, x,
           value * c[siz[y]] % mod * c[n - siz[y] - 1] % mod * ni(val[y]) %
               mod);
    }
  }
}
signed main() {
  n = re();
  c[0] = 1;
  for (int i = 1; i <= n; i++)
    c[i] = c[i - 1] * i % mod;
  d[1] = 1;
  d[0] = 1;
  for (int i = 2; i <= n; i++)
    d[i] = d[mod % i] * (mod - mod / i) % mod;
  for (int i = 1; i <= n; i++)
    d[i] = d[i - 1] * d[i] % mod;
  for (int i = 1; i < n; i++) {
    int x = re(), y = re();
    addedge(x, y);
  }
  Dfs1(1, 0);
  Dfs2(1, 0, 1);
  for (int i = 1; i <= n; i++)
    printf("%lld\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
const int N = 1e6 + 444;
int re() {
  int x;
  scanf("%lld", &x);
  return x;
}
int val[N];
int tot;
int ans[N];
int fir[N], nxt[N], to[N];
int siz[N];
int jie[N];
int c[N], d[N];
void addedge(int x, int y) {
  nxt[++tot] = fir[x];
  fir[x] = tot;
  to[tot] = y;
  swap(x, y);
  nxt[++tot] = fir[x];
  fir[x] = tot;
  to[tot] = y;
}
int quick(int a, int b) {
  int ha = 1;
  while (b) {
    if (b & 1)
      ha = ha * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return ha;
}
int ni(int a) { return quick(a, mod - 2); }
int cata(int n, int m) {
  if (n < m)
    return 0;
  return c[n] * d[m] % mod * d[n - m] % mod;
}
void Dfs1(int x, int fa) {
  val[x] = 1;
  siz[x] = 1;
  for (int e = fir[x]; e; e = nxt[e]) {
    int y = to[e];
    if (y == fa)
      continue;
    Dfs1(y, x);
    siz[x] += siz[y];
    val[x] = val[x] * val[y] % mod;
  }
  val[x] = val[x] * c[siz[x] - 1] % mod;
  for (int e = fir[x]; e; e = nxt[e]) {
    int y = to[e];
    if (y == fa)
      continue;
    val[x] = val[x] * d[siz[y]] % mod;
  }
}
int n;
void Dfs2(int x, int fa, int value) {
  if (x == 1) {
    ans[x] = val[x];
    value = value * d[siz[x] - 1] % mod * val[x] % mod;
    for (int e = fir[x]; e; e = nxt[e]) {
      int y = to[e];
      Dfs2(y, x,
           value * c[siz[y]] % mod * (c[siz[x] - 1 - siz[y]]) % mod *
               ni(val[y]) % mod);
    }
  } else {
    ans[x] = val[x] * d[siz[x] - 1] % mod * d[n - siz[x]] % mod * c[n - 1] %
             mod * value % mod;
    value = value * val[x] % mod * d[siz[x] - 1] % mod * d[n - siz[x]] % mod;
    for (int e = fir[x]; e; e = nxt[e]) {
      int y = to[e];
      if (y == fa)
        continue;
      Dfs2(y, x,
           value * c[siz[y]] % mod * c[n - siz[y] - 1] % mod * ni(val[y]) %
               mod);
    }
  }
}
signed main() {
  n = re();
  c[0] = 1;
  for (int i = 1; i <= n; i++)
    c[i] = c[i - 1] * i % mod;
  d[1] = 1;
  d[0] = 1;
  for (int i = 2; i <= n; i++)
    d[i] = d[mod % i] * (mod - mod / i) % mod;
  for (int i = 1; i <= n; i++)
    d[i] = d[i - 1] * d[i] % mod;
  for (int i = 1; i < n; i++) {
    int x = re(), y = re();
    addedge(x, y);
  }
  Dfs1(1, 0);
  Dfs2(1, 0, 1);
  for (int i = 1; i <= n; i++)
    printf("%lld\n", ans[i]);
  return 0;
}