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

#include <iostream>
#include <vector>
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
using namespace std;
int n;
ll quick(ll a, ll b, ll m) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
ll jiecheng[maxn];
void ini_zuhelogn() {
  jiecheng[0] = 1;
  for (int i = 1; i < maxn; i++) {
    jiecheng[i] = jiecheng[i - 1] * i % mod;
  }
}
ll zuhe_logn(int m, int n) {
  if (jiecheng[0] != 1)
    ini_zuhelogn();
  ll ans = ((jiecheng[m] * quick(jiecheng[n], mod - 2, mod)) % mod) *
           quick(jiecheng[m - n], mod - 2, mod) % mod;
  return ans;
}
ll ans[maxn], ans_up[maxn], sizet[maxn];
vector<int> G[maxn];
void dfs1(int x, int f) {
  ll anst = 1, sum = 0;
  for (int i = 0; i < G[x].size(); i++) {
    int v = G[x][i];
    if (v != f) {
      dfs1(v, x);
      //            cout<<v<<" "<<ans[v]<<" "<<sum<<" "<<sizet[v]<<endl;
      anst = anst * (ans[v] * zuhe_logn(sum + sizet[v], sizet[v]) % mod) % mod;
      sum += sizet[v];
    }
  }
  ans[x] = anst;
  sizet[x] = sum + 1;
}
void dfs2(int x, int f) {

  ll anst = ans_up[x], sum = n - sizet[x];
  for (int i = 0; i < G[x].size(); i++) {
    int v = G[x][i];
    if (v == f)
      continue;
    anst = anst * (ans[v] * zuhe_logn(sum + sizet[v], sizet[v]) % mod) % mod;
    sum += sizet[v];
  }
  ans[x] = anst;
  for (int i = 0; i < G[x].size(); i++) {
    int v = G[x][i];
    if (v == f)
      continue;

    ans_up[v] =
        ans[x] *
        quick((ans[v] * zuhe_logn(n - 1, sizet[v]) % mod), mod - 2, mod) % mod;
    //        cout<<v<<" "<<ans[x]<<" "<<ans[v]<<" "<<ans_up[v]<<endl;
    dfs2(v, x);
  }
}
int main() {

  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);
  }
  dfs1(1, 1);
  ans_up[1] = 1;

  dfs2(1, 1);
  for (int i = 0; i < n; i++)
    cout << ans[i + 1] << endl;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
using namespace std;
int n;
ll quick(ll a, ll b, ll m) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
ll jiecheng[maxn];
void ini_zuhelogn() {
  jiecheng[0] = 1;
  for (int i = 1; i < maxn; i++) {
    jiecheng[i] = jiecheng[i - 1] * i % mod;
  }
}
ll zuhe_logn(int m, int n) {
  if (jiecheng[0] != 1)
    ini_zuhelogn();
  ll ans = ((jiecheng[m] * quick(jiecheng[n], mod - 2, mod)) % mod) *
           quick(jiecheng[m - n], mod - 2, mod) % mod;
  return ans;
}
ll ans[maxn], ans_up[maxn], sizet[maxn];
vector<int> G[maxn];
void dfs1(int x, int f) {
  ll anst = 1, sum = 0;
  for (int i = 0; i < G[x].size(); i++) {
    int v = G[x][i];
    if (v != f) {
      dfs1(v, x);
      //            cout<<v<<" "<<ans[v]<<" "<<sum<<" "<<sizet[v]<<endl;
      anst = anst * (ans[v] * zuhe_logn(sum + sizet[v], sizet[v]) % mod) % mod;
      sum += sizet[v];
    }
  }
  ans[x] = anst;
  sizet[x] = sum + 1;
}
void dfs2(int x, int f) {

  ll anst = ans_up[x], sum = n - sizet[x];
  for (int i = 0; i < G[x].size(); i++) {
    int v = G[x][i];
    if (v == f)
      continue;
    anst = anst * (ans[v] * zuhe_logn(sum + sizet[v], sizet[v]) % mod) % mod;
    sum += sizet[v];
  }
  ans[x] = anst;
  for (int i = 0; i < G[x].size(); i++) {
    int v = G[x][i];
    if (v == f)
      continue;

    ans_up[v] =
        ans[x] *
        quick((ans[v] * zuhe_logn(n - 1, sizet[v]) % mod), mod - 2, mod) % mod;
    //        cout<<v<<" "<<ans[x]<<" "<<ans[v]<<" "<<ans_up[v]<<endl;
    dfs2(v, x);
  }
}
int main() {

  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);
  }
  dfs1(1, 1);
  ans_up[1] = 1;

  dfs2(1, 1);
  for (int i = 0; i < n; i++)
    cout << ans[i + 1] << endl;
}