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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
const int mod = 1e9 + 7;
template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using mint = ModInt<mod>;

const int n_max = 100 + 2e5;
pair<int, mint> dp[n_max];
vector<int> g[n_max];
mint fact[n_max];
mint inv[n_max];

void dfs1(int cv, int p) {
  dp[cv] = {1, 1};
  int tot = 0;
  mint kei = 1;
  for (auto i : g[cv]) {
    if (i == p)
      continue;
    dfs1(i, cv);
    tot += dp[i].first;
    kei *= dp[i].second;
    kei *= fact[tot] * inv[tot - dp[i].first] * inv[dp[i].first];
  }
  tot++;
  dp[cv] = {tot, kei};
}
int n;
mint ans[n_max];
void dfs2(int cv, int p) {
  ans[cv] = 1;
  mint kei = 1;
  int tot = 0;
  for (auto i : g[cv]) {
    tot += dp[i].first;
    kei *= dp[i].second;
    kei *= fact[tot] * inv[tot - dp[i].first] * inv[dp[i].first];
  }

  ans[cv] = kei;
  for (auto i : g[cv]) {
    if (i == p)
      continue;
    // dp書き換え
    mint nkei = kei;
    int ntot = n - dp[i].first;
    nkei /= dp[i].second;
    nkei *= fact[n - 1 - dp[i].first] * fact[dp[i].first] * inv[n - 1];
    dp[cv] = {ntot, nkei};
    dfs2(i, cv);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].pb(b);
    g[b].pb(a);
  }
  fact[0] = 1;
  rep1(i, n_max - 1) fact[i] = fact[i - 1] * i;
  rep(i, n_max) inv[i] = ((mint)1) / fact[i];
  dfs1(0, -1);
  //  rep(i,n) cout << dp[i].first << " " << dp[i].second << endl;
  dfs2(0, -1);
  rep(i, n) { cout << ans[i] << endl; }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
const int mod = 1e9 + 7;
template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using mint = ModInt<mod>;

const int n_max = 100 + 2e5;
pair<int, mint> dp[n_max];
vector<int> g[n_max];
mint fact[n_max];
mint inv[n_max];

void dfs1(int cv, int p) {
  dp[cv] = {1, 1};
  int tot = 0;
  mint kei = 1;
  for (auto i : g[cv]) {
    if (i == p)
      continue;
    dfs1(i, cv);
    tot += dp[i].first;
    kei *= dp[i].second;
    kei *= fact[tot] * inv[tot - dp[i].first] * inv[dp[i].first];
  }
  tot++;
  dp[cv] = {tot, kei};
}
int n;
mint ans[n_max];
void dfs2(int cv, int p) {
  ans[cv] = 1;
  mint kei = 1;
  int tot = 0;
  for (auto i : g[cv]) {
    tot += dp[i].first;
    kei *= dp[i].second;
    kei *= fact[tot] * inv[tot - dp[i].first] * inv[dp[i].first];
  }

  ans[cv] = kei;
  tot = n;

  for (auto i : g[cv]) {
    if (i == p)
      continue;
    // dp書き換え
    mint nkei = kei;
    tot -= dp[i].first;
    nkei /= dp[i].second;
    nkei *= fact[n - 1 - dp[i].first] * fact[dp[i].first] * inv[n - 1];
    dp[cv] = {tot, nkei};
    dfs2(i, cv);
    // dp元に戻す
    tot += dp[i].first;
    dp[cv] = {tot, kei};
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].pb(b);
    g[b].pb(a);
  }
  fact[0] = 1;
  rep1(i, n_max - 1) fact[i] = fact[i - 1] * i;
  rep(i, n_max) inv[i] = ((mint)1) / fact[i];
  dfs1(0, -1);
  //  rep(i,n) cout << dp[i].first << " " << dp[i].second << endl;
  dfs2(0, -1);
  rep(i, n) { cout << ans[i] << endl; }

  return 0;
}