Problem p02728 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <vector>
#define ll long long int
#define mp make_pair
#define pb push_back
#define vi vector<int>
using namespace std;
vector<vector<int>> edge(200007);
vector<pair<ll, int>> dp(200007, mp(-1, 0));
vector<ll> ans(200007);
vector<int> euler(400007);
vector<bool> vis(200007, 0);
const ll p = 1e9 + 7;
ll modpower(ll base, ll power, ll mod = p) {
  ll ans = 1;
  base %= p;
  while (power) {
    if (power & 1) {
      ans *= base;
      ans %= p;
    }
    base *= base;
    base %= p;
    power >>= 1;
  }
  return ans;
}
ll fact[200007];
ll invfact[200007];
void computefactorial() {
  fact[0] = 1;
  for (int i = 1; i < 200007; i++) {
    fact[i] = i * fact[i - 1];
    fact[i] %= p;
  }
  invfact[200006] = modpower(fact[200006], p - 2);
  for (int i = 200005; i >= 0; i--) {
    invfact[i] = (i + 1) * invfact[i + 1];
    invfact[i] %= p;
  }
}
void eulerTree(int u, int &indx) {
  vis[u] = 1;
  euler[indx++] = u;
  for (auto it : edge[u]) {
    if (!vis[it]) {
      eulerTree(it, indx);
      euler[indx++] = u;
    }
  }
}
void solve(int u, int v) {
  if (dp[u].first != -1) {
    return;
  }
  if (u != v && edge[u].size() == 1) {
    dp[u] = mp(1, 1);
    return;
  }
  ll ans = 1;
  int size = 0;
  for (int i = 0; i < edge[u].size(); i++) {
    if (edge[u][i] == v) {
      continue;
    }
    solve(edge[u][i], u);
    ans *= dp[edge[u][i]].first;
    ans %= p;
    ans *= invfact[dp[edge[u][i]].second];
    ans %= p;
    size += dp[edge[u][i]].second;
  }
  ans *= fact[size];
  ans %= p;
  dp[u] = mp(ans, size + 1);
  return;
}
ll solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    edge[--u].pb(--v);
    edge[v].pb(u);
  }
  solve(0, 0);
  ans[0] = dp[0].first;
  int idx = 0;
  eulerTree(0, idx);
  for (int i = 1; i < idx; i++) {
    dp[euler[i - 1]].first *= invfact[dp[euler[i - 1]].second - 1];
    dp[euler[i - 1]].first %= p;
    dp[euler[i - 1]].second -= dp[euler[i]].second;
    dp[euler[i - 1]].first *= fact[dp[euler[i - 1]].second - 1];
    dp[euler[i - 1]].first %= p;
    dp[euler[i - 1]].first *= modpower(dp[euler[i]].first, p - 2);
    dp[euler[i - 1]].first %= p;
    dp[euler[i - 1]].first *= fact[dp[euler[i]].second];
    dp[euler[i - 1]].first %= p;
    dp[euler[i]] = mp(-1, 0);
    solve(euler[i], euler[i]);
    ans[euler[i]] = dp[euler[i]].first;
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  computefactorial();
  solve();
}

Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <vector>
#define ll long long int
#define mp make_pair
#define pb push_back
#define vi vector<int>
using namespace std;
vector<vector<int>> edge(200007);
vector<pair<ll, int>> dp(200007, mp(-1, 0));
vector<ll> ans(200007);
vector<int> euler(400007);
vector<bool> vis(200007, 0);
const ll p = 1e9 + 7;
ll modpower(ll base, ll power, ll mod = p) {
  ll ans = 1;
  base %= p;
  while (power) {
    if (power & 1) {
      ans *= base;
      ans %= p;
    }
    base *= base;
    base %= p;
    power >>= 1;
  }
  return ans;
}
ll fact[200007];
ll invfact[200007];
void computefactorial() {
  fact[0] = 1;
  for (int i = 1; i < 200007; i++) {
    fact[i] = i * fact[i - 1];
    fact[i] %= p;
  }
  invfact[200006] = modpower(fact[200006], p - 2);
  for (int i = 200005; i >= 0; i--) {
    invfact[i] = (i + 1) * invfact[i + 1];
    invfact[i] %= p;
  }
}
void eulerTree(int u, int &indx) {
  vis[u] = 1;
  euler[indx++] = u;
  for (auto it : edge[u]) {
    if (!vis[it]) {
      eulerTree(it, indx);
      euler[indx++] = u;
    }
  }
}
void solve(int u, int v) {
  if (dp[u].first != -1) {
    return;
  }
  if (u != v && edge[u].size() == 1) {
    dp[u] = mp(1, 1);
    return;
  }
  ll ans = 1;
  int size = 0;
  for (int i = 0; i < edge[u].size(); i++) {
    if (edge[u][i] == v) {
      continue;
    }
    solve(edge[u][i], u);
    ans *= dp[edge[u][i]].first;
    ans %= p;
    ans *= invfact[dp[edge[u][i]].second];
    ans %= p;
    size += dp[edge[u][i]].second;
  }
  ans *= fact[size];
  ans %= p;
  dp[u] = mp(ans, size + 1);
  return;
}
ll solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    edge[--u].pb(--v);
    edge[v].pb(u);
  }
  solve(0, 0);
  ans[0] = dp[0].first;
  int idx = 0;
  eulerTree(0, idx);
  for (int i = 1; i < idx; i++) {
    dp[euler[i - 1]].first *= invfact[dp[euler[i - 1]].second - 1];
    dp[euler[i - 1]].first %= p;
    dp[euler[i - 1]].second -= dp[euler[i]].second;
    dp[euler[i - 1]].first *= fact[dp[euler[i - 1]].second - 1];
    dp[euler[i - 1]].first %= p;
    dp[euler[i - 1]].first *= modpower(dp[euler[i]].first, p - 2);
    dp[euler[i - 1]].first %= p;
    dp[euler[i - 1]].first *= fact[dp[euler[i]].second];
    dp[euler[i - 1]].first %= p;
    dp[euler[i]].first *= invfact[dp[euler[i]].second - 1];
    dp[euler[i]].second += dp[euler[i - 1]].second;
    dp[euler[i]].first %= p;
    dp[euler[i]].first *= fact[dp[euler[i]].second - 1];
    dp[euler[i]].first %= p;
    dp[euler[i]].first *= invfact[dp[euler[i - 1]].second];
    dp[euler[i]].first %= p;
    dp[euler[i]].first *= dp[euler[i - 1]].first;
    dp[euler[i]].first %= p;
    ans[euler[i]] = dp[euler[i]].first;
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i] << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  computefactorial();
  solve();
}