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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

#define fir first
#define sec second
#define sz(s) (s).size()
#define pb push_back
#define get(n) scanf("%d", &n);
#define gets(s)                                                                \\
  string s;                                                                    \\
  cin >> (s);
#define prfi(n) printf("%d", &n);
#define prfd(n) printf("%lf", &n);
#define All(s) (s).begin(), (s).end()
#define rep(i, j) for (int(i) = 0; (i) < (j); (i)++)
#define For(i, j, k) for (int(i) = (j); (i) < (k); (i)++)
#define drep(i, j) for (int(i) = (j); (i) >= 0; (i)--)
#define Ford(i, j, k) for (int(i) = (j); i >= (k); i--)
#define vfor(c, v) for (auto(c) : (v))
#define lpi(n) for (int i = 0; i < (n); i++)
#define lpj(n) for (int j = 0; j < (n); j++)
#define lpz(n) for (int z = 0; z < (n); z++)
#define mem(a, b) memset(a, b, sizeof(a));
#define dump(x) std::cout << #x << " = " << (x) << std::endl;
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
using ll = long long;
using ld = long double;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vd = std::vector<double>;
using vvd = std::vector<vd>;
using qi = std::queue<int>;
using vpii = std::vector<std::pair<int, int>>;
using vpll = std::vector<pll>;
using namespace std;

const int Mod = (1e9) + 7;
const int INF = 1e9 + 19;
const ll INFL = 1e18 + 19;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const int dx2[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int dy2[] = {1, 0, -1, 1, 0, -1, 1, 0, -1};
//_____________________________________Templates_________________________________________//

template <class T1, class T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> inline void swap(T &a, T &b) {
  T c = a;
  a = b;
  b = c;
}
template <class T> inline void pri(T a) { cout << a << endl; }
template <class Z> using vec = vector<Z>;
// mainly use for dynamic prog
template <class T1, class T2> void update(T1 &a, T2 b) {
  a += b;
  if (a > Mod)
    a %= Mod;
}

inline void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void) {
  cout << "\\n";
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

bool pairsort(pll pl, pll pr) {
  if (pl.first == pr.first)
    return pl.second > pr.second;
  return pl.first < pr.first;
}

int cntbit(ll a, int n, int j) {
  int res = 0;
  For(i, j, n) {
    if (a >> i & 1) {
      res++;
    }
  }
  return res;
}
vector<int> make_bit(int a) {
  vector<int> res;
  lpi(32) if (a & (1 << i)) res.pb(i);
  return res;
}
bool stdbit(int a, int b) { return a & (1 << b); }
int GCD(int a, int b) {
  if (b > a)
    return GCD(b, a);
  if (a % b == 0)
    return b;
  else
    return GCD(b, a % b);
}
int LCM(int a, int b) { return a * b / GCD(a, b); }
int roundup(int a, int b) {
  if (a % b == 0)
    return a / b;
  else
    return (a + b) / b;
}
int rounddown(int a, int b) {
  if (a % b == 0)
    return a / b;
  else {
    return (a - b) / b;
  }
}
ll GetDiviserCount(ll N) // 約数の個数
{
  ll res = 1;
  For(i, 2, sqrt(N) + 1) {
    ll cnt = 0;
    while (N % i == 0) {
      cnt++;
      N /= i;
    }
    res *= (cnt + 1);
    if (N == 1)
      break;
  }
  if (N != 1)
    res *= 2;
  return res;
}
vll GetDivisor(ll N) // 約数列挙
{
  vll res;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      res.pb(i);
      if (i * i != N)
        res.pb(N / i);
    }
  }
  sort(All(res));
  return res;
}
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 {
    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;
  }
  friend ostream &operator<<(ostream &os, const mint &a);
};
ostream &operator<<(ostream &os, const mint &a) {
  os << a.x;
  return os;
}
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];
  }
};
//_____________________　following sorce code_________________________//
const int max_n = 3 * (1e5) + 1;
const int max_m = 83 * (1e5) + 1;

int n, m, k;
ll N;
int h, w;
string S;
int a, b, c;
vi v;
vector<mint> dp;
vector<mint> ans;
vvi G;
vi t;
combination comb(200100);
void dfs1(int v, int u = -1) // vは現在の頂点、uはどこからきたか
{
  mint res(1);
  if (G[v].size() == 0) {
    dp[v] = 1;
    return;
  }
  for (auto c : G[v]) {
    if (c == u)
      continue;
    dfs1(c, v);
    res *= dp[c];
    t[v] += t[c];
  }
  // combination comb(n+1);
  N = t[v] - 1;
  for (auto c : G[v]) {
    if (c == u)
      continue;
    res *= comb(N, t[c]);
    N -= t[c];
  }
  dp[v] = res;
  // cout << "dp[" << v << "] = " << dp[v] << endl;
  // cout << "t[" << v << "]= " << t[v] << endl;
  return;
}

void dfs2(int v, int u = -1) {
  if (v == 0) {
    for (auto c : G[v]) {
      dfs2(c, 0);
    }
    return;
  }
  mint resu(1);
  // dump(ans[u]);
  // dump(dp[v]);
  // dump(conb(n-1,t[v]));
  // cout << "t[" << u << "] = " << t[u] << endl;
  // cout << "t[" << v << "] = " << t[v] << endl;
  mint p(n - t[v]);
  resu = ans[u] * t[v];
  // dump(resu);
  resu /= p;
  ans[v] = resu;
  /*
  dump(v);
  dump(ans[u]);
  dump(t[v]);
  dump(p);
  dump(resu);
  OUT();
  */
  for (auto c : G[v]) {
    if (c == u)
      continue;
    dfs2(c, v);
  }
  // cout << "t[" << v << "]= " << t[v] << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  IN(n);
  t = vi(n, 1);
  dp = vector<mint>(n + 1);
  G = vvi(n);
  lpi(n - 1) {
    IN(a, b);
    a--;
    b--;
    G[a].pb(b);
    G[b].pb(a);
  }
  dfs1(0);
  ans = vector<mint>(n);
  ans[0] = dp[0];
  // dump(dp[0]);
  dfs2(0);
  for (auto c : ans) {
    cout << c << endl;
  }
  // for(auto c : ans){cout << c << endl;}
  // cout << fixed << setprecision(15) << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>

#define fir first
#define sec second
#define sz(s) (s).size()
#define pb push_back
#define get(n) scanf("%d", &n);
#define gets(s)                                                                \\
  string s;                                                                    \\
  cin >> (s);
#define prfi(n) printf("%d", &n);
#define prfd(n) printf("%lf", &n);
#define All(s) (s).begin(), (s).end()
#define rep(i, j) for (int(i) = 0; (i) < (j); (i)++)
#define For(i, j, k) for (int(i) = (j); (i) < (k); (i)++)
#define drep(i, j) for (int(i) = (j); (i) >= 0; (i)--)
#define Ford(i, j, k) for (int(i) = (j); i >= (k); i--)
#define vfor(c, v) for (auto(c) : (v))
#define lpi(n) for (int i = 0; i < (n); i++)
#define lpj(n) for (int j = 0; j < (n); j++)
#define lpz(n) for (int z = 0; z < (n); z++)
#define mem(a, b) memset(a, b, sizeof(a));
#define dump(x) std::cout << #x << " = " << (x) << std::endl;
#define debug(x)                                                               \\
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
using ll = long long;
using ld = long double;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vd = std::vector<double>;
using vvd = std::vector<vd>;
using qi = std::queue<int>;
using vpii = std::vector<std::pair<int, int>>;
using vpll = std::vector<pll>;
using namespace std;

const int Mod = (1e9) + 7;
const int INF = 1e9 + 19;
const ll INFL = 1e18 + 19;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
const int dx2[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int dy2[] = {1, 0, -1, 1, 0, -1, 1, 0, -1};
//_____________________________________Templates_________________________________________//

template <class T1, class T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> inline void swap(T &a, T &b) {
  T c = a;
  a = b;
  b = c;
}
template <class T> inline void pri(T a) { cout << a << endl; }
template <class Z> using vec = vector<Z>;
// mainly use for dynamic prog
template <class T1, class T2> void update(T1 &a, T2 b) {
  a += b;
  if (a > Mod)
    a %= Mod;
}

inline void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void) {
  cout << "\\n";
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

bool pairsort(pll pl, pll pr) {
  if (pl.first == pr.first)
    return pl.second > pr.second;
  return pl.first < pr.first;
}

int cntbit(ll a, int n, int j) {
  int res = 0;
  For(i, j, n) {
    if (a >> i & 1) {
      res++;
    }
  }
  return res;
}
vector<int> make_bit(int a) {
  vector<int> res;
  lpi(32) if (a & (1 << i)) res.pb(i);
  return res;
}
bool stdbit(int a, int b) { return a & (1 << b); }
int GCD(int a, int b) {
  if (b > a)
    return GCD(b, a);
  if (a % b == 0)
    return b;
  else
    return GCD(b, a % b);
}
int LCM(int a, int b) { return a * b / GCD(a, b); }
int roundup(int a, int b) {
  if (a % b == 0)
    return a / b;
  else
    return (a + b) / b;
}
int rounddown(int a, int b) {
  if (a % b == 0)
    return a / b;
  else {
    return (a - b) / b;
  }
}
ll GetDiviserCount(ll N) // 約数の個数
{
  ll res = 1;
  For(i, 2, sqrt(N) + 1) {
    ll cnt = 0;
    while (N % i == 0) {
      cnt++;
      N /= i;
    }
    res *= (cnt + 1);
    if (N == 1)
      break;
  }
  if (N != 1)
    res *= 2;
  return res;
}
vll GetDivisor(ll N) // 約数列挙
{
  vll res;
  for (ll i = 1; i * i <= N; i++) {
    if (N % i == 0) {
      res.pb(i);
      if (i * i != N)
        res.pb(N / i);
    }
  }
  sort(All(res));
  return res;
}
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 {
    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;
  }
  friend ostream &operator<<(ostream &os, const mint &a);
};
ostream &operator<<(ostream &os, const mint &a) {
  os << a.x;
  return os;
}
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];
  }
};
//_____________________　following sorce code_________________________//
const int max_n = 3 * (1e5) + 1;
const int max_m = 83 * (1e5) + 1;

int n, m, k;
ll N;
int h, w;
string S;
int a, b, c;
vi v;
vector<mint> dp;
vector<mint> ans;
vvi G;
vi t;
combination comb(20101);
void dfs1(int v, int u = -1) // vは現在の頂点、uはどこからきたか
{
  mint res(1);
  if (G[v].size() == 0) {
    dp[v] = 1;
    return;
  }
  for (auto c : G[v]) {
    if (c == u)
      continue;
    dfs1(c, v);
    res *= dp[c];
    t[v] += t[c];
  }
  // combination comb(n+1);
  N = t[v] - 1;
  for (auto c : G[v]) {
    if (c == u)
      continue;
    res *= comb(N, t[c]);
    N -= t[c];
  }
  dp[v] = res;
  // cout << "dp[" << v << "] = " << dp[v] << endl;
  // cout << "t[" << v << "]= " << t[v] << endl;
  return;
}

void dfs2(int v, int u = -1) {
  if (v == 0) {
    for (auto c : G[v]) {
      dfs2(c, 0);
    }
    return;
  }
  mint resu(1);
  // dump(ans[u]);
  // dump(dp[v]);
  // dump(conb(n-1,t[v]));
  // cout << "t[" << u << "] = " << t[u] << endl;
  // cout << "t[" << v << "] = " << t[v] << endl;
  mint p(n - t[v]);
  resu = ans[u] * t[v];
  // dump(resu);
  resu /= p;
  ans[v] = resu;
  /*
  dump(v);
  dump(ans[u]);
  dump(t[v]);
  dump(p);
  dump(resu);
  OUT();
  */
  for (auto c : G[v]) {
    if (c == u)
      continue;
    dfs2(c, v);
  }
  // cout << "t[" << v << "]= " << t[v] << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  IN(n);
  t = vi(n, 1);
  dp = vector<mint>(n + 1);
  G = vvi(n);
  lpi(n - 1) {
    IN(a, b);
    a--;
    b--;
    G[a].pb(b);
    G[b].pb(a);
  }
  dfs1(0);
  ans = vector<mint>(n);
  ans[0] = dp[0];
  // dump(dp[0]);
  dfs2(0);
  for (auto c : ans) {
    cout << c << endl;
  }
  // for(auto c : ans){cout << c << endl;}
  // cout << fixed << setprecision(15) << ans << endl;
  return 0;
}