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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e14;
const ll mod = 1e9 + 7;
// __uint128_t

const int MAXX = 510000;
const int MOD = 1000000007;
long long fac[MAXX], finv[MAXX], inv[MAXX];

// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^{-1} mod を計算する
// (a%MOD) * modinv(b, MOD) % MOD で　a/b;
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
// (a%MOD) * modinv(b, MOD) % MOD で　a/b;
long long moddiv(long long a, long long b, long long mod = 1e9 + 7) {
  ll res = (a % mod) * modinv(b, mod) % mod;
  return (res + mod) % mod;
}

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAXX; 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;
}

ll N;
const ll MAX = 2 * (1e5) + 5;
vector<vector<ll>> G(MAX);
vector<ll> Size(MAX), Size2(MAX); // 部分木のサイズ
vector<ll> DP(MAX), DP2(MAX), ans(MAX);
ll CNT = 0;

ll rec(ll x, ll p = -1, ll cnt = 0) { // サイズ
  ll num = 1;
  for (auto v : G[x]) {
    if (v == p)
      continue;
    num += rec(v, x, cnt + 1);
  }
  return Size[x] = num;
}

ll dfs(ll x, ll p = -1) { // DPの計算
  ll res = 1;
  res *= fac[Size[x] - 1];
  for (auto v : G[x]) {
    if (v == p)
      continue;
    res *= finv[Size[v]];
    res %= MOD;
    // res = moddiv(res, fac[Size[v]]);
    res *= dfs(v, x);
    res %= MOD;
  }
  return DP[x] = res;
}

void dfs2(ll x, ll p = -1) { // ある接点から到達できる接点についてDP2を計算
  ll d = 1;                  // DP
  ll f = 1;                  // fac
  ll SSize = 1;
  for (auto v : G[x]) {
    if (v == p)
      continue;
    d *= DP[v];
    d %= MOD;
    f *= fac[Size[v]];
    f %= MOD;
    SSize += Size[v];
  }
  SSize = N - SSize;
  f *= fac[SSize];
  f %= MOD;
  for (auto v : G[x]) {
    if (v == p)
      continue;
    DP2[v] = (((moddiv(fac[N - (Size[v] + 1)], moddiv(f, fac[Size[v]])) % MOD) *
               moddiv(d, DP[v])) %
              MOD * DP2[x]) %
             MOD;
    // DP2[v] = (moddiv(fac[N-(Size[v]+1)],  moddiv(f, fac[Size[v]])) *
    // moddiv(d, DP[v]) * DP2[x])%MOD;
  }
  for (auto v : G[x]) {
    if (v != p)
      dfs2(v, x);
  }
}

void dfs3(ll x, ll p = -1) {
  ll res = 1;
  res *= fac[N - 1];
  ll SSize = 1;
  for (auto v : G[x]) {
    if (v == p)
      continue;
    SSize += Size[v];
  }
  SSize = N - SSize;

  for (auto v : G[x]) {
    if (v == p)
      res *= finv[SSize];
    else
      res *= finv[Size[v]];
    res %= MOD;

    // if(v == p) res = moddiv(res, fac[SSize]);
    // else res = moddiv(res, fac[Size[v]]);
  }
  for (auto v : G[x]) {
    if (v == p)
      continue;
    res *= DP[v];
    res %= MOD;
  }
  ans[x] = (res * DP2[x]) % MOD;
  for (auto v : G[x]) {
    if (v != p)
      dfs3(v, x);
  }
}

int main() {
  COMinit();
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  rec(0);
  dfs(0);
  DP2[0] = 1;
  ans[0] = DP[0];
  dfs2(0);
  dfs3(0);

  for (int i = 0; i < N; i++)
    cout << (ans[i] + MOD) % MOD << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e14;
const ll mod = 1e9 + 7;
// __uint128_t

const int MAXX = 510000;
const int MOD = 1000000007;
long long fac[MAXX], finv[MAXX], inv[MAXX];

// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// a^{-1} mod を計算する
// (a%MOD) * modinv(b, MOD) % MOD で　a/b;
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

long long moddiv(long long a, long long b, long long mod = 1e9 + 7) {
  return (a % mod) * modinv(b, mod) % mod;
}

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAXX; 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;
}

ll N;
const ll MAX = 2 * (1e2) + 5;
vector<vector<ll>> G(MAX);
vector<ll> Size(MAX), Size2(MAX); // 部分木のサイズ
vector<ll> DP(MAX), DP2(MAX), ans(MAX);
ll CNT = 0;

ll rec(ll x, ll p = -1, ll cnt = 0) {
  ll num = 1;
  for (auto v : G[x]) {
    if (v == p)
      continue;
    num += rec(v, x, cnt + 1);
  }
  return Size[x] = num;
}

ll dfs(ll x, ll p = -1) {
  ll res = 1;
  res *= fac[Size[x] - 1];
  for (auto v : G[x]) {
    if (v == p)
      continue;
    res = moddiv(res, fac[Size[v]]);
    res *= dfs(v, x);
  }
  return DP[x] = res;
}

void dfs2(ll x, ll p = -1) { // ある接点から到達できる接点についてDP2を計算
  ll d = 1;                  // DP
  ll f = 1;                  // fac
  ll SSize = 1;
  for (auto v : G[x]) {
    if (v == p)
      continue;
    d *= DP[v];
    f *= fac[Size[v]];
    SSize += Size[v];
  }
  SSize = N - SSize;
  f *= fac[SSize];
  for (auto v : G[x]) {
    if (v == p)
      continue;
    DP2[v] = moddiv(fac[N - (Size[v] + 1)], moddiv(f, fac[Size[v]])) *
             moddiv(d, DP[v]) * DP2[x];
  }
  for (auto v : G[x]) {
    if (v != p)
      dfs2(v, x);
  }
}

void dfs3(ll x, ll p = -1) {
  ll res = 1;
  res *= fac[N - 1];
  ll SSize = 1;
  for (auto v : G[x]) {
    if (v == p)
      continue;
    SSize += Size[v];
  }
  SSize = N - SSize;

  for (auto v : G[x]) {
    if (v == p)
      res = moddiv(res, fac[SSize]);
    else
      res = moddiv(res, fac[Size[v]]);
  }
  for (auto v : G[x]) {
    if (v == p)
      continue;
    res *= DP[v];
  }
  ans[x] = res * DP2[x];
  for (auto v : G[x]) {
    if (v != p)
      dfs3(v, x);
  }
}

int main() {
  COMinit();
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  rec(0);
  dfs(0);
  DP2[0] = 1;
  ans[0] = DP[0];
  dfs2(0);
  dfs3(0);

  for (int i = 0; i < N; i++)
    cout << ans[i] % MOD << endl;
}