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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

constexpr ll MOD = 1000000007;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

vector<int> g[200000];
ll sub_size[200000];
ll finv[200010];
ll fact[200010];
ll ans[200000];
ll final_ans[200000];

ll inv(ll x) {
  ll e = MOD - 2;
  ll res = 1;
  while (e) {
    if (e & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    e >>= 1;
  }
  return res;
}

ll dfs(int v, int p) {
  sub_size[v] = 1;
  for (int nv : g[v]) {
    if (nv == p)
      continue;
    sub_size[v] += dfs(nv, v);
  }
  return sub_size[v];
}

ll solve1(int v, int p) {
  ans[v] = fact[sub_size[v] - 1];
  for (int nv : g[v]) {
    if (nv == p)
      continue;
    ans[v] = ans[v] * solve1(nv, v) % MOD * finv[sub_size[nv]] % MOD;
  }
  return ans[v];
}

void solve2(int v, int p) {
  final_ans[v] = fact[sub_size[v] - 1];
  for (int nv : g[v])
    final_ans[v] = final_ans[v] * ans[nv] % MOD * finv[sub_size[nv]] % MOD;

  for (int nv : g[v]) {
    if (nv == p)
      continue;
    // nvを根とした状態にする
    ans[v] = ans[v] * inv(ans[nv]) % MOD * fact[sub_size[nv]] % MOD *
             inv(fact[sub_size[v] - 1]) % MOD *
             fact[sub_size[v] - 1 - sub_size[nv]] % MOD;
    ans[nv] = ans[nv] * inv(fact[sub_size[nv] - 1]) % MOD;
    sub_size[v] -= sub_size[nv];
    sub_size[nv] += sub_size[v];
    ans[nv] = ans[nv] * fact[sub_size[nv] - 1] % MOD * ans[v] % MOD *
              finv[sub_size[v]] % MOD;

    solve2(nv, v);
    ans[nv] = ans[nv] *
              inv(fact[sub_size[nv] - 1] % MOD * ans[v] % MOD *
                  finv[sub_size[v]] % MOD) %
              MOD;
    sub_size[nv] -= sub_size[v];
    sub_size[v] += sub_size[nv];
    ans[nv] = ans[nv] * fact[sub_size[nv] - 1] % MOD;
    ans[v] = ans[v] * ans[nv] % MOD * inv(fact[sub_size[nv]]) % MOD *
             fact[sub_size[v] - 1] % MOD *
             inv(fact[sub_size[v] - 1 - sub_size[nv]]) % MOD;
  }
}

int main() {
  int n, a, b;
  cin >> n;
  rep(i, 0, n - 1) {
    cin >> a >> b;
    --a, --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  fact[0] = finv[0] = 1;
  rep(i, 1, n + 5) {
    fact[i] = fact[i - 1] * i % MOD;
    finv[i] = finv[i - 1] * inv(i) % MOD;
  }

  dfs(0, 0);
  solve1(0, 0);
  solve2(0, 0);
  rep(i, 0, n) cout << final_ans[i] << '\\n';

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)

constexpr ll MOD = 1000000007;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

vector<int> g[100000];
ll sub_size[100000];
ll finv[100000];
ll fact[100000];
ll ans[100000];
ll final_ans[100000];

ll inv(ll x) {
  ll e = MOD - 2;
  ll res = 1;
  while (e) {
    if (e & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    e >>= 1;
  }
  return res;
}

ll dfs(int v, int p) {
  sub_size[v] = 1;
  for (int nv : g[v]) {
    if (nv == p)
      continue;
    sub_size[v] += dfs(nv, v);
  }
  return sub_size[v];
}

ll solve1(int v, int p) {
  ans[v] = fact[sub_size[v] - 1];
  for (int nv : g[v]) {
    if (nv == p)
      continue;
    ans[v] = ans[v] * solve1(nv, v) % MOD * finv[sub_size[nv]] % MOD;
  }
  return ans[v];
}

void solve2(int v, int p) {
  final_ans[v] = fact[sub_size[v] - 1];
  for (int nv : g[v])
    final_ans[v] = final_ans[v] * ans[nv] % MOD * finv[sub_size[nv]] % MOD;

  for (int nv : g[v]) {
    if (nv == p)
      continue;
    // nvを根とした状態にする
    ans[v] = ans[v] * inv(ans[nv]) % MOD * fact[sub_size[nv]] % MOD *
             inv(fact[sub_size[v] - 1]) % MOD *
             fact[sub_size[v] - 1 - sub_size[nv]] % MOD;
    ans[nv] = ans[nv] * inv(fact[sub_size[nv] - 1]) % MOD;
    sub_size[v] -= sub_size[nv];
    sub_size[nv] += sub_size[v];
    ans[nv] = ans[nv] * fact[sub_size[nv] - 1] % MOD * ans[v] % MOD *
              finv[sub_size[v]] % MOD;

    solve2(nv, v);
    ans[nv] = ans[nv] *
              inv(fact[sub_size[nv] - 1] % MOD * ans[v] % MOD *
                  finv[sub_size[v]] % MOD) %
              MOD;
    sub_size[nv] -= sub_size[v];
    sub_size[v] += sub_size[nv];
    ans[nv] = ans[nv] * fact[sub_size[nv] - 1] % MOD;
    ans[v] = ans[v] * ans[nv] % MOD * inv(fact[sub_size[nv]]) % MOD *
             fact[sub_size[v] - 1] % MOD *
             inv(fact[sub_size[v] - 1 - sub_size[nv]]) % MOD;
  }
}

int main() {
  int n, a, b;
  cin >> n;
  rep(i, 0, n - 1) {
    cin >> a >> b;
    --a, --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  fact[0] = finv[0] = 1;
  rep(i, 1, n) {
    fact[i] = fact[i - 1] * i % MOD;
    finv[i] = finv[i - 1] * inv(i) % MOD;
  }

  dfs(0, 0);
  solve1(0, 0);
  solve2(0, 0);
  rep(i, 0, n) cout << final_ans[i] << '\\n';

  return 0;
}