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

#include <bits/stdc++.h>
using namespace std;

const long mod = 1000000007;
int n;
vector<int> g[200000];
long sz[200000];
long dp[200000];
long f[200001];
long ans[200000];

void pre() {
  f[0] = 1;
  for (int i = 0; i < n; ++i) {
    f[i + 1] = f[i] * (i + 1) % mod;
  }
}

long fact(int k) { return f[k]; }

long mpow(long b, long e) {
  if (e == 0)
    return 1;
  if (e % 2 == 1)
    return mpow(b, e - 1) * b % mod;
  long t = mpow(b, e / 2);
  return t * t % mod;
}

long inv(long n) { return mpow(n, mod - 2); }

// (size, dp)
pair<long, long> prerec(int from, int prev) {
  long way = 1;
  long cnt = 1;
  long coeff = 1;
  for (int to : g[from]) {
    if (to == prev)
      continue;
    auto p = prerec(to, from);
    cnt += p.first;
    way = way * p.second % mod;
    coeff = coeff * fact(p.first) % mod;
  }

  long ret = way * fact(cnt - 1) % mod * inv(coeff) % mod;
  sz[from] = cnt;
  dp[from] = ret;
  return make_pair(cnt, ret);
}

void rec(int from, int prev, pair<long, long> inher) {
  int m = g[from].size();
  long cnt = 1;
  vector<long> ways;
  for (int to : g[from]) {
    if (to == prev) {
      cnt += inher.first;
      ways.push_back(inher.second * inv(fact(inher.first)) % mod);
    } else {
      cnt += sz[to];
      ways.push_back(dp[to] * inv(fact(sz[to])) % mod);
    }
  }

  vector<long> l(m + 1, 1);
  vector<long> r(m, 1);
  for (int i = 1; i <= m; ++i)
    l[i] = l[i - 1] * ways[i - 1] % mod;
  for (int i = m - 2; i >= 0; --i)
    r[i] = r[i + 1] * ways[i + 1] % mod;

  for (int i = 0; i < m; ++i) {
    int to = g[from][i];
    if (to == prev)
      continue;
    long ncnt = cnt - sz[to];
    long nret = l[i] * r[i] % mod * fact(ncnt - 1) % mod;
    rec(to, from, make_pair(ncnt, nret));
  }
  ans[from] = fact(n - 1) * l[m] % mod;
}

int main() {
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  pre();
  prerec(0, -1);
  rec(0, -1, make_pair(0, 1));

  for (int i = 0; i < n; ++i) {
    cout << ans[i] % mod << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int mod = 1000000007;
int n;
vector<int> g[100000];
long sz[100000];
long dp[100000];
long f[100001];
long ans[100000];

void pre() {
  f[0] = 1;
  for (int i = 0; i < n; ++i) {
    f[i + 1] = f[i] * (i + 1) % mod;
  }
}

long fact(int n) { return f[n]; }

long mpow(long b, long e) {
  if (e == 0)
    return 1;
  if (e % 2 == 1)
    return mpow(b, e - 1) * b % mod;
  long t = mpow(b, e / 2);
  return t * t % mod;
}

long inv(long n) { return mpow(n, mod - 2); }

// (size, dp)
pair<long, long> prerec(int from, int prev) {
  long way = 1;
  long cnt = 1;
  long coeff = 1;
  for (int to : g[from]) {
    if (to == prev)
      continue;
    auto p = prerec(to, from);
    cnt += p.first;
    way = way * p.second % mod;
    coeff = coeff * fact(p.first) % mod;
  }

  long ret = way * fact(cnt - 1) % mod * inv(coeff) % mod;
  sz[from] = cnt;
  dp[from] = ret;
  return make_pair(cnt, ret);
}

void rec(int from, int prev, pair<long, long> inher) {
  int m = g[from].size();
  long cnt = 1;
  vector<long> ways;
  for (int to : g[from]) {
    if (to == prev) {
      cnt += inher.first;
      ways.push_back(inher.second * inv(fact(inher.first)) % mod);
    } else {
      cnt += sz[to];
      ways.push_back(dp[to] * inv(fact(sz[to])) % mod);
    }
  }

  vector<long> l(m + 1, 1);
  vector<long> r(m, 1);
  for (int i = 1; i <= m; ++i)
    l[i] = l[i - 1] * ways[i - 1] % mod;
  for (int i = m - 2; i > 0; --i)
    r[i] = r[i + 1] * ways[i + 1] % mod;

  for (int i = 0; i < m; ++i) {
    int to = g[from][i];
    if (to == prev)
      continue;
    long ncnt = cnt - sz[to];
    long nret = l[i] * r[i] % mod * fact(ncnt - 1) % mod;
    rec(to, from, make_pair(ncnt, nret));
  }
  ans[from] = fact(n - 1) * l[m] % mod;
}

int main() {
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  pre();
  prerec(0, -1);
  rec(0, -1, make_pair(0, 1));

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