Problem p02728 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pr;
typedef vector<ll> vc;
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define reps(i, v) for (ll i = 0; i < v.size(); i++)
vc g[200005];
ll n, d[200005] = {0}, par[200005] = {0}, bk[200005] = {0};
ll ans[200005] = {0}, dp1[200005] = {0}, dp2[200005] = {0};
bool used[200005] = {0};
const long long mod = 1e9 + 7;
const long long c_max = 1000003;
long long fac[c_max], finv[c_max], inv[c_max];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i < c_max; 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;
  }
}
long long COM(long long comn, long long comk) {
  if (comn < comk)
    return 0;
  if (comn < 0 || comk < 0)
    return 0;
  if (comn >= c_max) {
    long long cret = finv[comk];
    for (long long i = 0; i < comk; i++)
      cret = (cret * (comn - i)) % mod;
    return cret;
  }
  return fac[comn] * (finv[comk] * finv[comn - comk] % mod) % mod;
}
long long powmod(long long px, long long py, long long pmod) {
  if (py == 0)
    return 1;
  if (py % 2 == 0)
    return powmod((px * px) % pmod, py / 2, pmod);
  else
    return (px * powmod(px, py - 1, pmod)) % pmod;
}
// Extended_GCD
long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long gcd_ = a;
  if (b != 0) {
    gcd_ = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return gcd_;
}
// Inverse
long long iv(long long a, long long b) {
  long long x, y;
  extgcd(a, b, x, y);
  if (x >= 0)
    return x % b;
  else
    return (b - (-x) % b) % b;
}

void dfs1(ll s) {
  used[s] = true;
  reps(i, g[s]) {
    if (!used[g[s][i]]) {
      par[g[s][i]] = s;
      dfs1(g[s][i]);
    }
  }
  bk[s] = 1;
  reps(j, g[s]) {
    if (par[s] == g[s][j])
      continue;
    bk[s] += bk[g[s][j]];
  }
  if (g[s].size() == 1 && s != 1) {
    d[s] = 1;
  } else {
    vc d1m;
    ll summ = 0;
    d[s] = 1;
    reps(j, g[s]) {
      if (par[s] == g[s][j])
        continue;
      d[s] = d[s] * d[g[s][j]] % mod;
      d1m.push_back(bk[g[s][j]]);
      summ += bk[g[s][j]];
    }
    reps(j, d1m) {
      d[s] = d[s] * COM(summ, d1m[j]) % mod;
      summ -= d1m[j];
    }
  }
}

void dfs2(ll s) {
  used[s] = true;
  if (s != 1) {
    vc d1m;
    ll summ = 0;
    dp1[s] = n - bk[s];
    dp2[s] = dp2[par[s]];
    // dp2[s]=dp2[par[s]]*d[par[s]]%mod;
    // dp2[s]=dp2[s]*iv(d[s],mod)%mod;
    // dp2[s]=dp2[s]*COM(n-1-bk[s],dp1[par[s]]);
    d1m.push_back(dp1[par[s]]);
    summ = dp1[par[s]];
    reps(j, g[par[s]]) {
      if (g[par[s]][j] == s)
        continue;
      if (g[par[s]][j] == par[par[s]])
        continue;
      dp2[s] = dp2[s] * d[g[par[s]][j]] % mod;
      d1m.push_back(bk[g[par[s]][j]]);
      summ += bk[g[par[s]][j]];
    }
    reps(j, d1m) {
      dp2[s] = dp2[s] * COM(summ, d1m[j]) % mod;
      summ -= d1m[j];
    }
    ans[s] = dp2[s] * d[s] % mod;
    ans[s] = ans[s] * COM(n - 1, dp1[s]) % mod;
  }
  reps(i, g[s]) {
    if (!used[g[s][i]]) {
      dfs2(g[s][i]);
    }
  }
}

int main() {
  // your code goes here
  COMinit();
  cin >> n;
  rep(i, 1, n - 1) {
    ll aa, bb;
    cin >> aa >> bb;
    g[aa].push_back(bb);
    g[bb].push_back(aa);
  }
  d[0] = 1;
  dfs1(1);
  ans[1] = d[1];
  // rep(i,1,n) cout << d[i] << endl;
  rep(i, 1, n) used[i] = false;
  dp1[1] = 0;
  dp2[1] = 1;
  dfs2(1);
  rep(i, 1, n) cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pr;
typedef vector<ll> vc;
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define reps(i, v) for (ll i = 0; i < v.size(); i++)
vc g[200005];
ll n, d[200005] = {0}, par[200005] = {0}, bk[200005] = {0};
ll ans[200005] = {0}, dp1[200005] = {0}, dp2[200005] = {0};
bool used[200005] = {0};
const long long mod = 1e9 + 7;
const long long c_max = 1000003;
long long fac[c_max], finv[c_max], inv[c_max];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i < c_max; 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;
  }
}
long long COM(long long comn, long long comk) {
  if (comn < comk)
    return 0;
  if (comn < 0 || comk < 0)
    return 0;
  if (comn >= c_max) {
    long long cret = finv[comk];
    for (long long i = 0; i < comk; i++)
      cret = (cret * (comn - i)) % mod;
    return cret;
  }
  return fac[comn] * (finv[comk] * finv[comn - comk] % mod) % mod;
}
long long powmod(long long px, long long py, long long pmod) {
  if (py == 0)
    return 1;
  if (py % 2 == 0)
    return powmod((px * px) % pmod, py / 2, pmod);
  else
    return (px * powmod(px, py - 1, pmod)) % pmod;
}
// Extended_GCD
long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long gcd_ = a;
  if (b != 0) {
    gcd_ = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return gcd_;
}
// Inverse
long long iv(long long a, long long b) {
  long long x, y;
  extgcd(a, b, x, y);
  if (x >= 0)
    return x % b;
  else
    return (b - (-x) % b) % b;
}

void dfs1(ll s) {
  used[s] = true;
  reps(i, g[s]) {
    if (!used[g[s][i]]) {
      par[g[s][i]] = s;
      dfs1(g[s][i]);
    }
  }
  bk[s] = 1;
  reps(j, g[s]) {
    if (par[s] == g[s][j])
      continue;
    bk[s] += bk[g[s][j]];
  }
  if (g[s].size() == 1 && s != 1) {
    d[s] = 1;
  } else {
    vc d1m;
    ll summ = 0;
    d[s] = 1;
    reps(j, g[s]) {
      if (par[s] == g[s][j])
        continue;
      d[s] = d[s] * d[g[s][j]] % mod;
      d1m.push_back(bk[g[s][j]]);
      summ += bk[g[s][j]];
    }
    reps(j, d1m) {
      d[s] = d[s] * COM(summ, d1m[j]) % mod;
      summ -= d1m[j];
    }
  }
}

void dfs2(ll s) {
  used[s] = true;
  if (s != 1) {
    vc d1m;
    ll summ = 0;
    dp1[s] = n - bk[s];
    // dp2[s]=dp2[par[s]];
    dp2[s] = dp2[par[s]] * d[par[s]] % mod;
    dp2[s] = dp2[s] * iv(d[s], mod) % mod;
    dp2[s] = dp2[s] * iv(COM(bk[par[s]] - 1, bk[s]), mod) % mod;
    dp2[s] = dp2[s] * COM(n - 1 - bk[s], dp1[par[s]]) % mod;

    ans[s] = dp2[s] * d[s] % mod;
    ans[s] = ans[s] * COM(n - 1, dp1[s]) % mod;
  }
  reps(i, g[s]) {
    if (!used[g[s][i]]) {
      dfs2(g[s][i]);
    }
  }
}

int main() {
  // your code goes here
  COMinit();
  cin >> n;
  rep(i, 1, n - 1) {
    ll aa, bb;
    cin >> aa >> bb;
    g[aa].push_back(bb);
    g[bb].push_back(aa);
  }
  d[0] = 1;
  dfs1(1);
  ans[1] = d[1];
  // rep(i,1,n) cout << d[i] << endl;
  rep(i, 1, n) used[i] = false;
  dp1[1] = 0;
  dp2[1] = 1;
  dfs2(1);
  rep(i, 1, n) cout << ans[i] << endl;
  return 0;
}