Problem p02728 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;

#define lp(var, start, end) for (ll var = start; var < end; ++var)
#define rlp(var, start, end) for (ll var = start; var >= end; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define vld vector<ld>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define vpll vector<pll>
#define vpld vector<pld>
#define all(X) X.begin(), X.end()
#define endl "\n"
#define sz(x) ((ll)((x).size()))
const ll N = 1e5 + 5, inf = 1e18, mod = 1e9 + 7;
ll expo(ll a, ll b, ll mod) {
  ll ans = 1;
  while (b != 0) {
    if ((b & 1) == 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans % mod;
}
vll G[N];
ll fac[N], sub[N], dp[N], invfac[N], ans[N];
ll dfs(ll a, ll p) {
  dp[a] = 1;
  lp(i, 0, sz(G[a])) {
    ll x = G[a][i];
    if (x != p) {
      dfs(x, a);
      sub[a] += sub[x];
      dp[a] *= invfac[sub[x]];
      dp[a] %= mod;
      dp[a] *= dp[x];
      dp[a] %= mod;
    }
  }
  dp[a] *= fac[sub[a]];
  dp[a] %= mod;
  sub[a] += 1;
  // if(dp[a]==0)dp[a]=1;
  // cout<<a<<" "<<dp[a]<<" "<<sub[a]<<endl;
}
ll n;
ll dfs2(ll a, ll p, ll val) {
  // cout<<val<<" "<<a<<endl;
  ans[a] = val;
  ans[a] *= fac[n - 1];
  ans[a] %= mod;
  ans[a] *= invfac[n - sub[a]];
  ans[a] %= mod;
  lp(i, 0, sz(G[a])) {
    ll x = G[a][i];
    if (x != p) {
      ans[a] *= dp[x];
      ans[a] %= mod;
      ans[a] *= invfac[sub[x]];
      ans[a] %= mod;
    }
  }
  lp(i, 0, sz(G[a])) {
    ll x = G[a][i];
    if (x != p) {
      ll val = ans[a];
      val *= invfac[n - 1];
      val %= mod;
      val *= fac[n - 1 - sub[x]];
      val %= mod;
      val *= expo(dp[x], mod - 2, mod);
      val %= mod;
      val *= fac[sub[x]];
      val %= mod;
      // cout<<a<<" "<<x<<" "<<val<<" "<<ans[a]<<endl;
      dfs2(x, a, val);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  fac[0] = 1;
  invfac[0] = 1;
  lp(i, 1, N) {
    fac[i] = (fac[i - 1] * i) % mod;
    invfac[i] = expo(fac[i], mod - 2, mod);
  }
  // ll n;
  cin >> n;
  lp(i, 0, n - 1) {
    ll a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }
  dfs(1, 0);
  dfs2(1, 0, 1);
  lp(i, 1, n + 1) { cout << ans[i] << endl; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;

#define lp(var, start, end) for (ll var = start; var < end; ++var)
#define rlp(var, start, end) for (ll var = start; var >= end; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define vld vector<ld>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define vpll vector<pll>
#define vpld vector<pld>
#define all(X) X.begin(), X.end()
#define endl "\n"
#define sz(x) ((ll)((x).size()))
const ll N = 2e5 + 5, inf = 1e18, mod = 1e9 + 7;
ll expo(ll a, ll b, ll mod) {
  ll ans = 1;
  while (b != 0) {
    if ((b & 1) == 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans % mod;
}
vll G[N];
ll fac[N], sub[N], dp[N], invfac[N], ans[N];
ll dfs(ll a, ll p) {
  dp[a] = 1;
  lp(i, 0, sz(G[a])) {
    ll x = G[a][i];
    if (x != p) {
      dfs(x, a);
      sub[a] += sub[x];
      dp[a] *= invfac[sub[x]];
      dp[a] %= mod;
      dp[a] *= dp[x];
      dp[a] %= mod;
    }
  }
  dp[a] *= fac[sub[a]];
  dp[a] %= mod;
  sub[a] += 1;
  // if(dp[a]==0)dp[a]=1;
  // cout<<a<<" "<<dp[a]<<" "<<sub[a]<<endl;
}
ll n;
ll dfs2(ll a, ll p, ll val) {
  // cout<<val<<" "<<a<<endl;
  ans[a] = val;
  ans[a] *= fac[n - 1];
  ans[a] %= mod;
  ans[a] *= invfac[n - sub[a]];
  ans[a] %= mod;
  lp(i, 0, sz(G[a])) {
    ll x = G[a][i];
    if (x != p) {
      ans[a] *= dp[x];
      ans[a] %= mod;
      ans[a] *= invfac[sub[x]];
      ans[a] %= mod;
    }
  }
  lp(i, 0, sz(G[a])) {
    ll x = G[a][i];
    if (x != p) {
      ll val = ans[a];
      val *= invfac[n - 1];
      val %= mod;
      val *= fac[n - 1 - sub[x]];
      val %= mod;
      val *= expo(dp[x], mod - 2, mod);
      val %= mod;
      val *= fac[sub[x]];
      val %= mod;
      // cout<<a<<" "<<x<<" "<<val<<" "<<ans[a]<<endl;
      dfs2(x, a, val);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  fac[0] = 1;
  invfac[0] = 1;
  lp(i, 1, N) {
    fac[i] = (fac[i - 1] * i) % mod;
    invfac[i] = expo(fac[i], mod - 2, mod);
  }
  // ll n;
  cin >> n;
  lp(i, 0, n - 1) {
    ll a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }
  dfs(1, 0);
  dfs2(1, 0, 1);
  lp(i, 1, n + 1) { cout << ans[i] << endl; }
  return 0;
}