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

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const ll MAXN = 2e5 + 10;
const ll MOD = 1e9 + 7;
struct Node {
  ll ind, nxt;
} node[MAXN * 2];
ll n, head[MAXN], cnt;
ll sz[MAXN], dp[MAXN], ans[MAXN];
ll fac[MAXN] = {1};
void add(ll x, ll y) {
  node[++cnt].ind = y;
  node[cnt].nxt = head[x];
  head[x] = cnt;
}
ll inv(ll a, ll m) {
  if (a == 1)
    return 1;
  return inv(m % a, m) * (m - m / a) % m;
}
void DFS1(ll cur, ll pre) {
  sz[cur] = dp[cur] = 1;
  for (ll i = head[cur]; i; i = node[i].nxt) {
    ll ind = node[i].ind;
    if (ind == pre)
      continue;
    DFS1(ind, cur);
    sz[cur] += sz[ind];
    dp[cur] = dp[cur] * inv(fac[sz[ind]], MOD) % MOD * dp[ind] % MOD;
  }
  dp[cur] = dp[cur] * fac[sz[cur] - 1] % MOD;
}
void DFS2(ll cur, ll pre) {
  ans[cur] = dp[cur];
  for (ll i = head[cur]; i; i = node[i].nxt) {
    ll ind = node[i].ind;
    if (ind == pre)
      continue;
    dp[cur] = dp[cur] * inv(dp[ind], MOD) % MOD * fac[sz[ind]] % MOD *
              fac[sz[cur] - sz[ind] - 1] % MOD * inv(fac[sz[cur] - 1], MOD) %
              MOD;
    sz[cur] -= sz[ind];
    sz[ind] += sz[cur];
    dp[ind] = dp[ind] * dp[cur] % MOD * inv(fac[sz[cur]], MOD) % MOD *
              fac[sz[ind] - 1] % MOD * inv(fac[sz[ind] - sz[cur] - 1], MOD) %
              MOD;
    DFS2(ind, cur);
    dp[ind] = dp[ind] * inv(dp[cur], MOD) % MOD * fac[sz[cur]] % MOD *
              inv(fac[sz[ind] - 1], MOD) % MOD * fac[sz[ind] - sz[cur] - 1] %
              MOD;
    sz[ind] -= sz[cur];
    sz[cur] += sz[ind];
    dp[cur] = dp[cur] * dp[ind] % MOD * inv(fac[sz[ind]], MOD) % MOD *
              inv(fac[sz[cur] - sz[ind] - 1], MOD) % MOD * fac[sz[cur] - 1] %
              MOD;
  }
}
int main() {
  for (ll i = 1; i < MAXN; i++)
    fac[i] = fac[i - 1] * i % MOD;
  scanf("%lld", &n);
  for (ll i = 1; i < n; i++) {
    ll x, y;
    scanf("%lld %lld", &x, &y);
    add(x, y);
    add(y, x);
  }
  DFS1(1, 0);
  DFS2(1, 0);
  for (ll i = 1; i <= n; i++) {
    printf("%lld\\n", ans[i]);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const ll MAXN = 2e5 + 10;
const ll MOD = 1e9 + 7;
struct Node {
  ll ind, nxt;
} node[MAXN];
ll n, head[MAXN], cnt;
ll sz[MAXN], dp[MAXN], ans[MAXN];
ll fac[MAXN] = {1};
void add(ll x, ll y) {
  node[++cnt].ind = y;
  node[cnt].nxt = head[x];
  head[x] = cnt;
}
ll inv(ll a, ll m) {
  if (a == 1)
    return 1;
  return inv(m % a, m) * (m - m / a) % m;
}
void DFS1(ll cur, ll pre) {
  sz[cur] = dp[cur] = 1;
  for (ll i = head[cur]; i; i = node[i].nxt) {
    ll ind = node[i].ind;
    if (ind == pre)
      continue;
    DFS1(ind, cur);
    sz[cur] += sz[ind];
    dp[cur] = dp[cur] * inv(fac[sz[ind]], MOD) % MOD * dp[ind] % MOD;
  }
  dp[cur] = dp[cur] * fac[sz[cur] - 1] % MOD;
}
void DFS2(ll cur, ll pre) {
  ans[cur] = dp[cur];
  for (ll i = head[cur]; i; i = node[i].nxt) {
    ll ind = node[i].ind;
    if (ind == pre)
      continue;
    dp[cur] = dp[cur] * inv(dp[ind], MOD) % MOD * fac[sz[ind]] % MOD *
              fac[sz[cur] - sz[ind] - 1] % MOD * inv(fac[sz[cur] - 1], MOD) %
              MOD;
    sz[cur] -= sz[ind];
    sz[ind] += sz[cur];
    dp[ind] = dp[ind] * dp[cur] % MOD * inv(fac[sz[cur]], MOD) % MOD *
              fac[sz[ind] - 1] % MOD * inv(fac[sz[ind] - sz[cur] - 1], MOD) %
              MOD;
    DFS2(ind, cur);
    dp[ind] = dp[ind] * inv(dp[cur], MOD) % MOD * fac[sz[cur]] % MOD *
              inv(fac[sz[ind] - 1], MOD) % MOD * fac[sz[ind] - sz[cur] - 1] %
              MOD;
    sz[ind] -= sz[cur];
    sz[cur] += sz[ind];
    dp[cur] = dp[cur] * dp[ind] % MOD * inv(fac[sz[ind]], MOD) % MOD *
              inv(fac[sz[cur] - sz[ind] - 1], MOD) % MOD * fac[sz[cur] - 1] %
              MOD;
  }
}
int main() {
  for (ll i = 1; i < MAXN; i++)
    fac[i] = fac[i - 1] * i % MOD;
  scanf("%lld", &n);
  for (ll i = 1; i < n; i++) {
    ll x, y;
    scanf("%lld %lld", &x, &y);
    add(x, y);
    add(y, x);
  }
  DFS1(1, 0);
  DFS2(1, 0);
  for (ll i = 1; i <= n; i++) {
    printf("%lld\\n", ans[i]);
  }
  return 0;
}