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

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define F first
#define S second

#define ONLINE_JUDGE 1

using namespace std;

const int mod = 1e9 + 7;
const int MAX = 200020;

vector<ll> fact(MAX), ifac(MAX);
vector<ll> invs(MAX);
vector<vector<int>> g(MAX);
vector<ll> dp(MAX);
vector<ll> sz(MAX);

ll pwr(ll a, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a % mod;
  ll q = pwr(a, n >> 1);
  q = 1ll * q * q % mod;
  if (n & 1)
    q = 1ll * q * a % mod;
  return q;
}

ll inv(ll n) { return pwr(n, mod - 2); }

void init() {
  fact[0] = 1, invs[1] = 1, ifac[0] = 1;
  for (int i = 1; i < MAX; i++)
    fact[i] = 1ll * fact[i - 1] * i % mod;
  for (int i = 2; i < MAX; i++)
    invs[i] = -invs[mod % i] * 1ll * (mod / i) % mod;
  for (int i = 1; i < MAX; i++)
    ifac[i] = 1ll * ifac[i - 1] * invs[i] % mod;
}

void dfs1(int v, int p = -1) {
  dp[v] = 1;
  sz[v] = 1;
  for (int u : g[v]) {
    if (u == p)
      continue;
    dfs1(u, v);
    sz[v] += sz[u];
    dp[v] = (1ll * dp[v] * dp[u] % mod) * ifac[sz[u]] % mod;
  }
  dp[v] = 1ll * dp[v] * fact[sz[v] - 1] % mod;
}

void dfs2(int v, int p = -1) {
  for (int u : g[v]) {
    if (u == p)
      continue;
    ll tmp = (dp[v] * fact[sz[u]] % mod) * inv(dp[u]) % mod;
    tmp = ifac[sz[1] - 1] * tmp % mod;
    tmp = fact[sz[1] - sz[u] - 1] * tmp % mod;
    ll tmp_n = ifac[sz[u] - 1] * dp[u] % mod;
    tmp_n = tmp_n * fact[sz[1] - 1] % mod;
    dp[u] = tmp_n * tmp % mod;
    dp[u] = dp[u] * ifac[sz[1] - sz[u]] % mod;
    dfs2(u, v);
  }
}

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs1(1);
  //	cout << dp[1] + mod << "\\n";
  dfs2(1);
  for (int i = 1; i <= n; i++) {
    cout << (dp[i] + mod) % mod << "\\n";
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  int t = 1;
  // cin >> t;
  init();
  while (t--)
    solve();
  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define F first
#define S second

// #define ONLINE_JUDGE 1

using namespace std;

const int mod = 1e9 + 7;
const int MAX = 200020;

vector<ll> fact(MAX), ifac(MAX);
vector<ll> invs(MAX);
vector<vector<int>> g(MAX);
vector<ll> dp(MAX);
vector<ll> sz(MAX);

ll pwr(ll a, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a % mod;
  ll q = pwr(a, n >> 1);
  q = 1ll * q * q % mod;
  if (n & 1)
    q = 1ll * q * a % mod;
  return q;
}

ll inv(ll n) { return pwr(n, mod - 2); }

void init() {
  fact[0] = 1, invs[1] = 1, ifac[0] = 1;
  for (int i = 1; i < MAX; i++)
    fact[i] = 1ll * fact[i - 1] * i % mod;
  for (int i = 2; i < MAX; i++)
    invs[i] = -invs[mod % i] * 1ll * (mod / i) % mod;
  for (int i = 1; i < MAX; i++)
    ifac[i] = 1ll * ifac[i - 1] * invs[i] % mod;
}

void dfs1(int v, int p = -1) {
  dp[v] = 1;
  sz[v] = 1;
  for (int u : g[v]) {
    if (u == p)
      continue;
    dfs1(u, v);
    sz[v] += sz[u];
    dp[v] = (1ll * dp[v] * dp[u] % mod) * ifac[sz[u]] % mod;
  }
  dp[v] = 1ll * dp[v] * fact[sz[v] - 1] % mod;
}

void dfs2(int v, int p = -1) {
  for (int u : g[v]) {
    if (u == p)
      continue;
    ll tmp = (dp[v] * fact[sz[u]] % mod) * inv(dp[u]) % mod;
    tmp = ifac[sz[1] - 1] * tmp % mod;
    tmp = fact[sz[1] - sz[u] - 1] * tmp % mod;
    ll tmp_n = ifac[sz[u] - 1] * dp[u] % mod;
    tmp_n = tmp_n * fact[sz[1] - 1] % mod;
    dp[u] = tmp_n * tmp % mod;
    dp[u] = dp[u] * ifac[sz[1] - sz[u]] % mod;
    dfs2(u, v);
  }
}

void solve() {
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs1(1);
  //	cout << dp[1] + mod << "\\n";
  dfs2(1);
  for (int i = 1; i <= n; i++) {
    cout << (dp[i] + mod) % mod << "\\n";
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  int t = 1;
  // cin >> t;
  init();
  while (t--)
    solve();
  return 0;
}