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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int INF = 1e9;
const int MOD = 1000000007;

const int MAX = 1000000;
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;
}

long long modpow(long long a, long long b, long long mod) {
  bitset<30> bit(b);
  string s = bit.to_string();
  long long ret = 1;
  for (int i = 0; i < s.length(); ++i) {
    ret = (ret * ret) % mod;
    if (s[i] - '0')
      ret = (ret * a) % mod;
  }
  return ret;
}

/*繰り返し二乗法でxのmod逆元を取る*/
long long inverse(long long x, long long mod) {
  return modpow(x, mod - 2, mod);
}

int n = 200005;
vector<vector<int>> tree(n, vector<int>());
vector<P> dp(n, P(0, 0));

P dfs1(int i, int p = -1) {
  if (dp[i].first != 0)
    return dp[i];
  ll sz = 0;
  ll dpi = 1;
  for (int next : tree[i]) {
    if (next == p)
      continue;
    P now = dfs1(next, i);
    sz += now.first;
    dpi = ((dpi * now.second) % MOD * COM(sz, now.first)) % MOD;
  }
  dp[i] = P(sz + 1, dpi);
  return dp[i];
}

void dfs2(int i, int p = -1, P gift = P(0LL, 0LL)) {
  if (p != -1) {
    dp[i].first += gift.first;
    dp[i].second = ((dp[i].second * gift.second) % MOD *
                    COM(dp[i].first - 1, gift.first)) %
                   MOD;
  }
  ll sz = dp[i].first;
  ll dpi = dp[i].second;
  for (int next : tree[i]) {
    if (next == p)
      continue;
    dfs2(next, i,
         P(sz - dp[next].first,
           (dpi * inverse((dp[next].second * COM(sz - 1, dp[next].first)) % MOD,
                          MOD)) %
               MOD));
  }
}

int main() {
  COMinit();
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  dfs1(0);
  dfs2(0);
  rep(i, n) cout << dp[i].second << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1000000007;
const int MAX = 1000000;

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;
  }
}

// 二項係数計算　n kどちらも10**5程度
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;
}

int n = 200005;
vector<vector<int>> tree(n, vector<int>());
vector<P> dp(n);
vector<bool> seen(n, false);

P dfs1(int i, int p = -1) {
  if (seen[i])
    return dp[i];
  seen[i] = true;
  int dp_i = 1;
  int sz = 0;
  for (int child : tree[i]) {
    if (child == p)
      continue;
    P res = dfs1(child, i);
    sz += res.second;
    dp_i = (dp_i * (COM(sz, res.second) * res.first) % MOD) % MOD;
  }
  dp[i] = P(dp_i, sz + 1);
  return dp[i];
}

void dfs2(int i, int p = -1, P now = P(0, 0)) {
  if (p != -1) {
    dp[i].second += now.second;
    dp[i].first =
        (dp[i].first * (COM(dp[i].second - 1, now.second) * now.first) % MOD) %
        MOD;
  }
  for (int child : tree[i]) {
    if (child == p)
      continue;
    P res;
    res.second = dp[i].second - dp[child].second;
    res.first = (dp[i].first * inv[dp[child].first]) % MOD;
    res.first =
        (res.first * inv[COM(dp[i].second - 1, dp[child].second)]) % MOD;
    dfs2(child, i, res);
  }
}

int main() {
  COMinit();
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  dfs1(0);
  dfs2(0);

  rep(i, n) { cout << dp[i].first << endl; }
}