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

#include <bits/stdc++.h>
using namespace std;
// typedef long long LL;
// typedef unsigned long long UL;
// typedef pair<int, int> P;
// typedef pair<int, P> PPI;
// typedef pair<LL, LL> PL;
// typedef long long LL;
// // LLONG_MIN, INT_MIN
// #define xx first
// #define yy second
// const LL INF = 1000000000;
#define RG register int
#define LL long long
struct edge {
  LL to, cost;
  edge(int a, int b) : to(a), cost(b) {}
};

void debug(vector<int> &db) {
  for (auto x : db)
    cout << x << " ";
  cout << endl;
}
LL mod = 1000000007;
vector<int> G[210000];
LL ps[210000];
vector<LL> st(210000, 1);
LL son[210000];
LL ans[210000];

LL pmod(LL x, LL k, LL m = mod) {
  LL re = 1;
  LL now = 1;
  while (k) {
    if (k & 1)
      re *= x;
    k >>= 1;
    x = (x * x) % m;
    re %= mod;
  }
  return re;
}

LL inv(LL a) {
  // if(a==0)return 1;
  return pmod(a, mod - 2);
}

void dfs(int p, int from = -1) {
  son[p] = 1;
  for (auto adj : G[p]) {
    if (adj == from)
      continue;
    dfs(adj, p);
    son[p] += son[adj];
  }
  st[p] *= ps[son[p] - 1];
  for (auto adj : G[p]) {
    if (adj == from)
      continue;
    st[p] *= inv(ps[son[adj]]);
    st[p] %= mod;
    st[p] *= st[adj];
    st[p] %= mod;
  }
}

void reroot(int p, int from = -1) {
  ans[p] = st[p];
  for (auto adj : G[p]) {
    if (adj == from)
      continue;
    LL or_stp = st[p], or_stadj = st[adj];
    LL diffp = (ps[son[p] - 1] * inv(ps[son[p] - 1 - son[adj]])) % mod;
    st[p] = (st[p] * inv(diffp)) % mod;
    st[p] = (st[p] * ps[son[adj]]) % mod;
    st[p] = (st[p] * inv(st[adj])) % mod;

    diffp = (ps[son[p] - 1] * inv(ps[son[adj] - 1])) % mod;
    st[adj] = (st[adj] * diffp) % mod;
    st[adj] = (st[adj] * st[p]) % mod;
    st[adj] = (st[adj] * inv(ps[son[p] - son[adj]])) % mod;

    son[p] -= son[adj];
    son[adj] += son[p];
    reroot(adj, p);
    son[adj] -= son[p];
    son[p] += son[adj];
    st[p] = or_stp;
    st[adj] = or_stadj;
  }
}
int main() {
  // freopen("input", "r", stdin);
  // freopen("output", "w", stdout);
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  ps[0] = 1;
  for (LL i = 1; i < 210000; i++)
    ps[i] = (ps[i - 1] * i) % mod;
  dfs(1);
  reroot(1);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// typedef long long LL;
// typedef unsigned long long UL;
// typedef pair<int, int> P;
// typedef pair<int, P> PPI;
// typedef pair<LL, LL> PL;
// typedef long long LL;
// // LLONG_MIN, INT_MIN
// #define xx first
// #define yy second
// const LL INF = 1000000000;
#define RG register int
#define LL long long
struct edge {
  LL to, cost;
  edge(int a, int b) : to(a), cost(b) {}
};

void debug(vector<int> &db) {
  for (auto x : db)
    cout << x << " ";
  cout << endl;
}
LL mod = 1000000007;
vector<int> G[210000];
LL ps[110000];
vector<LL> st(110000, 1);
LL son[110000];
LL ans[110000];

LL pmod(LL x, LL k, LL m = mod) {
  LL re = 1;
  LL now = 1;
  while (k) {
    if (k & 1)
      re *= x;
    k >>= 1;
    x = (x * x) % m;
    re %= mod;
  }
  return re;
}

LL inv(LL a) {
  if (a == 0)
    return 1;
  return pmod(a, mod - 2);
}

void dfs(int p, int from = -1) {
  son[p] = 1;
  for (auto adj : G[p]) {
    if (adj == from)
      continue;
    dfs(adj, p);
    son[p] += son[adj];
  }
  st[p] *= ps[son[p] - 1];
  for (auto adj : G[p]) {
    if (adj == from)
      continue;
    st[p] *= inv(ps[son[adj]]);
    st[p] %= mod;
    st[p] *= st[adj];
    st[p] %= mod;
  }
}

void reroot(int p, int from = -1) {
  ans[p] = st[p];
  for (auto adj : G[p]) {
    if (adj == from)
      continue;
    LL or_stp = st[p], or_stadj = st[adj];
    LL diffp = (ps[son[p] - 1] * inv(ps[son[p] - 1 - son[adj]])) % mod;
    st[p] = (st[p] * inv(diffp)) % mod;
    st[p] = (st[p] * ps[son[adj]]) % mod;
    st[p] = (st[p] * inv(st[adj])) % mod;

    diffp = (ps[son[p] - 1] * inv(ps[son[adj] - 1])) % mod;
    st[adj] = (st[adj] * diffp) % mod;
    st[adj] = (st[adj] * st[p]) % mod;
    st[adj] = (st[adj] * inv(ps[son[p] - son[adj]])) % mod;

    son[p] -= son[adj];
    son[adj] += son[p];
    reroot(adj, p);
    son[adj] -= son[p];
    son[p] += son[adj];
    st[p] = or_stp;
    st[adj] = or_stadj;
  }
}
int main() {
  // freopen("input", "r", stdin);
  // freopen("output", "w", stdout);
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  ps[0] = 1;
  for (LL i = 1; i < 110000; i++)
    ps[i] = (ps[i - 1] * i) % mod;
  dfs(1);
  reroot(1);
  for (int i = 1; i <= n; i++)
    cout << ans[i] << endl;
}