Problem p02728 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define int long long
#define endl "\n"
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb(x) push_back(x)
#define MAXN 2020
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll, ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
inline int mod(int n, int m) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int exp(int a, int b, int m) {
  if (b == 0)
    return 1;
  if (b == 1)
    return mod(a, m);
  int k = mod(exp(a, b / 2, m), m);
  if (b & 1) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
const int M = 1e9 + 7;
vector<int> adj[MAXN];
int res[MAXN];
int dp[MAXN];
int sz[MAXN];
int fat[MAXN], inv[MAXN];
void dfs(int v, int p = -1) {
  sz[v] = 1;
  for (int x : adj[v]) {
    if (x == p)
      continue;
    dfs(x, v);
    sz[v] += sz[x];
  }
}
int solve(int v, int p) {
  int ways = fat[sz[v] - 1];
  for (int x : adj[v]) {
    if (x == p)
      continue;
    ways = mod(ways * mod(solve(x, v) * inv[sz[x]], M), M);
  }
  return dp[v] = ways;
}
int n;
void solve2(int v, int p) {
  if (p == -1) {
    res[v] = dp[v];

  } else {
    int dpp = mod(res[p], M);
    dpp = mod(dpp * fat[sz[v]], M);
    dpp = mod(dpp * exp(dp[v], M - 2, M), M);
    dpp = mod(dpp * fat[n - sz[v] - 1], M);
    dpp = mod(dpp * inv[n - sz[v]], M);
    res[v] = mod(dpp, M);

    for (int x : adj[v]) {
      if (x != p) {
        res[v] = mod(res[v] * mod(dp[x] * inv[sz[x]], M), M);
      }
    }
  }
  for (int x : adj[v]) {
    if (x == p)
      continue;
    solve2(x, v);
  }
}

int32_t main() {
  fastio;
  fat[0] = inv[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fat[i] = mod(fat[i - 1] * i, M);
    inv[i] = exp(fat[i], M - 2, M);
  }
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(1, -1);
  solve(1, -1);
  solve2(1, -1);
  for (int i = 1; i <= n; i++)
    cout << res[i] << endl;
  // Math -> gcd it all
  // did you check N=1? Did you mix up N,M?
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define trav(a, x) for (auto &a : x)
#define allin(a, x) for (auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define int long long
#define endl "\n"
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb(x) push_back(x)
#define MAXN 200100
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll, ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;
inline int mod(int n, int m) {
  int ret = n % m;
  if (ret < 0)
    ret += m;
  return ret;
}
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int exp(int a, int b, int m) {
  if (b == 0)
    return 1;
  if (b == 1)
    return mod(a, m);
  int k = mod(exp(a, b / 2, m), m);
  if (b & 1) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
const int M = 1e9 + 7;
vector<int> adj[MAXN];
int res[MAXN];
int dp[MAXN];
int sz[MAXN];
int fat[MAXN], inv[MAXN];
void dfs(int v, int p = -1) {
  sz[v] = 1;
  for (int x : adj[v]) {
    if (x == p)
      continue;
    dfs(x, v);
    sz[v] += sz[x];
  }
}
int solve(int v, int p) {
  int ways = fat[sz[v] - 1];
  for (int x : adj[v]) {
    if (x == p)
      continue;
    ways = mod(ways * mod(solve(x, v) * inv[sz[x]], M), M);
  }
  return dp[v] = ways;
}
int n;
void solve2(int v, int p) {
  if (p == -1) {
    res[v] = dp[v];

  } else {
    int dpp = mod(res[p], M);
    dpp = mod(dpp * fat[sz[v]], M);
    dpp = mod(dpp * exp(dp[v], M - 2, M), M);
    dpp = mod(dpp * fat[n - sz[v] - 1], M);
    dpp = mod(dpp * inv[n - sz[v]], M);
    res[v] = mod(dpp, M);

    for (int x : adj[v]) {
      if (x != p) {
        res[v] = mod(res[v] * mod(dp[x] * inv[sz[x]], M), M);
      }
    }
  }
  for (int x : adj[v]) {
    if (x == p)
      continue;
    solve2(x, v);
  }
}

int32_t main() {
  fastio;
  fat[0] = inv[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    fat[i] = mod(fat[i - 1] * i, M);
    inv[i] = exp(fat[i], M - 2, M);
  }
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].pb(b);
    adj[b].pb(a);
  }
  dfs(1, -1);
  solve(1, -1);
  solve2(1, -1);
  for (int i = 1; i <= n; i++)
    cout << res[i] << endl;
  // Math -> gcd it all
  // did you check N=1? Did you mix up N,M?
}