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<int, int>;

const int MAX_N = 200005;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(MAX_N);

vector<int> to[MAX_N];

struct DP {
  mint dp;
  int t;
  DP(mint dp = 1, int t = 0) : dp(dp), t(t) {}
  DP operator+(const DP &a) const {
    return DP(dp * a.dp * comb(t + a.t, t), t + a.t);
  }
  DP addRoot() const { return DP(dp, t + 1); }
};

vector<DP> dp[MAX_N];
DP ans[MAX_N];

DP dfs(int v, int p = -1) {
  DP dpSum;
  dp[v] = vector<DP>(to[v].size());
  rep(i, to[v].size()) {
    int u = to[v][i];
    if (u == p)
      continue;
    dp[v][i] = dfs(u, v);
    dpSum = dpSum + dp[v][i];
  }
  return dpSum.addRoot();
}
void bfs(int v, const DP &dpP = DP(), int p = -1) {
  int deg = to[v].size();
  rep(i, deg) if (to[v][i] == p) dp[v][i] = dpP;

  vector<DP> dpLeft(deg + 1);
  rep(i, deg) dpLeft[i + 1] = dpLeft[i] + dp[v][i];

  vector<DP> dpRight(deg + 1);
  for (int i = deg - 1; i >= 0; --i)
    dpRight[i] = dpRight[i + 1] + dp[v][i];

  ans[v] = dpLeft[deg].addRoot();

  rep(i, deg) {
    int u = to[v][i];
    if (u == p)
      continue;
    DP d = dpLeft[i] + dpRight[i + 1];
    bfs(u, d.addRoot(), v);
  }
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  dfs(0);
  bfs(0);
  rep(i, n) cout << ans[i].dp << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define M 1000000007
#define MAX_SIZE 20001
using namespace std;

vector<ll> fac(MAX_SIZE);  // n!(mod M)
vector<ll> ifac(MAX_SIZE); // k!^{M-2} (mod M)

ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % M;
    x = x * x % M;
    n = n >> 1; // n = n/2
  }
  return ans;
}
void init_comb() {
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 1; i < MAX_SIZE; i++) {
    fac[i] = fac[i - 1] * i % M;                // n!(mod M)
    ifac[i] = ifac[i - 1] * mpow(i, M - 2) % M; // k!^{M-2} (mod M)
  }
}
ll combination(ll n, ll m) {
  if (m == n || m == 0)
    return 1;
  ll tmp = fac[n] * ifac[m] % M;
  return tmp * ifac[n - m] % M;
}

struct Info {
  ll cmb;
  ll t;
  Info(ll cmb = 1, ll t = 0) : cmb(cmb), t(t) {}
  Info operator+(Info a) {
    ll tmp_cmb = cmb * a.cmb % M;
    return Info(tmp_cmb * combination(t + a.t, t) % M, t + a.t);
  }
};

vector<ll> graph[MAX_SIZE];
Info infov[MAX_SIZE];

Info dfs(ll i, ll parent_i) {

  Info info;
  for (int j = 0; j < graph[i].size(); j++) {
    ll child_i = graph[i][j];
    if (child_i == parent_i)
      continue;
    info = info + dfs(child_i, i);
  }
  info.t += 1;
  infov[i] = info;
  return info;
}

int main() {
  init_comb();
  ll N, a, b;

  cin >> N;

  for (ll i = 0; i < N - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  for (int k = 0; k < N; k++) {
    Info ans = dfs(k, -1);
    cout << ans.cmb << endl;
    // cout<<"----------------------"<<endl;
    // for(int i=0;i<N;i++){
    // 	cout<<" "<<infov[i].cmb<< " " << infov[i].t << endl;
    // }
  }

  return 0;
}