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

#include <bits/stdc++.h>

using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1LL << 60;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

const int MAX = 510000;
const ll MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

vector<int> G[200100];

pair<int, ll> info_low[200200];
int N;
ll ans[200200];

pair<int, ll> dfs1(int v, int par) {

  int cnt = 1;

  ll inv_fac = 1;
  ll tmp_ans = 1;
  for (auto nv : G[v]) {
    if (nv == par)
      continue;

    pair<int, ll> tmp_ret = dfs1(nv, v);

    cnt += tmp_ret.first;
    inv_fac = (inv_fac * finv[tmp_ret.first]) % MOD;
    tmp_ans = (tmp_ans * tmp_ret.second) % MOD;
  }

  ll ret;
  ret = ((fac[cnt - 1] * tmp_ans) % MOD * inv_fac) % MOD;

  return info_low[v] = make_pair(cnt, ret);
}
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

void dfs2(int v, int par) {

  if (par == -1) {
    ans[v] = info_low[v].second;
  } else {
    ll ans_par = ans[par];

    ans_par = (ans_par * fac[info_low[v].first]) % MOD;
    ans_par = (ans_par * finv[info_low[v].first - 1]) % MOD;
    ans_par = (ans_par * fac[N - info_low[v].first - 1]) % MOD;
    ans_par = (ans_par * finv[N - info_low[v].first]) % MOD;
    ans[v] = ans_par;
  }

  for (auto nv : G[v]) {
    if (nv == par)
      continue;
    dfs2(nv, v);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  COMinit();

  cin >> N;

  int a, b;
  rep(i, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  dfs1(0, -1);
  dfs2(0, -1);

  rep(i, N) { cout << ans[i] << endl; }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1LL << 60;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

const int MAX = 510000;
const ll MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

vector<int> G[200100];

pair<int, ll> info_low[200];
int N;
ll ans[200100];

pair<int, ll> dfs1(int v, int par) {

  int cnt = 1;

  ll inv_fac = 1;
  ll tmp_ans = 1;
  for (auto nv : G[v]) {
    if (nv == par)
      continue;

    pair<int, ll> tmp_ret = dfs1(nv, v);

    cnt += tmp_ret.first;
    inv_fac = (inv_fac * finv[tmp_ret.first]) % MOD;
    tmp_ans = (tmp_ans * tmp_ret.second) % MOD;
  }

  ll ret;
  ret = ((fac[cnt - 1] * tmp_ans) % MOD * inv_fac) % MOD;

  return info_low[v] = make_pair(cnt, ret);
}

void dfs2(int v, int par) {

  if (par == -1) {
    ans[v] = info_low[v].second;
  } else {
    ll ans_par = ans[par];

    ans_par = (ans_par * fac[info_low[v].first]) % MOD;
    ans_par = (ans_par * info_low[v].second) % MOD;
    ans_par = (ans_par * finv[info_low[v].first - 1]) % MOD;

    ans[v] = ans_par;
  }

  for (auto nv : G[v]) {

    if (nv == par)
      continue;

    dfs2(nv, v);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  COMinit();

  cin >> N;

  int a, b;
  rep(i, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  dfs1(0, -1);
  dfs2(0, -1);

  rep(i, N) { cout << ans[i] << endl; }

  return 0;
}