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

#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007;
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;

ll fac[200007], finv[200007], inv[200007];
void cominit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < 200007; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

vector<vector<ll>> g(200000);
ll pa[200000];
ll dpinline[200000], dpinnum[200000], dpoutnum[200000], dpoutline[200000];
void dfs1(ll now, ll p) {
  pa[now] = p;
  dpinline[now] = 0;
  dpinnum[now] = 1;
  for (auto &to : g[now]) {
    if (to == p)
      continue;
    dfs1(to, now);
    dpinline[now] += dpinline[to];
    dpinnum[now] *= finv[dpinline[to]];
    dpinnum[now] %= mod;
    dpinnum[now] *= dpinnum[to];
    dpinnum[now] %= mod;
  }
  dpinnum[now] *= fac[dpinline[now]];
  dpinnum[now] %= mod;
  dpinline[now]++;
}
void dfs2(ll now, ll p) {
  vector<ll> v1, v2, v3, v4;
  for (auto &to : g[now]) {
    if (to == p)
      v1.pb(1), v2.pb(1);
    else
      v1.pb((dpinnum[to] * finv[dpinline[to]]) % mod),
          v2.pb((dpinnum[to] * finv[dpinline[to]]) % mod);
    if (to == p)
      v3.pb(0), v4.pb(0);
    else
      v3.pb(dpinline[to]), v4.pb(dpinline[to]);
  }
  int niko = v1.size();
  for (int i = 1; i < niko; i++)
    v1[i] = (v1[i - 1] * v1[i]) % mod;
  for (int i = niko - 2; i >= 0; i--)
    v2[i] = (v2[i + 1] * v2[i]) % mod;
  for (int i = 1; i < niko; i++)
    v3[i] += v3[i - 1];
  for (int i = niko - 2; i >= 0; i--)
    v4[i] += v4[i + 1];
  for (int i = 0; i < g[now].size(); i++) {
    int to = g[now][i];
    if (to == p)
      continue;
    ll ret = 1LL, line = 0LL;
    if (i > 0)
      ret = (ret * v1[i - 1]) % mod, line += v3[i - 1];
    if (i < niko - 1)
      ret = (ret * v2[i + 1]) % mod, line += v4[i + 1];
    ret = (ret * dpoutnum[now]) % mod;
    ret = (ret * finv[dpoutline[now]]) % mod;
    line += dpoutline[now];
    ret = (ret * fac[line]) % mod;
    line++;
    dpoutline[to] = line;
    dpoutnum[to] = ret;
    dfs2(to, now);
  }
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cominit();
  ll n;
  cin >> n;
  ll a, b;
  for (int i = 1; i < n; i++) {
    cin >> a >> b;
    a--, b--;
    g[a].pb(b);
    g[b].pb(a);
  }

  dfs1(0, -1);
  dpoutnum[0] = 1;
  dpoutline[0] = 0;
  dfs2(0, -1);
  for (int i = 0; i < n; i++) {
    ll line = dpoutline[i];
    ll ans = (dpoutnum[i] * finv[dpoutline[i]]) % mod;
    for (auto &to : g[i]) {
      if (to == pa[i])
        continue;
      line += dpinline[to];
      ans = (ans * dpinnum[to]) % mod;
      ans = (ans * finv[dpinline[to]]) % mod;
    }
    ans = (ans * fac[line]) % mod;
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define be(v) (v).begin(), (v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod = 1000000007;
#define doublecout(a) cout << fixed << setprecision(10) << a << endl;

ll fac[200007], finv[200007], inv[200007];
void cominit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < 200007; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

vector<vector<ll>> g(200001);
ll dpinline[200001], dpinnum[200001];
void dfs1(ll now, ll p) {
  dpinline[now] = 0;
  dpinnum[now] = 1;
  for (auto &to : g[now]) {
    if (to == p)
      continue;
    dfs1(to, now);
    dpinline[now] += dpinline[to];
    dpinnum[now] *= finv[dpinline[to]];
    dpinnum[now] %= mod;
    dpinnum[now] *= dpinnum[to];
    dpinnum[now] %= mod;
  }
  dpinnum[now] *= fac[dpinline[now]];
  dpinnum[now] %= mod;
  dpinline[now]++;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cominit();
  ll n;
  cin >> n;
  ll a, b;
  for (int i = 1; i < n; i++) {
    cin >> a >> b;
    a--, b--;
    g[a].pb(b);
    g[b].pb(a);
  }
  for (int i = 0; i < n; i++) {
    dfs1(i, -1);
    cout << dpinnum[i] << endl;
  }
  return 0;
}