Problem p02728 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); ++i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
class mint {
public:
  long long x;

  constexpr mint(long long _x = 0) : x((_x % mod + mod) % mod) {}
  constexpr mint operator-() const { return mint(-x); }
  constexpr mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  constexpr mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  constexpr mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  constexpr mint operator+(const mint &a) const {
    mint res(*this);
    return res += a;
  }
  constexpr mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  constexpr mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  constexpr mint pow(long long x) const {
    if (!x)
      return 1;
    mint a = pow(x >> 1);
    a *= a;
    if (x & 1)
      a *= *this;
    return a;
  }
  constexpr mint inv() const { return pow(mod - 2); }
  constexpr mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  constexpr mint operator/(const mint &a) const {
    mint res(*this);
    return res /= a;
  }
  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};
const int nmax = 1005;
ll fac[nmax], finv[nmax], inv[nmax];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < nmax; 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;
  }
}
// modintを使うときはllをmintに
mint com(int n, int k) {
  if (n < k || n < 0 || k < 0) {
    return 0;
  }
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
vector<vector<int>> g(nmax);
vector<mint> dp(nmax), ans(nmax);
vector<int> c(nmax);

void dfs1(int cur, int pre = -1) {
  c[cur] = 0;
  dp[cur] = 1;
  for (int to : g[cur]) {
    if (to == pre) {
      continue;
    }
    dfs1(to, cur);
    dp[cur] *= dp[to] * com(c[cur] + c[to], c[to]);
    c[cur] += c[to];
  }
  c[cur]++;
}
int n;
void dfs2(int cur, int pre = -1, mint res = 1) {
  dp[cur] *= res * com(n - 1, n - c[cur]);
  for (int to : g[cur]) {
    if (to == pre) {
      continue;
    }
    res = dp[cur] / dp[to] / com(n - 1, c[to]);
    dfs2(to, cur, res);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  COMinit();
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs1(0);
  dfs2(0);
  mint ans = 0;
  rep(i, n) { cout << dp[i] << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (n); ++i)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
class mint {
public:
  long long x;

  constexpr mint(long long _x = 0) : x((_x % mod + mod) % mod) {}
  constexpr mint operator-() const { return mint(-x); }
  constexpr mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  constexpr mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  constexpr mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  constexpr mint operator+(const mint &a) const {
    mint res(*this);
    return res += a;
  }
  constexpr mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  constexpr mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  constexpr mint pow(long long x) const {
    if (!x)
      return 1;
    mint a = pow(x >> 1);
    a *= a;
    if (x & 1)
      a *= *this;
    return a;
  }
  constexpr mint inv() const { return pow(mod - 2); }
  constexpr mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  constexpr mint operator/(const mint &a) const {
    mint res(*this);
    return res /= a;
  }
  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};
const int nmax = 200005;
ll fac[nmax], finv[nmax], inv[nmax];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < nmax; 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;
  }
}
// modintを使うときはllをmintに
mint com(int n, int k) {
  if (n < k || n < 0 || k < 0) {
    return 0;
  }
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
vector<vector<int>> g(nmax);
vector<mint> dp(nmax), ans(nmax);
vector<int> c(nmax);

void dfs1(int cur, int pre = -1) {
  c[cur] = 0;
  dp[cur] = 1;
  for (int to : g[cur]) {
    if (to == pre) {
      continue;
    }
    dfs1(to, cur);
    dp[cur] *= dp[to] * com(c[cur] + c[to], c[to]);
    c[cur] += c[to];
  }
  c[cur]++;
}
int n;
void dfs2(int cur, int pre = -1, mint res = 1) {
  dp[cur] *= res * com(n - 1, n - c[cur]);
  for (int to : g[cur]) {
    if (to == pre) {
      continue;
    }
    res = dp[cur] / dp[to] / com(n - 1, c[to]);
    dfs2(to, cur, res);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  COMinit();
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs1(0);
  dfs2(0);
  mint ans = 0;
  rep(i, n) { cout << dp[i] << endl; }
}