Problem p02728 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<ll, Pll> PlP;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

const int mod = 1000000007;
class mint {
  long long x;

public:
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  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;
  }
};

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(200010);

ll N;
vector<ll> G[200010];
vector<vector<ll>> memo(200010);
map<ll, ll> mp[200010];
/*ll dfs(ll v, ll n){
    if(n == N){
        return 1?0;
    }
    ll ncan = G[v].size();
    comb.fact[]*comb.ifact[ncan];
}*/

ll dfs(ll v, ll p) {
  // vector<ll> abc(ll(G[v].size()));
  memo[v] = vector<ll>(ll(G[v].size()));
  // cout << "v " << v << endl;

  // memo[v].push_back(1LL);
  // rep(i,G[v].size()){
  //     memo[v].push_back(1);
  // }
  ll res = 1;
  ll deg = G[v].size();
  rep(i, deg) {
    ll u = G[v][i];
    if (u == p)
      continue;
    memo[v][i] = dfs(u, v);
    res += memo[v][i];
  }
  // memo[v] = res;
  return res;
}

void bfs(ll v, ll p) {
  ll deg = G[v].size();
  ll sum = 1;
  rep(i, deg) {
    ll u = G[v][i];
    // if(u == p) continue;
    sum += memo[v][i];
  }
  rep(i, deg) {
    ll u = G[v][i];
    if (u == p)
      continue;
    // cout << "a " << i << endl;
    memo[u][mp[u][v]] = sum - memo[v][i]; // kokokokokokok
    bfs(u, v);
  }
}

vector<mint> dp[200010];

mint efs(ll v, ll p) {
  dp[v] = vector<mint>(G[v].size(), mint(1));
  ll sum = 0;
  mint res = 1;
  ll deg = G[v].size();
  rep(i, deg) {
    ll u = G[v][i];
    if (u == p)
      continue;
    sum += memo[v][i];
  }
  rep(i, deg) {
    ll u = G[v][i];
    if (u == p)
      continue;
    res *= comb(sum, memo[v][i]);
    sum -= memo[v][i];
    dp[v][i] *= efs(u, v);
    res *= dp[v][i];
  }
  return res;
}

void cfs(ll v, ll p) {
  ll deg = G[v].size();
  ll sum = 0;
  mint acc = 1;
  mint res = 1;
  rep(i, deg) {
    sum += memo[v][i];
    acc *= dp[v][i];
  }
  ll tmp_sum = sum;
  rep(i, deg) {
    res *= comb(tmp_sum, memo[v][i]);
    tmp_sum -= memo[v][i];
  }
  res *= acc;
  rep(i, deg) {
    ll u = G[v][i];
    if (u == p)
      continue;
    // res *= comb(sum, memo[v][i]);
    // sum -= memo[v][i];
    dp[u][mp[u][v]] = (res / comb(sum, memo[v][i])) / dp[v][i];
    cfs(u, v);
  }
}

int main() {
  cin >> N;
  ll a, b;
  rep(i, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    mp[a][b] = ll(G[a].size()) - 1;
    G[b].push_back(a);
    mp[b][a] = ll(G[b].size()) - 1;
  }
  dfs(0, -1);
  /*rep(i,N){
      rep(j,G[i].size()){
          cout << i+1 << " " << G[i][j]+1 << " "<< memo[i][j] << endl;
      }
  }*/
  bfs(0, -1);
  efs(0, -1);
  cfs(0, -1);
  /*rep(i,N){
      rep(j,G[i].size()){
          cout << i + 1 << " " << G[i][j] + 1 << " "<< memo[i][j] << endl;
      }
  }*/

  rep(i, N) {
    mint ans = 1;
    ll sum = 0;
    ll deg = memo[i].size();
    rep(j, deg) { sum += memo[i][j]; }
    rep(j, deg) {
      ans *= dp[i][j];
      ans *= comb(sum, memo[i][j]);
      sum -= memo[i][j];
      // cout << dp[i][j] << endl;
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<ll, Pll> PlP;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

const int mod = 1000000007;
class mint {
  long long x;

public:
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint &a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint &a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint &a) { return (*this) *= a.inv(); }
  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;
  }
};

struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
} comb(200010);

ll N;
vector<vector<ll>> G(200010);
vector<vector<ll>> memo(200010);
vector<map<ll, ll>> mp(200010);
/*ll dfs(ll v, ll n){
    if(n == N){
        return 1?0;
    }
    ll ncan = G[v].size();
    comb.fact[]*comb.ifact[ncan];
}*/

ll dfs(ll v, ll p) {
  memo[v] = vector<ll>(G[v].size());
  ll res = 1;
  ll deg = G[v].size();
  rep(i, deg) {
    ll u = G[v][i];
    if (u == p)
      continue;
    memo[v][i] = dfs(u, v);
    res += memo[v][i];
  }
  // memo[v] = res;
  return res;
}
ll bfs(ll v, ll p) {
  ll deg = G[v].size();
  ll sum = 1;
  rep(i, deg) {
    ll u = G[v][i];
    // if(u == p) continue;
    sum += memo[v][i];
  }
  // cout << sum << endl;
  rep(i, deg) {
    ll u = G[v][i];
    if (u == p)
      continue;
    // cout << "a " << i << endl;
    memo[u][mp[u][v]] = sum - memo[v][i]; // kokokokokokok
    bfs(u, v);
  }
}

vector<vector<mint>> dp(200010);

mint efs(ll v, ll p) {
  dp[v] = vector<mint>(G[v].size(), mint(1));
  ll sum = 0;
  mint res = 1;
  ll deg = G[v].size();
  rep(i, deg) {
    ll u = G[v][i];
    if (u == p)
      continue;
    sum += memo[v][i];
  }
  rep(i, deg) {
    ll u = G[v][i];
    if (u == p)
      continue;
    res *= comb(sum, memo[v][i]);
    sum -= memo[v][i];
    dp[v][i] *= efs(u, v);
    res *= dp[v][i];
  }
  return res;
}

ll cfs(ll v, ll p) {
  ll deg = G[v].size();
  ll sum = 0;
  mint acc = 1;
  mint res = 1;
  rep(i, deg) {
    sum += memo[v][i];
    acc *= dp[v][i];
  }
  ll tmp_sum = sum;
  rep(i, deg) {
    res *= comb(tmp_sum, memo[v][i]);
    tmp_sum -= memo[v][i];
  }
  res *= acc;
  rep(i, deg) {
    ll u = G[v][i];
    if (u == p)
      continue;
    // res *= comb(sum, memo[v][i]);
    // sum -= memo[v][i];
    dp[u][mp[u][v]] = (res / comb(sum, memo[v][i])) / dp[v][i];
    cfs(u, v);
  }
}

int main() {
  cin >> N;
  ll a, b;
  rep(i, N - 1) {
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    mp[a][b] = G[a].size() - 1;
    G[b].push_back(a);
    mp[b][a] = G[b].size() - 1;
  }
  dfs(0, -1);
  /*rep(i,N){
      rep(j,G[i].size()){
          cout << i+1 << " " << G[i][j]+1 << " "<< memo[i][j] << endl;
      }
  }*/
  bfs(0, -1);
  efs(0, -1);
  cfs(0, -1);
  /*rep(i,N){
      rep(j,G[i].size()){
          cout << i + 1 << " " << G[i][j] + 1 << " "<< memo[i][j] << endl;
      }
  }*/
  rep(i, N) {
    mint ans = 1;
    ll sum = 0;
    ll deg = memo[i].size();
    rep(j, deg) { sum += memo[i][j]; }
    rep(j, deg) {
      ans *= dp[i][j];
      ans *= comb(sum, memo[i][j]);
      sum -= memo[i][j];
      // cout << dp[i][j] << endl;
    }
    cout << ans << endl;
  }
}