Problem p02728 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 2e5 + 54;
const long long MOD = 1000000007;
struct point {
  int from, to;
} eg[200010];
int front[N], num;
void lian(int x, int b) {
  eg[++num].to = b;
  eg[num].from = front[x];
  front[x] = num;
}
long long ksm(long long x, long long b) {
  long long maretu = 1;
  for (; b; b >>= 1) {
    if (b & 1) {
      maretu = maretu * x % MOD;
    }
    x = x * x % MOD;
  }
  return maretu;
}
int n, vis[N];
long long jc[N], ny[N], ru[N], ans[N], siz[N], gl[N];
long long dfs(int o, int fa) {
  gl[o] = 1;
  siz[o] = 1;
  for (int i = front[o]; i; i = eg[i].from) {
    int to = eg[i].to;
    if (to == fa)
      continue;
    gl[o] *= dfs(to, o);
    gl[o] %= MOD;
    siz[o] += siz[to];
  }
  // printf("%d %lld\n", o, (gl * ny[siz[o]]) % MOD);
  return (gl[o] * ny[siz[o]]) % MOD;
}
void check(int o) {
  // printf("%d\n", o);
  vis[o] = 1;
  ans[o] = (((jc[n] * gl[o]) % MOD) * ny[n] % MOD) % MOD;
  // printf("%lld\n", ans);
  for (int i = front[o]; i; i = eg[i].from) {
    int to = eg[i].to;
    if (vis[to] == 1)
      continue;
    long long ori_gl1, ori_gl2, ori_siz1, ori_siz2;
    ori_gl1 = gl[o];
    ori_gl2 = gl[to];
    ori_siz1 = siz[o];
    ori_siz2 = siz[to];
    gl[o] = (gl[o] * ksm((gl[to] * ny[siz[to]]) % MOD, MOD - 2)) % MOD;
    siz[o] = n - siz[to];
    gl[to] = gl[to] * ((gl[o] * ny[siz[o]]) % MOD) % MOD;
    siz[to] = n;
    check(to);
    gl[o] = ori_gl1;
    gl[to] = ori_gl2;
    siz[o] = ori_siz1;
    siz[to] = ori_siz2;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, b;
    scanf("%d%d", &x, &b);
    ++ru[x];
    ++ru[b];
    lian(x, b);
    lian(b, x);
  }
  jc[0] = ny[0] = 1;
  for (long long i = 1; i <= n; i++) {
    jc[i] = jc[i - 1] * i % MOD;
    ny[i] = ksm(i, MOD - 2);
  }
  dfs(1, 0);
  check(1);
  for (int i = 1; i <= n; ++i)
    std::cout << ans[i] << '\n';
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 2e5 + 54;
const long long MOD = 1000000007;
struct point {
  int from, to;
} eg[N << 1];
int front[N], num;
void lian(int x, int b) {
  eg[++num].to = b;
  eg[num].from = front[x];
  front[x] = num;
}
long long ksm(long long x, long long b) {
  long long maretu = 1;
  for (; b; b >>= 1) {
    if (b & 1) {
      maretu = maretu * x % MOD;
    }
    x = x * x % MOD;
  }
  return maretu;
}
int n, vis[N];
long long jc[N], ny[N], ru[N], ans[N], siz[N], gl[N];
long long dfs(int o, int fa) {
  gl[o] = 1;
  siz[o] = 1;
  for (int i = front[o]; i; i = eg[i].from) {
    int to = eg[i].to;
    if (to == fa)
      continue;
    gl[o] *= dfs(to, o);
    gl[o] %= MOD;
    siz[o] += siz[to];
  }
  // printf("%d %lld\n", o, (gl * ny[siz[o]]) % MOD);
  return (gl[o] * ny[siz[o]]) % MOD;
}
void check(int o) {
  // printf("%d\n", o);
  vis[o] = 1;
  ans[o] = (((jc[n] * gl[o]) % MOD) * ny[n] % MOD) % MOD;
  // printf("%lld\n", ans);
  for (int i = front[o]; i; i = eg[i].from) {
    int to = eg[i].to;
    if (vis[to] == 1)
      continue;
    long long ori_gl1, ori_gl2, ori_siz1, ori_siz2;
    ori_gl1 = gl[o];
    ori_gl2 = gl[to];
    ori_siz1 = siz[o];
    ori_siz2 = siz[to];
    gl[o] = (gl[o] * ksm((gl[to] * ny[siz[to]]) % MOD, MOD - 2)) % MOD;
    siz[o] = n - siz[to];
    gl[to] = gl[to] * ((gl[o] * ny[siz[o]]) % MOD) % MOD;
    siz[to] = n;
    check(to);
    gl[o] = ori_gl1;
    gl[to] = ori_gl2;
    siz[o] = ori_siz1;
    siz[to] = ori_siz2;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int x, b;
    scanf("%d%d", &x, &b);
    ++ru[x];
    ++ru[b];
    lian(x, b);
    lian(b, x);
  }
  jc[0] = ny[0] = 1;
  for (long long i = 1; i <= n; i++) {
    jc[i] = jc[i - 1] * i % MOD;
    ny[i] = ksm(i, MOD - 2);
  }
  dfs(1, 0);
  check(1);
  for (int i = 1; i <= n; ++i)
    std::cout << ans[i] << '\n';
}