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

#include <bits/stdc++.h>

using namespace std;

struct modular_num {
  int d, md = 1e9 + 7;

  modular_num(int x = 0, int _mod = 1e9 + 7) {
    d = normal(x);
    md = _mod;
  }

  int normal(int x) {
    x %= md;
    while (x < 0)
      x += md;
    return x;
  }

  modular_num inverse(modular_num t) { return t ^ (md - 2); }

  friend istream &operator>>(istream &in, modular_num &t) {
    in >> t();
    return in;
  }

  friend ostream &operator<<(ostream &out, modular_num t) {
    out << t();
    return out;
  }

  int &operator()() { return d; }

  modular_num operator+(modular_num t) { return normal(d + t()); }

  modular_num &operator+=(modular_num t) { return *this = *this + t; }

  modular_num operator-(modular_num t) { return normal(d - t()); }

  modular_num &operator-=(modular_num t) { return *this = *this - t; }

  modular_num operator*(modular_num t) { return 1ll * d * t() % md; }

  modular_num &operator*=(modular_num t) { return *this = *this * t; }

  modular_num operator^(int power) {
    modular_num base = *this, res = 1;
    power %= md - 1;
    while (power > 0) {
      if (power & 1)
        res *= base;
      base *= base;
      power >>= 1;
    }
    return res;
  }

  modular_num &operator^=(int power) { return *this = *this ^ power; }

  modular_num operator/(modular_num t) { return 1ll * d * inverse(t)() % md; }

  modular_num &operator/=(modular_num t) { return *this = *this / t; }

  modular_num &operator=(modular_num t) {
    d = t();
    return *this;
  }

  bool operator==(modular_num t) { return d == t(); }

  bool operator!=(modular_num t) { return d != t(); }
};

using mint = modular_num;

const int maxn = 2e5 + 10;
int sz[maxn], n;
vector<int> adj[maxn];
mint dp[maxn], pd[maxn], fac[maxn];

void precalc() {
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = fac[i - 1] * i;
}

void dfs(int u, int par = -1) {
  sz[u] = 1;
  for (int v : adj[u]) {
    if (v != par) {
      dfs(v, u);
      sz[u] += sz[v];
    }
  }
  pd[u] = fac[sz[u] - 1];
  for (int v : adj[u]) {
    if (v != par) {
      (pd[u] *= pd[v]) /= fac[sz[v]];
    }
  }
}

void ufs(int u, int par = -1) {
  if (par == -1) {
    dp[u] = pd[u];
  } else {
    dp[u] = mint(1) / fac[n - sz[u]];
    for (int v : adj[u])
      if (v != par)
        (dp[u] *= pd[v]) /= fac[sz[v]];
    dp[u] *= dp[par];
    dp[u] *= fac[n - 1 - sz[u]];
    (dp[u] /= pd[u]) *= fac[sz[u]];
  }

  for (int v : adj[u])
    if (v != par)
      ufs(v, u);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;

  precalc();
  for (int i = 0, u, v; i < n - 1; i++) {
    cin >> u >> v;
    --u, --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(0);
  ufs(0);
  for (int i = 0; i < n; i++) {
    cout << dp[i] << '\\n';
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct modular_num {
  int d, md = 1e9 + 7;

  modular_num(int x = 0, int _mod = 1e9 + 7) {
    d = normal(x);
    md = _mod;
  }

  int normal(int x) {
    x %= md;
    while (x < 0)
      x += md;
    return x;
  }

  modular_num inverse(modular_num t) { return t ^ (md - 2); }

  friend istream &operator>>(istream &in, modular_num &t) {
    in >> t();
    return in;
  }

  friend ostream &operator<<(ostream &out, modular_num t) {
    out << t();
    return out;
  }

  int &operator()() { return d; }

  modular_num operator+(modular_num t) { return normal(d + t()); }

  modular_num &operator+=(modular_num t) { return *this = *this + t; }

  modular_num operator-(modular_num t) { return normal(d - t()); }

  modular_num &operator-=(modular_num t) { return *this = *this - t; }

  modular_num operator*(modular_num t) { return 1ll * d * t() % md; }

  modular_num &operator*=(modular_num t) { return *this = *this * t; }

  modular_num operator^(int power) {
    modular_num base = *this, res = 1;
    power %= md - 1;
    while (power > 0) {
      if (power & 1)
        res *= base;
      base *= base;
      power >>= 1;
    }
    return res;
  }

  modular_num &operator^=(int power) { return *this = *this ^ power; }

  modular_num operator/(modular_num t) { return 1ll * d * inverse(t)() % md; }

  modular_num &operator/=(modular_num t) { return *this = *this / t; }

  modular_num &operator=(modular_num t) {
    d = t();
    return *this;
  }

  bool operator==(modular_num t) { return d == t(); }

  bool operator!=(modular_num t) { return d != t(); }
};

using mint = modular_num;

const int maxn = 5000;
int sz[maxn], n;
vector<int> adj[maxn];
mint dp[maxn], pd[maxn], fac[maxn];

void precalc() {
  fac[0] = 1;
  for (int i = 1; i < maxn; i++)
    fac[i] = fac[i - 1] * i;
}

void dfs(int u, int par = -1) {
  sz[u] = 1;
  for (int v : adj[u]) {
    if (v != par) {
      dfs(v, u);
      sz[u] += sz[v];
    }
  }
  pd[u] = fac[sz[u] - 1];
  for (int v : adj[u]) {
    if (v != par) {
      (pd[u] *= pd[v]) /= fac[sz[v]];
    }
  }
}

void ufs(int u, int par = -1) {
  if (par == -1) {
    dp[u] = pd[u];
  } else {
    dp[u] = mint(1) / fac[n - sz[u]];
    for (int v : adj[u])
      if (v != par)
        (dp[u] *= pd[v]) /= fac[sz[v]];
    dp[u] *= dp[par];
    dp[u] *= fac[n - 1 - sz[u]];
    (dp[u] /= pd[u]) *= fac[sz[u]];
  }

  for (int v : adj[u])
    if (v != par)
      ufs(v, u);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;

  precalc();
  for (int i = 0, u, v; i < n - 1; i++) {
    cin >> u >> v;
    --u, --v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(0);
  ufs(0);
  for (int i = 0; i < n; i++) {
    cout << dp[i] << '\\n';
  }

  return 0;
}