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

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
// #include"testlib.h"
#define endl "\\n"
#define all(v) v.begin(), v.end()
#define allr(s) s.rbegin(), s.rend()
#define RT(s) return cout << s, 0
#define watch(x) cout << (#x) << " = " << x << endl
#define sz(s) (int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
  // freopen("street.in", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
}
const int N = 2e5 + 9;
vi adjL[N];
int n, mod = 1e9 + 7;
ll fact[N], show[N];
pair<ll, ll> mem[N], store[N];

ll fpow(ll x, ll p) {
  if (!p)
    return 1;
  ll sq = fpow(x, p / 2);
  sq *= sq;
  sq %= mod;
  if (p & 1)
    sq *= x;
  return sq % mod;
}
ll cdiv(ll num, ll dem) {
  num %= mod;
  return (num * fpow(dem % mod, mod - 2LL)) % mod;
}
pair<ll, ll> solve(int u, int par) {
  if (mem[u] != make_pair(-1LL, -1LL))
    return mem[u];
  ll sizeofR = 0;
  ll NumofWays = 1, down = 1;
  for (auto &v : adjL[u]) {
    if (v == par)
      continue;
    pair<ll, ll> Com = solve(v, u);
    sizeofR += Com.second;
    NumofWays *= Com.first, down *= fact[Com.second];
    NumofWays %= mod, down %= mod;
  }
  store[u] = {NumofWays, down};
  ll ans = cdiv(fact[sizeofR], down);
  ans *= NumofWays;
  ans %= mod;
  return mem[u] = {ans, sizeofR + 1};
}

void Final(int u, int par) {
  if (par == -1)
    show[u] = mem[u].first;
  else {
    ll upove = n - mem[u].second - 1;
    ll ParWays = cdiv(store[par].first, mem[u].first);
    ll Pardown = cdiv(store[par].second, fact[mem[u].second]);
    ll All = cdiv(fact[upove], Pardown);
    All *= ParWays;
    All %= mod;
    ll NumofWays = (store[u].first * All) % mod;
    ll down = (store[u].second * fact[upove + 1]) % mod;
    store[u] = {NumofWays, down};
    show[u] = (NumofWays * cdiv(fact[n - 1LL], down)) % mod;
  }
  for (auto &v : adjL[u]) {
    if (v != par)
      Final(v, u);
  }
}
int main() {
  // file();
  fast();
  memset(mem, -1LL, sizeof mem);
  fact[0] = fact[1] = 1;
  for (int i = 2; i < N; i++)
    fact[i] = (i * fact[i - 1]) % mod;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adjL[u].push_back(v);
    adjL[v].push_back(u);
  }
  solve(1, -1);
  Final(1, -1);
  for (int i = 1; i <= n; i++)
    cout << show[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
// #include"testlib.h"
#define endl "\\n"
#define all(v) v.begin(), v.end()
#define allr(s) s.rbegin(), s.rend()
#define RT(s) return cout << s, 0
#define watch(x) cout << (#x) << " = " << x << endl
#define sz(s) (int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
  // freopen("street.in", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
}
const int N = 2e5 + 9;
vi adjL[N];
int n, mod = 1e9 + 7;
ll fact[N];
map<ii, pair<ll, ll>> mem;

ll fpow(ll x, ll p) {
  if (!p)
    return 1;
  ll sq = fpow(x, p / 2);
  sq *= sq;
  sq %= mod;
  if (p & 1)
    sq *= x;
  return sq % mod;
}
pair<ll, ll> solve(int u, int par) {
  if (mem.find({u, par}) != mem.end())
    return mem[{u, par}];
  ll sizeofR = 0;
  ll NumofWays = 1, down = 1;
  for (auto &v : adjL[u]) {
    if (v == par)
      continue;
    pair<ll, ll> Com = solve(v, u);
    sizeofR += Com.second;
    NumofWays *= Com.first, down *= fact[Com.second];
    NumofWays %= mod, down %= mod;
  }
  ll ans = (fact[sizeofR] * fpow(down, mod - 2LL)) % mod;
  ans *= NumofWays;
  ans %= mod;
  return mem[{u, par}] = {ans, sizeofR + 1};
}
int main() {
  // file();
  fast();
  fact[0] = fact[1] = 1;
  for (int i = 2; i < N; i++)
    fact[i] = (i * fact[i - 1]) % mod;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adjL[u].push_back(v);
    adjL[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    cout << solve(i, -1).first << endl;
  }
  return 0;
}