Problem p02728 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

constexpr ll mod = 1e9 + 7;

ll mod_pow(ll a, ll b) {
  a %= mod;
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll res = mod_pow(a, b / 2) % mod;
  res *= res;
  res %= mod;
  if (b % 2)
    res *= a;
  return res % mod;
}

int n;
vector<int> g[200200];
ll p[200200];
ll d[202020];

ll q[202020];
ll qq[202020];

void dfs(int s, int pp) {
  d[s] = 1;
  for (int t : g[s]) {
    if (t == pp)
      continue;
    p[t] = s;
    dfs(t, s);
    d[s] += d[t];
    (q[s] *= q[t]) %= mod;
  }
  (q[s] *= mod_pow(d[s], mod - 2)) %= mod;
}

void dfs2(int s, int p) {
  ll re = 1;
  for (int t : g[s]) {
    if (t == p)
      continue;
    (re *= q[t]) %= mod;
  }
  for (int t : g[s]) {
    if (t == p)
      continue;
    (qq[t] *= qq[s] * re % mod * mod_pow(q[t], mod - 2) % mod *
              mod_pow((n - d[t]), mod - 2) % mod) %= mod;
    dfs2(t, s);
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    q[i] = 1;
    qq[i] = 1;
  }
  p[0] = -1;
  dfs(0, -1);
  dfs2(0, -1);
  ll res = 1;
  for (int i = 1; i < n; i++) {
    (res *= i) %= mod;
  }
  for (int i = 0; i < n; i++) {
    ll ans = res;
    for (int t : g[i]) {
      if (p[t] == i) {
        (ans *= q[t]) %= mod;
      } else {
        (ans *= mod_pow(n - d[i], mod - 2)) %= mod;
        ll re = q[t];
        (re *= d[t]) %= mod;
        (re *= mod_pow(q[i], mod - 2)) %= mod;
        (ans *= re) %= mod;
        (ans *= qq[t]) %= mod;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;

ll mod = 1e9 + 7;

ll mod_pow(ll a, ll b) {
  a %= mod;
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll res = mod_pow(a, b / 2) % mod;
  res *= res;
  res %= mod;
  if (b % 2)
    res *= a;
  return res % mod;
}

int n;
vector<int> g[200200];
ll p[200200];
ll d[202020];

ll q[202020];
ll qq[202020];

void dfs(int s, int pp) {
  d[s] = 1;
  for (int t : g[s]) {
    if (t == pp)
      continue;
    p[t] = s;
    dfs(t, s);
    d[s] += d[t];
    (q[s] *= q[t]) %= mod;
  }
  (q[s] *= mod_pow(d[s], mod - 2)) %= mod;
}

void dfs2(int s, int p) {
  ll re = 1;
  for (int t : g[s]) {
    if (t == p)
      continue;
    (re *= q[t]) %= mod;
  }
  for (int t : g[s]) {
    if (t == p)
      continue;
    (qq[t] *= qq[s] * re % mod * mod_pow(q[t], mod - 2) % mod *
              mod_pow((n - d[t]), mod - 2) % mod) %= mod;
    dfs2(t, s);
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    q[i] = 1;
    qq[i] = 1;
  }
  p[0] = -1;
  dfs(0, -1);
  dfs2(0, -1);
  ll res = 1;
  for (int i = 1; i < n; i++) {
    (res *= i) %= mod;
  }
  for (int i = 0; i < n; i++) {
    ll ans = res;
    for (int t : g[i]) {
      if (p[t] == i) {
        (ans *= q[t]) %= mod;
      } else {
        (ans *= mod_pow(n - d[i], mod - 2)) %= mod;
        for (int tt : g[t]) {
          if (tt == i)
            continue;
          if (p[tt] == t) {
            (ans *= q[tt]) %= mod;
          }
        }
        (ans *= qq[t]) %= mod;
      }
    }
    cout << ans << endl;
  }
}