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

#include <bits/stdc++.h>
template <long long mod> class Combination {
  std::vector<long long> fact, finv, inv;

public:
  Combination() {}
  Combination(int sz) {
    fact.resize(sz);
    finv.resize(sz);
    inv.resize(sz);
    fact[0] = 1;
    for (int i = 1; i < sz; ++i) {
      fact[i] = fact[i - 1] * i % mod;
    }
    inv[0] = 0;
    inv[1] = 1;
    for (int i = 2; i < sz; ++i) {
      inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    }
    finv[0] = 1;
    for (int i = 1; i < sz; ++i) {
      finv[i] = finv[i - 1] * inv[i] % mod;
    }
  }
  long long C(long long n, long long k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return (fact[n] * finv[k] % mod) * finv[n - k] % mod;
  }
  long long P(long long n, long long k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return fact[n] * finv[n - k] % mod;
  }
  long long H(long long n, long long k) { return this->C(n + k - 1, k); }
  long long getfact(int i) { return fact[i]; }
  long long getinv(int i) { return inv[i]; }
  long long getfinv(int i) { return finv[i]; }
};
template <typename T> T mod_pow(T x, T n, const T &p) {
  if (x < 0)
    return 0;
  T ret = 1;
  while (n > 0) {
    if (n & 1)
      (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const ld eps = 1e-9;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll << 50;
template <typename T> void printv(const vector<T> &s) {
  for (int i = 0; i < (int)(s.size()); ++i) {
    cout << s[i];
    if (i == (int)(s.size()) - 1)
      cout << endl;
    else
      cout << " ";
  }
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
int n;
Combination<MOD> comb;
vector<ll> sz, ans1, ans2;
int dfs1(int now, int prev, const vvi &tr) {
  int res = 1;
  ll su = 0;
  for (int i = 0; i < (int)(tr[now].size()); ++i) {
    int nxt = tr[now][i];
    if (nxt == prev)
      continue;
    int nowsz = dfs1(nxt, now, tr);
    su += nowsz;
    res += nowsz;
    ans1[now] *= ans1[nxt];
    ans1[now] %= MOD;
  }
  for (int i = 0; i < (int)(tr[now].size()); ++i) {
    int nxt = tr[now][i];
    if (nxt == prev)
      continue;
    ans1[now] *= comb.C(su, sz[nxt]);
    ans1[now] %= MOD;
    su -= sz[nxt];
  }
  sz[now] = res;
  return res;
}
void dfs2(int now, int prev, ll par, const vvi &tr) {
  ll tmpsu = n - 1;
  for (int i = 0; i < (int)(tr[now].size()); ++i) {
    int nxt = tr[now][i];
    if (nxt == prev)
      continue;
    ans2[now] *= ans1[nxt];
    ans2[now] %= MOD;
    ans2[now] *= comb.C(tmpsu, sz[nxt]);
    ans2[now] %= MOD;
    tmpsu -= sz[nxt];
  }
  ans2[now] *= par;
  ans2[now] %= MOD;
  for (int i = 0; i < (int)(tr[now].size()); ++i) {
    int nxt = tr[now][i];
    if (nxt == prev)
      continue;
    ll nxtpar = ans2[now];
    nxtpar *= mod_pow(ans1[nxt], MOD - 2, MOD);
    nxtpar %= MOD;
    nxtpar *= mod_pow(comb.C(n - 1, sz[nxt]), MOD - 2, MOD);
    nxtpar %= MOD;
    dfs2(nxt, now, nxtpar, tr);
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  comb = Combination<MOD>(1000000);
  cin >> n;
  vvi tr(n);
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tr[a].push_back(b);
    tr[b].push_back(a);
  }
  ans1.resize(n, 1);
  ans2.resize(n, 1);
  sz.resize(n, 0);
  dfs1(0, -1, tr);
  dfs2(0, -1, 1, tr);
  for (int i = 0; i < n; ++i) {
    cout << ans2[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
template <long long mod> class Combination {
  std::vector<long long> fact, finv, inv;

public:
  Combination() {}
  Combination(int sz) {
    fact.resize(sz);
    finv.resize(sz);
    inv.resize(sz);
    fact[0] = 1;
    for (int i = 1; i < sz; ++i) {
      fact[i] = fact[i - 1] * i % mod;
    }
    inv[0] = 0;
    inv[1] = 1;
    for (int i = 2; i < sz; ++i) {
      inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    }
    finv[0] = 1;
    for (int i = 1; i < sz; ++i) {
      finv[i] = finv[i - 1] * inv[i] % mod;
    }
  }
  long long C(long long n, long long k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return (fact[n] * finv[k] % mod) * finv[n - k] % mod;
  }
  long long P(long long n, long long k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return fact[n] * finv[n - k] % mod;
  }
  long long H(long long n, long long k) { return this->C(n + k - 1, k); }
  long long getfact(int i) { return fact[i]; }
  long long getinv(int i) { return inv[i]; }
  long long getfinv(int i) { return finv[i]; }
};
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const ld eps = 1e-9;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll << 50;
template <typename T> void printv(const vector<T> &s) {
  for (int i = 0; i < (int)(s.size()); ++i) {
    cout << s[i];
    if (i == (int)(s.size()) - 1)
      cout << endl;
    else
      cout << " ";
  }
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
int n;
Combination<MOD> comb;
vector<ll> sz, ans1, ans2;
int dfs1(int now, int prev, const vvi &tr) {
  int res = 1;
  ll su = 0;
  for (int i = 0; i < (int)(tr[now].size()); ++i) {
    int nxt = tr[now][i];
    if (nxt == prev)
      continue;
    int nowsz = dfs1(nxt, now, tr);
    su += nowsz;
    res += nowsz;
    ans1[now] *= ans1[nxt];
    ans1[now] %= MOD;
  }
  for (int i = 0; i < (int)(tr[now].size()); ++i) {
    int nxt = tr[now][i];
    if (nxt == prev)
      continue;
    ans1[now] *= comb.C(su, sz[nxt]);
    ans1[now] %= MOD;
    su -= sz[nxt];
  }
  sz[now] = res;
  return res;
}
void dfs2(int now, int prev, ll par, const vvi &tr) {
  ll tmpsu = n - 1;
  for (int i = 0; i < (int)(tr[now].size()); ++i) {
    int nxt = tr[now][i];
    if (nxt == prev)
      continue;
    ans2[now] *= ans1[nxt];
    ans2[now] %= MOD;
    ans2[now] *= comb.C(tmpsu, sz[nxt]);
    ans2[now] %= MOD;
    tmpsu -= sz[nxt];
  }
  ans2[now] *= par;
  ans2[now] %= MOD;
  for (int i = 0; i < (int)(tr[now].size()); ++i) {
    int nxt = tr[now][i];
    if (nxt == prev)
      continue;
    ll nxtpar = ans2[now];
    nxtpar *= comb.getinv(ans1[nxt]);
    nxtpar %= MOD;
    nxtpar *= comb.getinv(comb.C(n - 1, sz[nxt]));
    nxtpar %= MOD;
    dfs2(nxt, now, nxtpar, tr);
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  comb = Combination<MOD>(1000000);
  cin >> n;
  vvi tr(n);
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tr[a].push_back(b);
    tr[b].push_back(a);
  }
  ans1.resize(n, 1);
  ans2.resize(n, 1);
  sz.resize(n, 0);
  dfs1(0, -1, tr);
  dfs2(0, -1, 1, tr);
  for (int i = 0; i < n; ++i) {
    cout << ans2[i] << endl;
  }
}