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

/*
 * short          2^15-1 ~ 3e4
 * int, long x86  2^31-1 ~ 2e9
 * long long      2^63-1 ~ 9e18
 */
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define fr(a, b) for (int a = 0; a < b; ++a)
using namespace std;
typedef long long ll;
const int inf = numeric_limits<int>::max();
void print() { cerr << endl; }
template <typename T, typename... Ts> void print(T &&a, Ts &&...etc) {
  cerr << a << ' ';
  print(etc...);
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> &p) {
  s << "<" << p.x << ", " << p.y << ">";
  return s;
} // ver. 2020-02-17
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  for (auto &x : v)
    s << x << ", ";
  if (!v.empty())
    s << "\\b\\b";
  s << "]";
  return s;
}
struct io {
  io() {
    ios_base::Init i;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  }
} io;

int n;
vector<vector<int>> g;
vector<ll> dp;
vector<ll> sz;
vector<ll> ans;
const int N = 200001;
const ll mod = 1000000007;
ll f[N + 1];

ll poww(ll a, ll n) {
  if (n == 0)
    return 1;
  if (n % 2) {
    return (a * poww(a, n - 1)) % mod;
  }
  ll a2 = poww(a, n / 2);
  return (a2 * a2) % mod;
}

ll inv(ll a) { return poww(a, mod - 2); }

void dfs_init(int v, int parent) {
  sz[v] = 1;
  dp[v] = 1;
  ll mul = 1;
  ll sum = 0;
  ll den = 1;
  // dp_i = (Σ sz_i)! * Π( dp_i / (sz_i)! )
  for (int u : g[v]) {
    if (u != parent) {
      dfs_init(u, v);
      sz[v] = (sz[v] + sz[u]) % mod;
      sum = (sum + sz[u]) % mod;
      mul = (mul * dp[u]) % mod;
      den = (den * f[sz[u]]) % mod;
    }
  }
  dp[v] = ((mul * f[sum]) % mod) * inv(den) % mod;
}

void reroot(int r, int v) {
  // update DP when the root moves from r to v
  // r and v must be neighbours
  if (r == -1)
    return;

  // new size of r-subtree:
  sz[r] = n - sz[v];

  // remove v-subtree from r:
  dp[r] *= inv((f[n - 1] * dp[v]) % mod);
  dp[r] %= mod;
  dp[r] *= (f[n - 1 - sz[v]] * f[sz[v]]) % mod;
  dp[r] %= mod;

  // add r-subtree to v:
  dp[v] *= (f[n - 1] * dp[r]) % mod;
  dp[v] %= mod;
  dp[v] *= inv((f[sz[v] - 1] * f[sz[r]]) % mod);
  dp[v] %= mod;

  // v is the new root:
  sz[v] = n;
}

void dfs_reroot(int v, int parent) {
  ans[v] = dp[v];
  for (int u : g[v]) {
    if (u != parent) {
      reroot(v, u);
      dfs_reroot(u, v);
      reroot(u, v);
    }
  }
}

int main() {
  f[0] = 1;
  for (ll i = 1; i <= N; ++i) {
    f[i] = f[i - 1] * i % mod;
  }

  cin >> n;
  g = vector<vector<int>>(n);
  dp.assign(n, 1);
  sz.assign(n, 0);
  ans.assign(n, 0);

  int u, v;
  fr(i, n - 1) {
    cin >> u >> v;
    u--, v--;
    g[u].pb(v);
    g[v].pb(u);
  }

  dfs_init(0, -1);

  dfs_reroot(0, -1);

  fr(k, n) { cout << ans[k] << '\\n'; }
}


Error: 0

Fixed submission:

/*
 * short          2^15-1 ~ 3e4
 * int, long x86  2^31-1 ~ 2e9
 * long long      2^63-1 ~ 9e18
 */
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define fr(a, b) for (int a = 0; a < b; ++a)
using namespace std;
typedef long long ll;
const int inf = numeric_limits<int>::max();
void print() { cerr << endl; }
template <typename T, typename... Ts> void print(T &&a, Ts &&...etc) {
  cerr << a << ' ';
  print(etc...);
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> &p) {
  s << "<" << p.x << ", " << p.y << ">";
  return s;
} // ver. 2020-02-17
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  for (auto &x : v)
    s << x << ", ";
  if (!v.empty())
    s << "\\b\\b";
  s << "]";
  return s;
}
struct io {
  io() {
    ios_base::Init i;
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  }
} io;

int n;
vector<vector<pair<int, int>>> g;
vector<ll> dp;
vector<ll> sz;
const int N = 200001;
const ll mod = 1000000007;
ll inv[N + 1], f[N + 1];

ll poww(ll a, ll n) {
  if (n == 0)
    return 1;
  if (n % 2) {
    return (a * poww(a, n - 1)) % mod;
  }
  ll a2 = poww(a, n / 2);
  return (a2 * a2) % mod;
}

ll dfs(int v, int parent, int edge) {
  int u, e;
  ll mul = 1;
  ll sum = 0;
  ll den = 1;
  for (auto p : g[v]) {
    u = p.x;
    e = p.y;
    if (u != parent) {
      if (dp[e] == -1) {
        dfs(u, v, e);
      }
      sum = (sum + sz[e] + 1) % mod;
      mul = (mul * dp[e]) % mod;
      // print("  edge", e, " size", sz[e], f[sz[e]]);
      den = (den * f[sz[e] + 1]) % mod;
    }
  }
  ll ans = ((mul * f[sum]) % mod) * inv[den] % mod;
  // print("edge", edge, mul, sum, f[sum], den, "ans", ans);
  if (edge != -1) {
    sz[edge] = sum;
    dp[edge] = ans;
  }
  return ans;
}

int main() {
  f[0] = 1;
  for (ll i = 1; i <= N; ++i) {
    f[i] = f[i - 1] * i % mod;
  }
  for (ll i = 1; i <= N; ++i) {
    inv[i] = poww(i, mod - 2);
  }

  cin >> n;
  g = vector<vector<pair<int, int>>>(n);
  dp.assign(2 * n, -1);
  sz.assign(2 * n, -1);
  int u, v;
  fr(i, n - 1) {
    cin >> u >> v;
    u--, v--;
    g[u].pb({v, 2 * i});
    g[v].pb({u, 2 * i + 1});
  }

  fr(k, n) { cout << dfs(k, -1, -1) << '\\n'; }

  return 0;
}