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

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = int(a); i < (b); i++)
#define repr(i, b, a) for (int i = int(b); i >= (a); i--)
#define all(x) x.begin(), x.end()

const ll mod = 1e9 + 7;
const ll INF = 1e9;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool valid(int lx, int ux, int ly, int uy, int x, int y) {
  return lx <= x && x < ux && ly <= y && y < uy;
}
ll power(ll x, ll p) {
  if (p == 0)
    return 1;
  ll res = power(x * x % mod, p / 2);
  if (p % 2 == 1)
    res = res * x % mod;
  return res;
}

vector<ll> fact, inv;

ll f(ll a, ll b, ll p) { // a^b mod pを求める
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll d = f(a, b / 2, p); // a^b = a^(2k) = (a^k)^2  k = b / 2
    return d * d % mod;
  } else {
    return a * f(a, b - 1, p) % mod;
  }
}

void init(ll n) {
  fact.resize(n + 1, 1LL);
  inv.resize(n + 1, 1LL);
  for (int i = 2; i <= n; i++) // 　nまでの階乗を求める
    fact[i] = fact[i - 1] * i % mod;
  for (int i = 2; i <= n;
       i++) // a^(-1) mod p = a^(p-2) mod p  ここをもう少し早くできるぽい
    inv[i] = f(fact[i], mod - 2, mod) % mod;
}

ll comb(ll n, ll k) { // n_C_k
  if (n < k)
    return 0;
  return (fact[n] * inv[k] % mod) * inv[n - k] % mod;
}

vector<vector<ll>> g, di_g;
vector<ll> dp, dp2, subtree_size, parent, used;

void dfs(ll cur) {
  used[cur] = true;
  dp[cur] = 1;
  subtree_size[cur] = 1;
  for (auto next : g[cur]) {
    if (used[next])
      continue;

    di_g[cur].push_back(next);
    parent[next] = cur;
    dfs(next);
    subtree_size[cur] += subtree_size[next];
    dp[cur] *= dp[next];
    dp[cur] %= mod;

    dp[cur] *= inv[subtree_size[next]];
    dp[cur] %= mod;
  }
  dp[cur] *= fact[subtree_size[cur] - 1];
  dp[cur] %= mod;
}

int main() {
  ll n;
  cin >> n;
  g.resize(n, vector<ll>());
  di_g.resize(n, vector<ll>());
  dp.resize(n, 0);
  dp2.resize(n, 0);
  used.resize(n);
  subtree_size.resize(n, 0);
  parent.resize(n, -1);
  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
    // parent[b-1] = a-1;
  }
  init(n + 10);

  ll root = 0;
  dfs(root);

  // rep(i,n)cout<<dp[i]<<"  ";
  // cout<<endl;

  vector<ll> dp_power(n, 0);
  rep(i, n) dp_power[i] = power(dp[i], mod - 2);

  queue<int> que;
  que.push(root);
  while (!que.empty()) {
    int cur = que.front();
    que.pop();
    dp2[cur] = 1LL;

    if (parent[cur] != -1) {
      ll tmp = dp2[parent[cur]];
      // tmp *= power(dp[cur], mod-2);
      tmp *= dp_power[cur];
      tmp %= mod;

      tmp *= fact[subtree_size[cur]];
      tmp %= mod;

      tmp *= inv[n - 1];
      tmp %= mod;

      tmp *= fact[n - subtree_size[cur] - 1];
      tmp %= mod;

      dp2[cur] *= tmp;
      dp2[cur] %= mod;
      dp2[cur] *= inv[n - subtree_size[cur]];
      dp2[cur] %= mod;
    }

    // cout<<"cur = "<<cur<<endl;
    for (auto x : di_g[cur]) {
      // cout<<"x = "<<x<<endl;
      dp2[cur] *= dp[x];
      dp2[cur] %= mod;
      dp2[cur] *= inv[subtree_size[x]];
      dp2[cur] %= mod;
      que.push(x);
    }
    dp2[cur] *= fact[n - 1];
    dp2[cur] %= mod;
  }

  rep(i, n) { cout << dp2[i] << endl; }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int, int> P;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = int(a); i < (b); i++)
#define repr(i, b, a) for (int i = int(b); i >= (a); i--)
#define all(x) x.begin(), x.end()

const ll mod = 1e9 + 7;
const ll INF = 1e9;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool valid(int lx, int ux, int ly, int uy, int x, int y) {
  return lx <= x && x < ux && ly <= y && y < uy;
}
ll power(ll x, ll p) {
  if (p == 0)
    return 1;
  ll res = power(x * x % mod, p / 2);
  if (p % 2 == 1)
    res = res * x % mod;
  return res;
}

vector<ll> fact, inv;

ll f(ll a, ll b, ll p) { // a^b mod pを求める
  if (b == 0)
    return 1;
  else if (b % 2 == 0) {
    ll d = f(a, b / 2, p); // a^b = a^(2k) = (a^k)^2  k = b / 2
    return d * d % mod;
  } else {
    return a * f(a, b - 1, p) % mod;
  }
}

void init(ll n) {
  fact.resize(n + 1, 1LL);
  inv.resize(n + 1, 1LL);
  for (int i = 2; i <= n; i++) // 　nまでの階乗を求める
    fact[i] = fact[i - 1] * i % mod;
  for (int i = 2; i <= n;
       i++) // a^(-1) mod p = a^(p-2) mod p  ここをもう少し早くできるぽい
    inv[i] = f(fact[i], mod - 2, mod) % mod;
}

ll comb(ll n, ll k) { // n_C_k
  if (n < k)
    return 0;
  return (fact[n] * inv[k] % mod) * inv[n - k] % mod;
}

vector<vector<ll>> g;
vector<ll> dp, dp2, used, subtree_size, parent;

void dfs(ll cur) {
  // used[cur]=true;
  dp[cur] = 1;
  subtree_size[cur] = 1;
  for (auto next : g[cur]) {
    if (parent[cur] == next)
      continue;

    dfs(next);
    subtree_size[cur] += subtree_size[next];
    dp[cur] *= dp[next];
    dp[cur] %= mod;

    dp[cur] *= inv[subtree_size[next]];
    dp[cur] %= mod;
  }
  dp[cur] *= fact[subtree_size[cur] - 1];
  dp[cur] %= mod;
}

int main() {
  ll n;
  cin >> n;
  g.resize(n, vector<ll>());
  dp.resize(n);
  dp2.resize(n);
  // used.resize(n);
  subtree_size.resize(n, 0);
  parent.resize(n, -1);
  rep(i, n - 1) {
    ll a, b;
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
    parent[b - 1] = a - 1;
  }
  init(n + 10);

  ll root = 0;
  rep(i, n) if (parent[i] == -1) root = i;

  dfs(root);

  // rep(i,n)cout<<i<<" "<<dp[i]<<" "<<subtree_size[i]<<endl;
  vector<ll> dp_power(n);
  rep(i, n) dp_power[i] = power(dp[i], mod - 2);

  queue<int> que;
  que.push(root);
  while (!que.empty()) {
    int cur = que.front();
    que.pop();
    dp2[cur] = 1;
    for (auto x : g[cur]) {
      if (parent[cur] == x) {
        ll tmp = dp2[x];
        // tmp *= power(dp[cur], mod-2);
        tmp *= dp_power[cur];
        tmp %= mod;

        tmp *= fact[subtree_size[cur]];
        tmp %= mod;

        tmp *= inv[n - 1];
        tmp %= mod;

        tmp *= fact[n - subtree_size[cur] - 1];
        tmp %= mod;

        dp2[cur] *= tmp;
        dp2[cur] %= mod;
        dp2[cur] *= inv[n - subtree_size[cur]];
        dp2[cur] %= mod;
      } else {
        dp2[cur] *= dp[x];
        dp2[cur] %= mod;
        dp2[cur] *= inv[subtree_size[x]];
        dp2[cur] %= mod;
        que.push(x);
      }
    }
    dp2[cur] *= fact[n - 1];
    dp2[cur] %= mod;
  }

  rep(i, n) { cout << dp2[i] << endl; }

  return 0;
}