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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define mo 1000000007
#define MOD 1000000007
// priority_queue<int,vector<int>, greater<int> > q2;

vector<vector<int>> G;
int N;
int dp[200010];
int ans[200010];
int t[200010]; // size of subtree
ll inv[200010], fac[200010], finv[200010];
const int SIZE = 200010;

ll comb(ll N_, ll C_) {
  const int NUM_ = 400001;
  static ll fact[400002], factr[400002], inv[400002];
  if (fact[0] == 0) {
    inv[1] = fact[0] = factr[0] = 1;
    for (int i = 2; i <= NUM_; ++i)
      inv[i] = inv[mo % i] * (mo - mo / i) % mo;
    for (int i = 1; i <= NUM_; ++i)
      fact[i] = fact[i - 1] * i % mo, factr[i] = factr[i - 1] * inv[i] % mo;
  }
  if (C_ < 0 || C_ > N_)
    return 0;
  return factr[C_] * fact[N_] % mo * factr[N_ - C_] % mo;
}
ll modpow(ll a, ll n = mo - 2) {
  ll r = 1;
  while (n)
    r = r * ((n % 2) ? a : 1) % mo, a = a * a % mo, n >>= 1;
  return r;
}

long long modinv(long long a) { return modpow(a, mo - 2); }

void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < SIZE; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fac[i] = fac[i - 1] * (ll)i % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

void dfs1(int now, int pa) {
  fore(to, G[now]) {
    if (to == pa)
      continue;
    dfs1(to, now);
  }

  dp[now] = fac[t[now]];
  // cout << now << " " << t[G[0][0]]<<" "<<t[now] << endl;
  fore(to, G[now]) {
    if (to == pa)
      continue;
    // if (now == 1)cout << t[now] <<" "<<t[to]<< endl;
    // dp[now] *= inv[fac[t[to]]];
    dp[now] *= modinv(fac[t[to]]);
    dp[now] %= mod;
    dp[now] *= dp[to];
    dp[now] %= mod;
  }
  // cout << now << " " << dp[now] << endl;
  t[now]++;
  if (pa != -1)
    t[pa] += t[now];
  return;
}

void dfs(int now, int pa) {
  if (pa == -1)
    ans[now] = dp[now];
  else {
    int res = fac[N - 1];
    fore(to, G[now]) {
      if (pa == to)
        continue;
      // res *= inv[fac[t[to]]];
      res *= modinv(fac[t[to]]);
      res %= mod;
      res *= dp[to];
      res %= mod;
    }

    // res *= inv[fac[N - t[now]]];//親側の部分木
    res *= modinv(fac[N - t[now]]);
    res %= mod;
    // cout << now << " " << res << endl;

    int a = dp[pa];
    // a = (a*inv[dp[now]]) % mod;
    a = (a * modinv(dp[now])) % mod;
    a = (a * fac[t[now]]) % mod;

    // a = (a*inv[fac[N - 1]]) % mod;
    a = (a * modinv(fac[N - 1])) % mod;
    a = (a * fac[N - t[now] - 1]) % mod;

    // res *= (dp[pa] * inv[dp[now]] * fac[t[now]]) % mod;
    res *= a;
    res %= mod;
    ans[now] = res;
    dp[now] = res;
  }
  fore(to, G[now]) {
    if (to == pa)
      continue;
    dfs(to, now);
  }
}

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

  make();
  cin >> N;
  G.resize(N);
  REP(i, N) dp[i] = 1;

  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs1(0, -1);
  // REP(i, N) cout << dp[i]<<" "<<t[i] << endl;
  dfs(0, -1);
  // REP(i, N) cout << dp[i] << endl;

  REP(i, N) cout << ans[i] << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define mo 1000000007
#define MOD 1000000007
// priority_queue<int,vector<int>, greater<int> > q2;

vector<vector<int>> G;
int N;
int dp[200010];
int ans[200010];
int t[200010]; // size of subtree
ll inv[200010], fac[200010], finv[200010];
const int SIZE = 200010;

ll comb(ll N_, ll C_) {
  const int NUM_ = 400001;
  static ll fact[400002], factr[400002], inv[400002];
  if (fact[0] == 0) {
    inv[1] = fact[0] = factr[0] = 1;
    for (int i = 2; i <= NUM_; ++i)
      inv[i] = inv[mo % i] * (mo - mo / i) % mo;
    for (int i = 1; i <= NUM_; ++i)
      fact[i] = fact[i - 1] * i % mo, factr[i] = factr[i - 1] * inv[i] % mo;
  }
  if (C_ < 0 || C_ > N_)
    return 0;
  return factr[C_] * fact[N_] % mo * factr[N_ - C_] % mo;
}
ll modpow(ll a, ll n = mo - 2) {
  ll r = 1;
  while (n)
    r = r * ((n % 2) ? a : 1) % mo, a = a * a % mo, n >>= 1;
  return r;
}

long long modinv(long long a) { return modpow(a, mo - 2); }

void make() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < SIZE; i++) {
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fac[i] = fac[i - 1] * (ll)i % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

void dfs1(int now, int pa) {
  fore(to, G[now]) {
    if (to == pa)
      continue;
    dfs1(to, now);
  }

  dp[now] = fac[t[now]];
  // cout << now << " " << t[G[0][0]]<<" "<<t[now] << endl;
  fore(to, G[now]) {
    if (to == pa)
      continue;
    // if (now == 1)cout << t[now] <<" "<<t[to]<< endl;
    dp[now] *= inv[fac[t[to]]];
    dp[now] %= mod;
    dp[now] *= dp[to];
    dp[now] %= mod;
  }
  // cout << now << " " << dp[now] << endl;
  t[now]++;
  if (pa != -1)
    t[pa] += t[now];
  return;
}

void dfs(int now, int pa) {
  if (pa == -1)
    ans[now] = dp[now];
  else {
    int res = fac[N - 1];
    fore(to, G[now]) {
      if (pa == to)
        continue;
      res *= inv[fac[t[to]]];
      res %= mod;
      res *= dp[to];
      res %= mod;
    }

    res *= inv[fac[N - t[now]]]; // 親側の部分木
    res %= mod;
    // cout << now << " " << res << endl;

    int a = dp[pa];
    a = (a * inv[dp[now]]) % mod;

    a = (a * fac[t[now]]) % mod;

    a = (a * inv[fac[N - 1]]) % mod;
    a = (a * fac[max(0LL, N - t[now] - 1)]) % mod;

    // res *= (dp[pa] * inv[dp[now]] * fac[t[now]]) % mod;
    res *= a;
    res %= mod;
    ans[now] = res;
    dp[now] = res;
  }
  fore(to, G[now]) {
    if (to == pa)
      continue;
    dfs(to, now);
  }
}

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

  make();
  cin >> N;
  G.resize(N);
  REP(i, N) dp[i] = 1;

  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs1(0, -1);
  // REP(i, N) cout << dp[i]<<" "<<t[i] << endl;
  dfs(0, -1);
  // REP(i, N) cout << dp[i] << endl;

  REP(i, N) cout << ans[i] << "\\n";

  return 0;
}