Problem p02728 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 100000 + 5, mod = 1e9 + 7;

int fpow(int base, int v) {
  int tot = 1;
  for (; v; v >>= 1, base = 1LL * base * base % mod)
    if (v & 1)
      tot = 1LL * tot * base % mod;
  return tot;
}

vector<int> G[N];
int n, dp[N], sz[N], fac[N], ifac[N], ans[N];

void dfs(int u, int v) {
  dp[u] = 1, sz[u] = 1;
  for (int i = 0; i < (signed)G[u].size(); ++i)
    if (G[u][i] != v) {
      dfs(G[u][i], u);
      sz[u] += sz[G[u][i]];
      dp[u] = 1LL * dp[u] * dp[G[u][i]] % mod * ifac[sz[G[u][i]]] % mod;
    }
  dp[u] = 1LL * dp[u] * fac[sz[u] - 1] % mod;
}

void dfs2(int u, int v) {
  if (u != 1)
    ans[u] = 1LL * dp[u] * ifac[sz[u] - 1] % mod * fac[n - 1] % mod * v % mod *
             ifac[n - sz[u]] % mod;
  int tmp =
      1LL * dp[u] * ifac[sz[u] - 1] % mod * v % mod * ifac[n - sz[u]] % mod;
  for (int i = 0; i < (signed)G[u].size(); ++i)
    if (sz[G[u][i]] < sz[u]) {
      dfs2(G[u][i], 1LL * tmp * fac[n - sz[G[u][i]] - 1] % mod *
                        fpow(dp[G[u][i]], mod - 2) % mod * fac[sz[G[u][i]]] %
                        mod);
    }
}

int main() {
  scanf("%d", &n);

  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1LL * fac[i - 1] * i % mod;
  ifac[n] = fpow(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; --i)
    ifac[i] = 1LL * ifac[i + 1] * (i + 1) % mod;

  for (int i = 1; i < n; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    G[a].push_back(b), G[b].push_back(a);
  }
  dfs(1, 0);
  ans[1] = dp[1];
  dfs2(1, 1);
  for (int i = 1; i <= n; ++i)
    printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int N = 200000 + 5, mod = 1e9 + 7;

int fpow(int base, int v) {
  int tot = 1;
  for (; v; v >>= 1, base = 1LL * base * base % mod)
    if (v & 1)
      tot = 1LL * tot * base % mod;
  return tot;
}

vector<int> G[N];
int n, dp[N], sz[N], fac[N], ifac[N], ans[N];

void dfs(int u, int v) {
  dp[u] = 1, sz[u] = 1;
  for (int i = 0; i < (signed)G[u].size(); ++i)
    if (G[u][i] != v) {
      dfs(G[u][i], u);
      sz[u] += sz[G[u][i]];
      dp[u] = 1LL * dp[u] * dp[G[u][i]] % mod * ifac[sz[G[u][i]]] % mod;
    }
  dp[u] = 1LL * dp[u] * fac[sz[u] - 1] % mod;
}

void dfs2(int u, int v) {
  if (u != 1)
    ans[u] = 1LL * dp[u] * ifac[sz[u] - 1] % mod * fac[n - 1] % mod * v % mod *
             ifac[n - sz[u]] % mod;
  int tmp =
      1LL * dp[u] * ifac[sz[u] - 1] % mod * v % mod * ifac[n - sz[u]] % mod;
  for (int i = 0; i < (signed)G[u].size(); ++i)
    if (sz[G[u][i]] < sz[u]) {
      dfs2(G[u][i], 1LL * tmp * fac[n - sz[G[u][i]] - 1] % mod *
                        fpow(dp[G[u][i]], mod - 2) % mod * fac[sz[G[u][i]]] %
                        mod);
    }
}

int main() {
  scanf("%d", &n);

  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1LL * fac[i - 1] * i % mod;
  ifac[n] = fpow(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; --i)
    ifac[i] = 1LL * ifac[i + 1] * (i + 1) % mod;

  for (int i = 1; i < n; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    G[a].push_back(b), G[b].push_back(a);
  }
  dfs(1, 0);
  ans[1] = dp[1];
  dfs2(1, 1);
  for (int i = 1; i <= n; ++i)
    printf("%d\n", ans[i]);
  return 0;
}