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

#include <bits/stdc++.h>

#define f(i, a, b) for (ll i = a; i < (ll)b; i++)
#define af(i, a, b) for (ll i = a; i >= (ll)b; i--)
#define rep(i, a, b, k) for (ll i = a; i < b; i += k)
#define arep(i, a, b, k) for (ll i = a; i >= b; i -= k)
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort(a.begin(), a.end())
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define inter                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);                                                            \\
  cout.tie(nullptr)
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;

const ll MAX = 3e5 + 100;

ll dp[MAX], n, ans[MAX], siz[MAX], fac[MAX], mod = 1e9 + 7;
vi adj[MAX];

ll bp(ll a, ll e) {
  if (e == 0)
    return 1;
  if (e & 1)
    return (bp(a, e - 1) * a) % mod;
  ll x = bp(a, e >> 1);
  return (x * x) % mod;
}

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

void dfs(int u, int f) {
  dp[u] = 1, siz[u] = 1;
  f(i, 0, adj[u].size()) {
    ll v = adj[u][i];
    if (v == f)
      continue;
    dfs(v, u);
    siz[u] += siz[v];
    (dp[u] *= dp[v]) %= mod;
    (dp[u] *= inv(fac[siz[v]])) %= mod;
  }
  (dp[u] *= fac[siz[u] - 1]) %= mod;
}

void dfs1(int u, int f) {
  f(i, 0, adj[u].size()) {
    ll v = adj[u][i];
    if (v == f)
      continue;
    ans[v] = (ans[u] * ((siz[v] * inv(n - siz[v])) % mod)) % mod;
    dfs1(v, u);
  }
}

int main() {
  fastio;
  ll u, v;
  fac[0] = 1;
  f(i, 1, MAX)(fac[i] = i * fac[i - 1]) %= mod;
  cin >> n;
  f(i, 1, n) {
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, 0);
  ans[1] = dp[1];
  dfs1(1, 0);
  f(i, 1, n + 1) cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag,
             trie_prefix_search_node_update>
    pref_trie;
typedef unsigned long long BITMASK;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<long long, long long> pll;
#define has_bit(bit_mask, x) ((bit_mask) & (1ULL << (x)))
#define turn_on_bit(bit_mask, x) (bit_mask |= (1ULL << (x)))
#define turn_off_bit(bit_mask, x) (bit_mask &= (~(1ULL << (x))))
#define smallest_on_bit(bit_mask) (__builtin_ctzint((bit_mask) & (-(bit_mask))))
#define CLOCK_START clock_t chrono_clk_beg = clock()
#define CLOCK_END                                                              \\
  clock_t chrono_clk_end = clock();                                            \\
  cerr << (double(chrono_clk_end - chrono_clk_beg) / CLOCKS_PER_SEC) << " sec"
#define bug(args...)                                                           \\
  cerr << __LINE__ << ">> ", err(new istringstream(string(#args)), args),      \\
      cerr << '\\n'
#define decToBin(name, num) string name = bitset<8>(num).to_string();
#define binToDec(name, binary)                                                 \\
  unsigned long name = bitset<8>(binary).to_ulong();
#define radToDeg(name, radians) double name = radians * 180 / M_PI;
#define all(x) x.begin(), x.end()
#define removeDuplicates(vec)                                                  \\
  sort(vec.begin(), vec.end());                                                \\
  vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define printMat(mat)                                                          \\
  for (auto &x : mat) {                                                        \\
    for (auto &y : x) {                                                        \\
      if (y < INF)                                                             \\
        cout << setw(7) << y;                                                  \\
      else                                                                     \\
        cout << setw(7) << "-";                                                \\
    }                                                                          \\
    cout << '\\n';                                                              \\
  }
#define fillMat(mat, n, m, x)                                                  \\
  for (int i = 0; i < n; ++i)                                                  \\
    for (int j = 0; j < m; ++j)                                                \\
      mat[i][j] = x;
#define printArr(arr)                                                          \\
  for (auto &x : arr)                                                          \\
    cout << x << " ";                                                          \\
  cout << '\\n';
#define arrReplace(arr, from, to)                                              \\
  for (auto &x : arr)                                                          \\
    if (x == from)                                                             \\
      x = to;
#define INF (1LL << 30)
#define INF32 (1LL << 62)
#define F first
#define S second
#define M_PI 3.14159265358979323846
inline int ceil(int a, int b) { return (a + b - 1) / b; }
void err(istringstream *iss) {}
template <typename T, typename... Args>
void err(istringstream *iss, const T &_val, const Args &...args) {
  string _name;
  *iss >> _name;
  if (_name.back() == ',')
    _name.pop_back();
  cerr << _name << " = " << _val << "; ", err(iss, args...);
}
int str_replace(string &str, const string &from, const string &to,
                int limit = -1) {
  if (from.empty())
    return 0;
  size_t start_pos = 0;
  int cnt = 0;
  while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
    str.replace(start_pos, from.length(), to);
    start_pos += to.length();
    cnt++;
    if (cnt == limit)
      break;
  }
  return cnt;
}
#define MOD 1000000007
#define int ll

const int MAXN = 2 * 1e5;
int fact[MAXN + 1], inv[MAXN + 1];
ll powMod(ll a, ll b) {
  ll x = 1;
  while (b > 0) {
    if (b & 1)
      x = (x * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return x;
}
ll multiply(ll x, ll y) { return ((x % MOD) * (y % MOD)) % MOD; }
ll divide(ll x, ll y) { return ((x % MOD) * inv[y % MOD]) % MOD; }

vector<int> adj[MAXN + 1];
int cnt[MAXN + 1], dp[MAXN + 1];
void precompute(int u, int par = -1) {
  cnt[u] = 1, dp[u] = 1;
  for (auto &v : adj[u]) {
    if (v == par)
      continue;
    precompute(v, u);
    cnt[u] += cnt[v];
    dp[u] = multiply(dp[u], divide(dp[v], fact[cnt[v]]));
  }
  dp[u] = multiply(dp[u], fact[cnt[u] - 1]);
}
void reroot(int u, int par = -1) {
  cout << dp[u] << '\\n';
  for (auto &v : adj[u]) {
    if (v == par)
      continue;
    int prev_dp_u = dp[u], prev_dp_v = dp[v], prev_cnt_u = cnt[u],
        prev_cnt_v = cnt[v];
    dp[u] = divide(dp[u], dp[v]);
    dp[u] = multiply(dp[u], fact[cnt[v]]);
    dp[u] = divide(dp[u], fact[cnt[u] - 1]);
    dp[v] = divide(dp[v], fact[cnt[v] - 1]);
    cnt[u] -= cnt[v];
    cnt[v] += cnt[u];
    dp[u] = multiply(dp[u], fact[cnt[u] - 1]);
    dp[v] = multiply(dp[v], dp[u]);
    dp[v] = multiply(dp[v], fact[cnt[v] - 1]);
    dp[v] = divide(dp[v], fact[cnt[u]]);

    reroot(v, u);

    dp[u] = prev_dp_u, dp[v] = prev_dp_v, cnt[u] = prev_cnt_u,
    cnt[v] = prev_cnt_v;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  fact[0] = 1;
  for (int i = 1; i <= MAXN; ++i)
    fact[i] = (i * fact[i - 1]) % MOD;
  for (int i = 0; i <= MAXN; ++i)
    inv[i] = powMod(i, MOD - 2);
  int n;
  cin >> n;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  precompute(1);
  reroot(1);
  return 0;
}