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

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

vector<long long> g[200000];
vector<int> f1[200000];
long long f2[200000];
long long ans[200000];

const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
int n;

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

long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int dfs1(int cur, int p) {
  int tmp = 0;
  for (int to : g[cur]) {
    if (to == p) {
      continue;
    }
    long long tmp2 = dfs1(to, cur);
    f1[cur].push_back(tmp2);
    tmp += tmp2;
  }
  return tmp + 1;
}
long long dfs2(int cur, int p) {
  long long tmp = 1;
  for (int to : g[cur]) {
    if (to == p) {
      continue;
    }
    tmp *= dfs2(to, cur);
    tmp %= MOD;
  }
  int sum = 0;
  for (int x : f1[cur]) {
    sum += x;
  }
  for (int x : f1[cur]) {
    tmp *= COM(sum, x);
    tmp %= MOD;
    sum -= x;
  }
  f2[cur] = tmp;
  return tmp;
}
long long calc_inv(long long n) {
  if (n < MAX) {
    return inv[n];
  }
  return MOD - calc_inv(MOD % n) * (MOD / n) % MOD;
}
void dfs3(int cur, int p) {
  if (p != -1) {
    long long tmp = f2[cur];
    ans[cur] = f2[cur];
    long long sum = 0;
    for (int x : f1[cur]) {
      sum += x;
    }
    ans[cur] *= COM(n - 1, sum);
    ans[cur] %= MOD;
    ans[cur] *= ans[p];
    ans[cur] %= MOD;
    ans[cur] *= calc_inv(f2[cur]);
    ans[cur] %= MOD;
    ans[cur] *= calc_inv(COM(n - 1, sum + 1));
    ans[cur] %= MOD;
  }
  for (int to : g[cur]) {
    if (p != to) {
      dfs3(to, cur);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  COMinit();
  dfs1(0, -1);
  dfs2(0, -1);
  ans[0] = f2[0];
  dfs3(0, -1);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}

Error: 0

Fixed submission:

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

vector<long long> g[200000];
vector<int> f1[200000];
long long f2[200000];
long long ans[200000];

const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
int n;

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

long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int dfs1(int cur, int p) {
  int tmp = 0;
  for (int to : g[cur]) {
    if (to == p) {
      continue;
    }
    long long tmp2 = dfs1(to, cur);
    f1[cur].push_back(tmp2);
    tmp += tmp2;
  }
  return tmp + 1;
}
long long dfs2(int cur, int p) {
  long long tmp = 1;
  for (int to : g[cur]) {
    if (to == p) {
      continue;
    }
    tmp *= dfs2(to, cur);
    tmp %= MOD;
  }
  int sum = 0;
  for (int x : f1[cur]) {
    sum += x;
  }
  for (int x : f1[cur]) {
    tmp *= COM(sum, x);
    tmp %= MOD;
    sum -= x;
  }
  f2[cur] = tmp;
  return tmp;
}

void dfs3(int cur, int p) {
  if (p != -1) {
    long long tmp = f2[cur];
    ans[cur] = f2[cur];
    long long sum = 0;
    for (int x : f1[cur]) {
      sum += x;
    }
    ans[cur] *= COM(n - 1, sum);
    ans[cur] %= MOD;
    ans[cur] *= ans[p] * inv[f2[cur]];
    ans[cur] %= MOD;
    ans[cur] *= inv[COM(n - 1, sum + 1)];
    ans[cur] %= MOD;
  }
  for (int to : g[cur]) {
    if (p != to) {
      dfs3(to, cur);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  COMinit();
  dfs1(0, -1);
  dfs2(0, -1);
  ans[0] = f2[0];
  dfs3(0, -1);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}