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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

struct Combination {
  vector<long long> _fac, _finv, _inv;
  const int m = MOD;

  Combination(int sz) : _fac(sz), _finv(sz), _inv(sz) {
    _fac[0] = _fac[1] = 1;
    _finv[0] = _finv[1] = 1;
    _inv[1] = 1;
    for (int i = 2; i <= sz; i++) {
      _fac[i] = _fac[i - 1] * i % m;
      _inv[i] = m - _inv[m % i] * (m / i) % m;
      _finv[i] = _finv[i - 1] * _inv[i] % m;
    }
  }

  inline long long fac(int n) { return _fac[n]; }
  inline long long inv(int n) { return _inv[n]; }
  inline long long finv(int n) { return _finv[n]; }

  long long comb(int n, int k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return _fac[n] * (_finv[k] * _finv[n - k] % m) % m;
  }
};

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int N;
vector<vector<int>> G;
ll dp[200010];
int sz[200010];
Combination C(200010);

void dfs1(int u, int p) {
  dp[u] = 1;
  sz[u] = 1;
  ll now = 1;
  ll now2 = 1;
  for (int v : G[u]) {
    if (v == p)
      continue;
    dfs1(v, u);
    sz[u] += sz[v];
    now *= C.fac(sz[v]);
    now %= MOD;
    now2 *= dp[v];
    now2 %= MOD;
  }
  dp[u] *= C.fac(sz[u] - 1) * modinv(now, MOD) % MOD * now2 % MOD;
  dp[u] %= MOD;
}

void dfs2(int u, int p) {
  for (int v : G[u]) {
    if (v == p)
      continue;
    ll s = dp[v];
    dp[v] *= C.finv(sz[v] - 1);
    dp[v] %= MOD;
    dp[v] *= C.fac(N - 1);
    dp[v] %= MOD;
    dp[v] *= C.finv(N - sz[v]);
    dp[v] %= MOD;
    ll k = dp[u];
    k *= C.finv(N - 1);
    k %= MOD;
    k *= C.fac(N - 1 - sz[v]);
    k %= MOD;
    k *= C.fac(sz[v]);
    k %= MOD;
    k *= modinv(s, MOD);
    k %= MOD;
    dp[v] *= k;
    dp[v] %= MOD;
    dfs2(v, u);
  }
}

int main() {
  cin >> N;
  G.resize(N);
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs1(0, -1);
  dfs2(0, -1);
  rep(i, N) cout << dp[i] << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

struct Combination {
  vector<long long> _fac, _finv, _inv;
  const int m = MOD;

  Combination(int sz) : _fac(sz), _finv(sz), _inv(sz) {
    _fac[0] = _fac[1] = 1;
    _finv[0] = _finv[1] = 1;
    _inv[1] = 1;
    for (int i = 2; i <= sz; i++) {
      _fac[i] = _fac[i - 1] * i % m;
      _inv[i] = m - _inv[m % i] * (m / i) % m;
      _finv[i] = _finv[i - 1] * _inv[i] % m;
    }
  }

  inline long long fac(int n) { return _fac[n]; }
  inline long long inv(int n) { return _inv[n]; }
  inline long long finv(int n) { return _finv[n]; }

  long long comb(int n, int k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return _fac[n] * (_finv[k] * _finv[n - k] % m) % m;
  }
};

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int N;
vector<vector<int>> G;
ll dp[200010];
int sz[200010];
Combination C(200010);

void dfs1(int u, int p) {
  dp[u] = 1;
  sz[u] = 1;
  ll now = 1;
  ll now2 = 1;
  for (int v : G[u]) {
    if (v == p)
      continue;
    dfs1(v, u);
    sz[u] += sz[v];
    now *= C.fac(sz[v]);
    now %= MOD;
    now2 *= dp[v];
    now2 %= MOD;
  }
  dp[u] *= C.fac(sz[u] - 1) * C.inv(now) % MOD * now2 % MOD;
  dp[u] %= MOD;
}

void dfs2(int u, int p) {
  for (int v : G[u]) {
    if (v == p)
      continue;
    ll s = dp[v];
    dp[v] *= C.finv(sz[v] - 1);
    dp[v] %= MOD;
    dp[v] *= C.fac(N - 1);
    dp[v] %= MOD;
    dp[v] *= C.finv(N - sz[v]);
    dp[v] %= MOD;
    ll k = dp[u];
    k *= C.finv(N - 1);
    k %= MOD;
    k *= C.fac(N - 1 - sz[v]);
    k *= C.fac(sz[v]);
    k %= MOD;
    k *= modinv(s, MOD);
    k %= MOD;
    dp[v] *= k;
    dp[v] %= MOD;
    dfs2(v, u);
  }
}

int main() {
  cin >> N;
  G.resize(N);
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dfs1(0, -1);
  dfs2(0, -1);
  rep(i, N) cout << dp[i] << endl;
}