Problem p02728 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

struct mint {
  ll x;
  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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= 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 {
    mint res(*this);
    return res /= a;
  }
};

const int cmax = 100010;
mint fac[cmax], finv[cmax], inv[cmax];

void com_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < cmax; i++) {
    fac[i] = fac[i - 1] * i;
    inv[i] = -inv[MOD % i] * (MOD / i);
    finv[i] = finv[i - 1] * inv[i];
  }
}

mint com(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  mint ret = fac[n] * finv[k] * finv[n - k];
  return ret;
}

struct DP {
  mint dp;
  ll cnt;
  DP(mint dp = 1, ll cnt = 0) : dp(dp), cnt(cnt){};
  DP operator+(const DP &a) {
    DP ret(*this);
    ret.dp *= com(ret.cnt + a.cnt, ret.cnt);
    ret.dp *= a.dp;
    ret.cnt += a.cnt;
    return ret;
  }
  DP operator-(const DP &a) {
    DP ret(*this);
    ret.cnt -= a.cnt;
    ret.dp /= com(ret.cnt + a.cnt, ret.cnt);
    ret.dp /= a.dp;
    return ret;
  }
  DP add_root() {
    DP ret(*this);
    ret.cnt++;
    return ret;
  }
};

vvll g;
vector<DP> dp;

void dfs(ll v, ll p = -1) {
  for (auto u : g[v]) {
    if (u == p)
      continue;
    dfs(u, v);
    dp[v] = dp[v] + dp[u].add_root();
  }
}

void dfs2(ll v, ll p = -1) {
  for (auto u : g[v]) {
    if (u == p)
      continue;
    DP d = dp[v] - dp[u].add_root();
    dp[u] = dp[u] + d.add_root();
    dfs2(u, v);
  }
}

void solve() {
  ll n;
  cin >> n;
  g = vvll(n);
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dp = vector<DP>(n);
  com_init();
  dfs(0);
  dfs2(0);
  for (ll i = 0; i < n; i++)
    cout << dp[i].dp.x << endl;
  return;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

/*
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
*/

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T &a, T b) { a = min(a, b); }
template <class T> void chmax(T &a, T b) { a = max(a, b); }

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

struct mint {
  ll x;
  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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= 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 {
    mint res(*this);
    return res /= a;
  }
};

const int cmax = 200010;
mint fac[cmax], finv[cmax], inv[cmax];

void com_init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < cmax; i++) {
    fac[i] = fac[i - 1] * i;
    inv[i] = -inv[MOD % i] * (MOD / i);
    finv[i] = finv[i - 1] * inv[i];
  }
}

mint com(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  mint ret = fac[n] * finv[k] * finv[n - k];
  return ret;
}

struct DP {
  mint dp;
  ll cnt;
  DP(mint dp = 1, ll cnt = 0) : dp(dp), cnt(cnt){};
  DP operator+(const DP &a) {
    DP ret(*this);
    ret.dp *= com(ret.cnt + a.cnt, ret.cnt);
    ret.dp *= a.dp;
    ret.cnt += a.cnt;
    return ret;
  }
  DP operator-(const DP &a) {
    DP ret(*this);
    ret.cnt -= a.cnt;
    ret.dp /= com(ret.cnt + a.cnt, ret.cnt);
    ret.dp /= a.dp;
    return ret;
  }
  DP add_root() {
    DP ret(*this);
    ret.cnt++;
    return ret;
  }
};

vvll g;
vector<DP> dp;

void dfs(ll v, ll p = -1) {
  for (auto u : g[v]) {
    if (u == p)
      continue;
    dfs(u, v);
    dp[v] = dp[v] + dp[u].add_root();
  }
}

void dfs2(ll v, ll p = -1) {
  for (auto u : g[v]) {
    if (u == p)
      continue;
    DP d = dp[v] - dp[u].add_root();
    dp[u] = dp[u] + d.add_root();
    dfs2(u, v);
  }
}

void solve() {
  ll n;
  cin >> n;
  g = vvll(n);
  for (ll i = 0; i < n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dp = vector<DP>(n);
  com_init();
  dfs(0);
  dfs2(0);
  for (ll i = 0; i < n; i++)
    cout << dp[i].dp.x << endl;
  return;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}