Problem p03492 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 1000000000
#define LLINF 1000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define EQ(a, b) (abs((a) - (b)) < eps)
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
#define MOD 1000000007ll // prime
// How to devide :
// ModInt a(6ll);
// ModInt b(2ll);
// a *= b.exp(MOD-2ll);  -> a/=b;  result: a = 3
struct ModInt {
  ll val;
  ModInt() : val(0ll) {}
  ModInt(ll v) : val(((v % MOD) + MOD) % MOD) {}
  ModInt exp(ll y) const {
    if (!y)
      return ModInt(1ll);
    ModInt a = exp(y / 2ll);
    a *= a;
    if (y & 1)
      a *= (*this);
    return a;
  }
  bool operator==(const ModInt &x) const { return val == x.val; }
  inline bool operator!=(const ModInt &x) const { return !(*this == x); }
  bool operator<(const ModInt &x) const { return val < x.val; }
  bool operator>(const ModInt &x) const { return val > x.val; }
  inline bool operator>=(const ModInt &x) const { return !(*this < x); }
  inline bool operator<=(const ModInt &x) const { return !(*this > x); }
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val += MOD - x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    (val *= x.val) %= MOD;
    return *this;
  }
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
};
istream &operator>>(istream &i, ModInt &x) {
  i >> x.val;
  return i;
}
ostream &operator<<(ostream &o, const ModInt &x) {
  o << x.val;
  return o;
}
ModInt pow(ModInt a, ll x) {
  ModInt res = ModInt(1ll);
  while (x) {
    if (x & 1)
      res *= a;
    x >>= 1;
    a = a * a;
  }
  return res;
}
const int SIZE = 100100;
ModInt inv[SIZE + 10], fac[SIZE + 10], facinv[SIZE + 10];
// notice: 0C0 = 1
ModInt nCr(int n, int r) {
  assert(!(n < r));
  assert(!(n < 0 || r < 0));
  return fac[n] * facinv[r] * facinv[n - r];
}
void init() {
  fac[0] = ModInt(1ll);
  for (int i = 1; i <= SIZE; i++)
    fac[i] = fac[i - 1] * ModInt(i);
  inv[1] = ModInt(1ll);
  for (int i = 2; i <= SIZE; i++)
    inv[i] = ModInt(0ll) - ModInt(MOD / i) * inv[MOD % i];
  facinv[0] = ModInt(1ll);
  for (int i = 1; i <= SIZE; i++)
    facinv[i] = facinv[i - 1] * inv[i];
  return;
}
const int MAX_V = 200100;
struct edge {
  int from, to;
  edge() {}
  edge(int f, int t) : from(f), to(t) {}
};
struct Tree {
  int V;
  int root;
  vector<int> depth;
  vector<int> par;
  vector<int> subtree_size;
  vector<int> centroid;
  vector<vector<edge>> G;
  Tree() {}
  Tree(int v, int r = 0) {
    V = v;
    root = r;
    G.resize(V);
    depth.resize(V);
    par.resize(V);
    subtree_size.resize(V);
  }
  void add_edge(int u, int v) {
    G[u].pb(edge(u, v));
    G[v].pb(edge(v, u));
  }
  int dfs(int v, int p, int d) {
    depth[v] = d;
    par[v] = p;
    subtree_size[v] = 1;
    bool is_centroid = true;
    for (int i = 0; i < G[v].size(); i++) {
      int u = G[v][i].to;
      if (u == p)
        continue;
      subtree_size[v] += dfs(u, v, d + 1);
      if (subtree_size[u] > V / 2)
        is_centroid = false;
    }
    if (V - subtree_size[v] > V / 2)
      is_centroid = false;
    if (is_centroid)
      centroid.pb(v);
    return subtree_size[v];
  }
  void dfs() { dfs(root, -1, 0); }
};
ModInt dp[5010][5010];
int N;
int main() {
  init();
  cin >> N;
  Tree G(N);
  for (int i = 0; i < N - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G.add_edge(x, y);
  }
  G.dfs();
  if (G.centroid.size() == 2) {
    assert(N % 2 == 0);
    cout << fac[N / 2] * fac[N / 2] << endl;
    return 0;
  }
  int c = G.centroid[0];
  vector<int> vec;
  for (auto v : G.G[c]) {
    if (v.to != G.par[c])
      vec.pb(G.subtree_size[v.to]);
  }
  vec.pb(N - G.subtree_size[c]);
  int K = vec.size();
  dp[0][0] = ModInt(1ll);
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k <= vec[i]; k++) {
        dp[i + 1][j] += dp[i][j - k] * nCr(vec[i], k) * nCr(vec[i], k) * fac[k];
      }
    }
  }
  ModInt ans = fac[N];
  for (int i = 1; i < N; i++) {
    if (i & 1)
      ans -= fac[N - i] * dp[K][i];
    else
      ans += fac[N - i] * dp[K][i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 1000000000
#define LLINF 1000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define repn(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define EQ(a, b) (abs((a) - (b)) < eps)
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}
#define MOD 1000000007ll // prime
// How to devide :
// ModInt a(6ll);
// ModInt b(2ll);
// a *= b.exp(MOD-2ll);  -> a/=b;  result: a = 3
struct ModInt {
  ll val;
  ModInt() : val(0ll) {}
  ModInt(ll v) : val(((v % MOD) + MOD) % MOD) {}
  ModInt exp(ll y) const {
    if (!y)
      return ModInt(1ll);
    ModInt a = exp(y / 2ll);
    a *= a;
    if (y & 1)
      a *= (*this);
    return a;
  }
  bool operator==(const ModInt &x) const { return val == x.val; }
  inline bool operator!=(const ModInt &x) const { return !(*this == x); }
  bool operator<(const ModInt &x) const { return val < x.val; }
  bool operator>(const ModInt &x) const { return val > x.val; }
  inline bool operator>=(const ModInt &x) const { return !(*this < x); }
  inline bool operator<=(const ModInt &x) const { return !(*this > x); }
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val += MOD - x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    (val *= x.val) %= MOD;
    return *this;
  }
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
};
istream &operator>>(istream &i, ModInt &x) {
  i >> x.val;
  return i;
}
ostream &operator<<(ostream &o, const ModInt &x) {
  o << x.val;
  return o;
}
ModInt pow(ModInt a, ll x) {
  ModInt res = ModInt(1ll);
  while (x) {
    if (x & 1)
      res *= a;
    x >>= 1;
    a = a * a;
  }
  return res;
}
const int SIZE = 100100;
ModInt inv[SIZE + 10], fac[SIZE + 10], facinv[SIZE + 10];
// notice: 0C0 = 1
ModInt nCr(int n, int r) {
  assert(!(n < r));
  assert(!(n < 0 || r < 0));
  return fac[n] * facinv[r] * facinv[n - r];
}
void init() {
  fac[0] = ModInt(1ll);
  for (int i = 1; i <= SIZE; i++)
    fac[i] = fac[i - 1] * ModInt(i);
  inv[1] = ModInt(1ll);
  for (int i = 2; i <= SIZE; i++)
    inv[i] = ModInt(0ll) - ModInt(MOD / i) * inv[MOD % i];
  facinv[0] = ModInt(1ll);
  for (int i = 1; i <= SIZE; i++)
    facinv[i] = facinv[i - 1] * inv[i];
  return;
}
const int MAX_V = 200100;
struct edge {
  int from, to;
  edge() {}
  edge(int f, int t) : from(f), to(t) {}
};
struct Tree {
  int V;
  int root;
  vector<int> depth;
  vector<int> par;
  vector<int> subtree_size;
  vector<int> centroid;
  vector<vector<edge>> G;
  Tree() {}
  Tree(int v, int r = 0) {
    V = v;
    root = r;
    G.resize(V);
    depth.resize(V);
    par.resize(V);
    subtree_size.resize(V);
  }
  void add_edge(int u, int v) {
    G[u].pb(edge(u, v));
    G[v].pb(edge(v, u));
  }
  int dfs(int v, int p, int d) {
    depth[v] = d;
    par[v] = p;
    subtree_size[v] = 1;
    bool is_centroid = true;
    for (int i = 0; i < G[v].size(); i++) {
      int u = G[v][i].to;
      if (u == p)
        continue;
      subtree_size[v] += dfs(u, v, d + 1);
      if (subtree_size[u] > V / 2)
        is_centroid = false;
    }
    if (V - subtree_size[v] > V / 2)
      is_centroid = false;
    if (is_centroid)
      centroid.pb(v);
    return subtree_size[v];
  }
  void dfs() { dfs(root, -1, 0); }
};
ModInt dp[5010][5010];
int N;
int main() {
  init();
  cin >> N;
  Tree G(N);
  for (int i = 0; i < N - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G.add_edge(x, y);
  }
  G.dfs();
  if (G.centroid.size() == 2) {
    assert(N % 2 == 0);
    cout << fac[N / 2] * fac[N / 2] << endl;
    return 0;
  }
  int c = G.centroid[0];
  vector<int> vec;
  for (auto v : G.G[c]) {
    if (v.to != G.par[c])
      vec.pb(G.subtree_size[v.to]);
  }
  vec.pb(N - G.subtree_size[c]);
  int K = vec.size();
  dp[0][0] = ModInt(1ll);
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k <= vec[i]; k++) {
        if (j < k)
          continue;
        dp[i + 1][j] += dp[i][j - k] * nCr(vec[i], k) * nCr(vec[i], k) * fac[k];
      }
    }
  }
  ModInt ans = fac[N];
  for (int i = 1; i < N; i++) {
    if (i & 1)
      ans -= fac[N - i] * dp[K][i];
    else
      ans += fac[N - i] * dp[K][i];
  }
  cout << ans << endl;
  return 0;
}