Problem p02728 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define del(a, i) memset(a, i, sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l + r) >> 1)
#define lowbit(x) (x & (-x))
#define INF 0x3f3f3f3f
using namespace std;
template <class T> il read(T &x) {
  int f = 1;
  char k = getchar();
  x = 0;
  for (; k > '9' || k < '0'; k = getchar())
    if (k == '-')
      f = -1;
  for (; k >= '0' && k <= '9'; k = getchar())
    x = x * 10 + k - '0';
  x *= f;
}
template <class T> il _print(T x) {
  if (x >= 10)
    _print(x / 10);
  putchar(x % 10 + '0');
}
template <class T> il print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  _print(x);
}
ll mul(ll a, ll b, ll mod) {
  long double c = 1.;
  return (a * b - (ll)(c * a * b / mod) * mod) % mod;
}
it qpow(int x, int k, int mod) {
  int res = 1, bas = x;
  while (k) {
    if (k & 1)
      res = 1ll * res * bas % mod;
    bas = 1ll * bas * bas % mod, k >>= 1;
  }
  return res;
}
const int N = 1e5 + 5, mod = 1e9 + 7;
it add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
it mul(int x, int y) { return 1ll * x * y % mod; }
il inc(int &x, int y) { x = add(x, y); }
int n, u, v, dp[N], ans[N], sz[N], fac[N], ifac[N];
vector<int> G[N];
il DFS1(int u, int fa) {
  dp[u] = 1, sz[u] = 1;
  for (auto v : G[u]) {
    if (v == fa)
      continue;
    DFS1(v, u), dp[u] = mul(dp[u], dp[v]);
    sz[u] += sz[v], dp[u] = mul(dp[u], ifac[sz[v]]);
  }
  dp[u] = mul(dp[u], fac[sz[u] - 1]);
}
il DFS2(int u, int fa) {
  ans[u] = dp[u];
  for (auto v : G[u]) {
    if (v == fa)
      continue;
    int tu = dp[u];
    dp[u] = mul(dp[u], ifac[n - 1]);
    dp[u] = mul(dp[u], qpow(dp[v], mod - 2, mod));
    dp[u] = mul(dp[u], fac[sz[v]]);
    dp[u] = mul(dp[u], fac[n - sz[v] - 1]);

    dp[v] = mul(dp[v], ifac[sz[v] - 1]);
    dp[v] = mul(dp[v], dp[u]);
    dp[v] = mul(dp[v], ifac[n - sz[v]]);
    dp[v] = mul(dp[v], fac[n - 1]);
    DFS2(v, u), dp[u] = tu;
  }
}
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  read(n), fac[0] = 1;
  for (ri i = 1; i < n; ++i) {
    read(u), read(v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (ri i = 1; i <= n; ++i)
    fac[i] = mul(fac[i - 1], i);
  ifac[n] = qpow(fac[n], mod - 2, mod);
  for (ri i = n - 1; i >= 0; --i)
    ifac[i] = mul(ifac[i + 1], i + 1);
  DFS1(1, 0), DFS2(1, 0);
  for (ri i = 1; i <= n; ++i)
    printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define del(a, i) memset(a, i, sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l + r) >> 1)
#define lowbit(x) (x & (-x))
#define INF 0x3f3f3f3f
using namespace std;
template <class T> il read(T &x) {
  int f = 1;
  char k = getchar();
  x = 0;
  for (; k > '9' || k < '0'; k = getchar())
    if (k == '-')
      f = -1;
  for (; k >= '0' && k <= '9'; k = getchar())
    x = x * 10 + k - '0';
  x *= f;
}
template <class T> il _print(T x) {
  if (x >= 10)
    _print(x / 10);
  putchar(x % 10 + '0');
}
template <class T> il print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  _print(x);
}
ll mul(ll a, ll b, ll mod) {
  long double c = 1.;
  return (a * b - (ll)(c * a * b / mod) * mod) % mod;
}
it qpow(int x, int k, int mod) {
  int res = 1, bas = x;
  while (k) {
    if (k & 1)
      res = 1ll * res * bas % mod;
    bas = 1ll * bas * bas % mod, k >>= 1;
  }
  return res;
}
const int N = 2e5 + 5, mod = 1e9 + 7;
it add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
it mul(int x, int y) { return 1ll * x * y % mod; }
il inc(int &x, int y) { x = add(x, y); }
int n, u, v, dp[N], ans[N], sz[N], fac[N], ifac[N];
vector<int> G[N];
il DFS1(int u, int fa) {
  dp[u] = 1, sz[u] = 1;
  for (auto v : G[u]) {
    if (v == fa)
      continue;
    DFS1(v, u), dp[u] = mul(dp[u], dp[v]);
    sz[u] += sz[v], dp[u] = mul(dp[u], ifac[sz[v]]);
  }
  dp[u] = mul(dp[u], fac[sz[u] - 1]);
}
il DFS2(int u, int fa) {
  ans[u] = dp[u];
  for (auto v : G[u]) {
    if (v == fa)
      continue;
    int tu = dp[u];
    dp[u] = mul(dp[u], ifac[n - 1]);
    dp[u] = mul(dp[u], qpow(dp[v], mod - 2, mod));
    dp[u] = mul(dp[u], fac[sz[v]]);
    dp[u] = mul(dp[u], fac[n - sz[v] - 1]);

    dp[v] = mul(dp[v], ifac[sz[v] - 1]);
    dp[v] = mul(dp[v], dp[u]);
    dp[v] = mul(dp[v], ifac[n - sz[v]]);
    dp[v] = mul(dp[v], fac[n - 1]);
    DFS2(v, u), dp[u] = tu;
  }
}
int main() {
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  read(n), fac[0] = 1;
  for (ri i = 1; i < n; ++i) {
    read(u), read(v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (ri i = 1; i <= n; ++i)
    fac[i] = mul(fac[i - 1], i);
  ifac[n] = qpow(fac[n], mod - 2, mod);
  for (ri i = n - 1; i >= 0; --i)
    ifac[i] = mul(ifac[i + 1], i + 1);
  DFS1(1, 0), DFS2(1, 0);
  for (ri i = 1; i <= n; ++i)
    printf("%d\n", ans[i]);
  return 0;
}