Problem p02728 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

struct Combination {
  const int MOD = 1e9 + 7;

  vector<int> fact; // 　fact[i] = iの階乗
  void init(int n) {
    fact.resize(n + 1);
    fact[0] = fact[1] = 1;
    for (int i = 2; i <= n; i++) {
      fact[i] = i * fact[i - 1] % MOD;
    }
  }

  int nCr(int n, int r) { // nCr = n!/r!(n-r)!
    if (n < r)
      return 0;
    return fact[n] * mod_pow(fact[r] * fact[n - r] % MOD, MOD - 2) % MOD;
  }

  int mod_pow(int n, int p) { // a/n ≡ a*n^(p-2) nとpは互いに素
    int r = 1;
    for (; p > 0; p >>= 1) {
      if (p & 1LL)
        r = (r * n) % MOD;
      n = (n * n) % MOD;
    }
    return r; // r = n^p % MOD
  }

} comb;
const int MOD = 1e9 + 7;

signed main() {

  int n;
  cin >> n;
  vector<vector<int>> G(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  comb.init(2e6);
  vector<int> num(n, 0), dp(n, 1);
  auto dfs1 = [&](auto &&dfs1, int u, int p) -> void {
    for (int v : G[u]) {
      if (v == p)
        continue;
      dfs1(dfs1, v, u);
      num[u] += num[v];
      dp[u] = dp[u] * dp[v] % MOD * comb.nCr(num[u], num[v]) % MOD;
    }
    num[u]++;
  };
  dfs1(dfs1, 0, -1);

  vector<int> ans(n);
  auto dfs2 = [&](auto &&dfs2, int u, int p, int res) -> void {
    ans[u] = dp[u] * res % MOD * comb.nCr(n - 1, n - num[u]) % MOD;
    for (int v : G[u]) {
      if (v == p)
        continue;
      res = ans[u] * comb.mod_pow(dp[v], MOD - 2) % MOD *
            comb.mod_pow(comb.nCr(n - 1, num[v]), MOD - 2) % MOD;
      dfs2(dfs2, v, u, res);
    }
  };
  dfs2(dfs2, 0, -1, 1);

  for (int p : ans) {
    cout << p << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif
#define int long long

struct Combination {
  const int MOD = 1e9 + 7;

  vector<int> fact; // 　fact[i] = iの階乗
  void init(int n) {
    fact.resize(n + 1);
    fact[0] = fact[1] = 1;
    for (int i = 2; i <= n; i++) {
      fact[i] = i * fact[i - 1] % MOD;
    }
  }

  int nCr(int n, int r) { // nCr = n!/r!(n-r)!
    if (n < r)
      return 0;
    return fact[n] * mod_pow(fact[r] * fact[n - r] % MOD, MOD - 2) % MOD;
  }

  int mod_pow(int n, int p) { // a/n ≡ a*n^(p-2) nとpは互いに素
    int r = 1;
    for (; p > 0; p >>= 1) {
      if (p & 1LL)
        r = (r * n) % MOD;
      n = (n * n) % MOD;
    }
    return r; // r = n^p % MOD
  }

} comb;
const int MOD = 1e9 + 7;

signed main() {

  int n;
  cin >> n;
  vector<vector<int>> G(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  comb.init(1e5);
  vector<int> num(n, 0), dp(n, 1);
  auto dfs1 = [&](auto &&dfs1, int u, int p) -> void {
    for (int v : G[u]) {
      if (v == p)
        continue;
      dfs1(dfs1, v, u);
      num[u] += num[v];
      dp[u] = dp[u] * dp[v] * comb.nCr(num[u], num[v]) % MOD;
    }
    num[u]++;
  };
  dfs1(dfs1, 0, -1);

  vector<int> ans(n);
  auto dfs2 = [&](auto &&dfs2, int u, int p, int res) -> void {
    ans[u] = dp[u] * res * comb.nCr(n - 1, n - num[u]) % MOD;
    for (int v : G[u]) {
      if (v == p)
        continue;
      res = ans[u] * comb.mod_pow(dp[v], MOD - 2) % MOD *
            comb.mod_pow(comb.nCr(n - 1, num[v]), MOD - 2) % MOD;
      dfs2(dfs2, v, u, res);
    }
  };
  dfs2(dfs2, 0, -1, 1);

  for (int p : ans) {
    cout << p << endl;
  }

  return 0;
}