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

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

#define int long long
const int INF = 1001001001;

template <class T, class U> bool chmax(T &a, const U &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  return a > b ? a = b, 1 : 0;
}

template <int mod> class ModInt {
public:
  ModInt() : v(0) {}
  ModInt(int x) : v((x + mod) % mod) {}
  int value() const { return v; }
  const ModInt operator+(const ModInt &r) const {
    return ModInt(this->v + r.v);
  }
  const ModInt operator-(const ModInt &r) const {
    return ModInt(this->v + mod - r.v);
  }
  const ModInt operator*(const ModInt &r) const {
    return ModInt(this->v * r.v);
  }
  const ModInt operator/(const ModInt &r) const { return (*this * (~r)); }
  const ModInt operator^(int k) const { return ModInt(bpow(this->v, k)); }
  const ModInt operator~() const { return ModInt(bpow(this->v, mod - 2)); }
  bool operator==(const ModInt &r) const { return this->v == r.v; }
  bool operator!=(const ModInt &r) const { return this->v != r.v; }
  ModInt &operator+=(const ModInt &r) { return *this = *this + r; }
  ModInt &operator-=(const ModInt &r) { return *this = *this - r; }
  ModInt &operator*=(const ModInt &r) { return *this = *this * r; }
  ModInt &operator/=(const ModInt &r) { return *this = *this * (~r); }

private:
  int v;
  int bpow(int a, int b) const {
    int ret = 1;
    while (b > 0) {
      if (b & 1)
        ret = (ret * a) % mod;
      a = (a * a) % mod;
      b >>= 1;
    }
    return ret;
  }
};
using Mint = ModInt<1000000007>;
struct Factorial {
  vector<Mint> fact, fact_inv;
  Factorial(size_t size) : fact(size), fact_inv(size) {
    fact[0] = fact_inv[0] = 1;
    for (int i = 1; i < size; ++i) {
      fact[i] = fact[i - 1] * i;
      fact_inv[i] = ~fact[i];
    }
  }
  Mint combination(int N, int R) {
    if (N < R)
      return 0;
    return fact[N] * fact_inv[N - R] * fact_inv[R];
  }
  static Mint bin_pow(Mint x, int p) {
    if (x.value() == 0)
      return x;
    Mint prod = 1;
    while (p > 0) {
      if (p & 1)
        prod *= x;
      x *= x;
      p >>= 1;
    }
    return prod;
  }
} fact(200005);

signed main() {
  int N;
  cin >> N;

  vector<vector<int>> G(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);
  }

  vector<Mint> dp(N);
  vector<int> sz(N);
  auto dfs = [&](auto &self, int v, int p) -> void {
    dp[v] = 1;
    sz[v] = 1;
    for (int w : G[v]) {
      if (w == p)
        continue;
      self(self, w, v);
      sz[v] += sz[w];
      dp[v] *= dp[w];
    }
    dp[v] *= fact.fact[sz[v] - 1];
    for (int w : G[v]) {
      if (w == p)
        continue;
      dp[v] *= fact.fact_inv[sz[w]];
    }
  };
  dfs(dfs, 0, -1);

  vector<Mint> ans(N);
  auto dfs2 = [&](auto &self, int v, int p, Mint prop) -> void {
    ans[v] = dp[v];
    ans[v] *= fact.fact_inv[sz[v] - 1] * fact.fact[N - 1];
    ans[v] *= prop;
    ans[v] *= fact.fact_inv[N - sz[v]];
    for (int w : G[v]) {
      if (w == p)
        continue;
      Mint tmp = ans[v] * (~dp[w]) * fact.fact[sz[w]];
      tmp *= fact.fact_inv[N - 1] * fact.fact[N - 1 - sz[w]];
      self(self, w, v, tmp);
    }
  };
  dfs2(dfs2, 0, -1, 1);

  for (auto x : ans) {
    cout << x.value() << endl;
  }
}


Error: 0

Fixed submission:

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

#define int long long
const int INF = 1001001001;

template <class T, class U> bool chmax(T &a, const U &b) {
  return a < b ? a = b, 1 : 0;
}
template <class T, class U> bool chmin(T &a, const U &b) {
  return a > b ? a = b, 1 : 0;
}

template <int mod> class ModInt {
public:
  ModInt() : v(0) {}
  ModInt(int x) : v((x + mod) % mod) {}
  int value() const { return v; }
  const ModInt operator+(const ModInt &r) const {
    return ModInt(this->v + r.v);
  }
  const ModInt operator-(const ModInt &r) const {
    return ModInt(this->v + mod - r.v);
  }
  const ModInt operator*(const ModInt &r) const {
    return ModInt(this->v * r.v);
  }
  const ModInt operator/(const ModInt &r) const { return (*this * (~r)); }
  const ModInt operator^(int k) const { return ModInt(bpow(this->v, k)); }
  const ModInt operator~() const { return ModInt(bpow(this->v, mod - 2)); }
  bool operator==(const ModInt &r) const { return this->v == r.v; }
  bool operator!=(const ModInt &r) const { return this->v != r.v; }
  ModInt &operator+=(const ModInt &r) { return *this = *this + r; }
  ModInt &operator-=(const ModInt &r) { return *this = *this - r; }
  ModInt &operator*=(const ModInt &r) { return *this = *this * r; }
  ModInt &operator/=(const ModInt &r) { return *this = *this * (~r); }

private:
  int v;
  int bpow(int a, int b) const {
    int ret = 1;
    while (b > 0) {
      if (b & 1)
        ret = (ret * a) % mod;
      a = (a * a) % mod;
      b >>= 1;
    }
    return ret;
  }
};
using Mint = ModInt<1000000007>;
struct Factorial {
  vector<Mint> fact, fact_inv;
  Factorial(size_t size) : fact(size), fact_inv(size) {
    fact[0] = fact_inv[0] = 1;
    for (int i = 1; i < size; ++i) {
      fact[i] = fact[i - 1] * i;
      fact_inv[i] = ~fact[i];
    }
  }
  Mint combination(int N, int R) {
    if (N < R)
      return 0;
    return fact[N] * fact_inv[N - R] * fact_inv[R];
  }
  static Mint bin_pow(Mint x, int p) {
    if (x.value() == 0)
      return x;
    Mint prod = 1;
    while (p > 0) {
      if (p & 1)
        prod *= x;
      x *= x;
      p >>= 1;
    }
    return prod;
  }
} fact(100005);

signed main() {
  int N;
  cin >> N;

  vector<vector<int>> G(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);
  }

  vector<Mint> dp(N);
  vector<int> sz(N);
  auto dfs = [&](auto &self, int v, int p) -> void {
    dp[v] = 1;
    sz[v] = 1;
    for (int w : G[v]) {
      if (w == p)
        continue;
      self(self, w, v);
      sz[v] += sz[w];
      dp[v] *= dp[w];
    }
    dp[v] *= fact.fact[sz[v] - 1];
    for (int w : G[v]) {
      if (w == p)
        continue;
      dp[v] *= fact.fact_inv[sz[w]];
    }
  };
  dfs(dfs, 0, -1);

  vector<Mint> ans(N);
  auto dfs2 = [&](auto &self, int v, int p, Mint prop) -> void {
    ans[v] = dp[v];
    ans[v] *= fact.fact_inv[sz[v] - 1] * fact.fact[N - 1];
    ans[v] *= prop;
    ans[v] *= fact.fact_inv[N - sz[v]];
    for (int w : G[v]) {
      if (w == p)
        continue;
      Mint tmp = ans[v] * (~dp[w]) * fact.fact[sz[w]];
      tmp *= fact.fact_inv[N - 1] * fact.fact[N - 1 - sz[w]];
      self(self, w, v, tmp);
    }
  };
  dfs2(dfs2, 0, -1, 1);

  for (auto x : ans) {
    cout << x.value() << endl;
  }
}