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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <assert.h>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) (min(a, b) <= (x) && (x) <= max(a, b)) // hei
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define INF 10000000000000000LL
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

struct Edge {
  int from, to;
  ll w;
  bool operator<(const Edge &rhs) const {
    return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
  }
};
typedef vector<vector<Edge>> Graph;
typedef unsigned long long ull;

ll facmemo[323456] = {};
ll factorial(int n) { // 階乗
  if (n > 0) {
    assert(n < 323456);
    if (facmemo[n] != 0)
      return facmemo[n];
    return facmemo[n] = (n * factorial(n - 1)) % MODU;
  } else {
    return 1;
  }
}
ll mod_pow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = (res * x) % MODU;
    x = (x * x) % MODU;
    n >>= 1;
  }
  return res;
}

ll divmod(ll a, ll b) { // a/b(mod m)
                        // a/b (mod m)
                        // a*b^-1
                        // b^-1 * b = 1
                        // b^(m-1) = 1
                        // b^(m-2) * b = 1
                        // b^-1 = b^(m-2)

  // a/b = a * b^(m-2)
  // if(bmemo[b] == -1) bmemo[b] = mod_pow(b, MODU - 2);
  return (a * mod_pow(b, MODU - 2)) % MODU;
}

ll ncr(ll n, ll r) {
  if (n < r || r < -1)
    return 1;
  return divmod(factorial(n), (factorial(r) * (factorial(n - r))) % MODU);
}

int main() {
  int n;
  cin >> n;
  Graph g(n);
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back({a, b});
    g[b].push_back({b, a});
  }

  vector<ll> dp(n), ans(n), sz(n);

  function<ll(int, int)> szdfs = [&](int cur, int par) {
    ll ret = 1;
    for (auto itr : g[cur]) {
      if (itr.to == par)
        continue;
      ret += szdfs(itr.to, cur);
    }
    return sz[cur] = ret;
  };

  szdfs(0, 0);

  function<ll(int, int)> dfs1 = [&](int cur, int par) {
    ll ret = 1;
    int rest = sz[cur] - 1;
    for (auto itr : g[cur]) {
      if (itr.to == par)
        continue;
      ret *= ncr(rest, sz[itr.to]);
      ret %= MODU;
      rest -= sz[itr.to];
      ret *= dfs1(itr.to, cur);
      ret %= MODU;
    }
    return dp[cur] = ret;
  };

  dfs1(0, 0);

  function<void(int, int, ll)> dfs2 = [&](int cur, int par, ll oya) {
    ll cans = 1;
    int rest = n - 1;
    for (auto itr : g[cur]) {
      if (itr.to == par)
        continue;
      cans *= (ncr(rest, sz[itr.to]) * dp[itr.to]) % MODU;
      cans %= MODU;

      rest -= sz[itr.to];
    }
    cans *= oya;
    cans %= MODU;
    ans[cur] = cans;

    ll noya = cans;
    for (auto itr : g[cur]) {
      if (itr.to == par)
        continue;
      dfs2(itr.to, cur,
           divmod(noya, (ncr(n - 1, sz[itr.to]) * dp[itr.to]) % MODU));
    }
  };

  dfs2(0, 0, 1);

  REP(i, n) { cout << ans[i] << endl; }
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) (min(a, b) <= (x) && (x) <= max(a, b)) // hei
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007LL
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

//[a, b)
#define Getsum(ar, a, b) (ar[b] - ar[a])
#define INF 10000000000000000LL
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)

struct Edge {
  int from, to;
  ll w;
  bool operator<(const Edge &rhs) const {
    return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
  }
};
typedef vector<vector<Edge>> Graph;
typedef unsigned long long ull;

ll facmemo[323456] = {};
ll factorial(int n) { // 階乗
  if (n > 0) {
    if (facmemo[n] != 0)
      return facmemo[n];
    return facmemo[n] = (n * factorial(n - 1)) % MODU;
  } else {
    return 1;
  }
}
ll mod_pow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MODU;
    x = x * x % MODU;
    n >>= 1;
  }
  return res;
}

ll bmemo[323456] = {};

ll divmod(ll a, ll b) { // a/b(mod m)
                        // a/b (mod m)
                        // a*b^-1
                        // b^-1 * b = 1
                        // b^(m-1) = 1
                        // b^(m-2) * b = 1
                        // b^-1 = b^(m-2)

  // a/b = a * b^(m-2)
  if (bmemo[b] == -1)
    bmemo[b] = mod_pow(b, MODU - 2);
  return (a * bmemo[b]) % MODU;
}

ll ncr(ll n, ll r) {
  if (n < r || r < -1)
    return 1;
  return divmod(factorial(n), (factorial(r) * (factorial(n - r))));
}

int main() {
  fill(begin(bmemo), end(bmemo), -1);

  int n;
  cin >> n;
  Graph g(n);
  REP(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back({a, b});
    g[b].push_back({b, a});
  }

  vector<ll> dp(n), ans(n), sz(n);

  function<ll(int, int)> szdfs = [&](int cur, int par) {
    ll ret = 1;
    for (auto itr : g[cur]) {
      if (itr.to == par)
        continue;
      ret += szdfs(itr.to, cur);
    }
    return sz[cur] = ret;
  };

  szdfs(0, 0);

  function<ll(int, int)> dfs1 = [&](int cur, int par) {
    ll ret = 1;
    int rest = sz[cur] - 1;
    for (auto itr : g[cur]) {
      if (itr.to == par)
        continue;
      ret *= ncr(rest, sz[itr.to]);
      ret %= MODU;
      rest -= sz[itr.to];
      ret *= dfs1(itr.to, cur);
      ret %= MODU;
    }
    return dp[cur] = ret;
  };

  dfs1(0, 0);

  function<void(int, int, ll)> dfs2 = [&](int cur, int par, ll oya) {
    ll cans = 1;
    int rest = n - 1;
    for (auto itr : g[cur]) {
      if (itr.to == par)
        continue;
      cans *= (ncr(rest, sz[itr.to]) * dp[itr.to]) % MODU;
      cans %= MODU;

      rest -= sz[itr.to];
    }
    cans *= oya;
    cans %= MODU;
    ans[cur] = cans;

    ll noya = cans;
    ll head = 1;
    for (auto itr : g[cur]) {
      if (itr.to == par)
        continue;
      dfs2(itr.to, cur,
           divmod(noya, (ncr(n - 1, sz[itr.to]) * dp[itr.to]) % MODU));
    }
  };

  dfs2(0, 0, 1);

  REP(i, n) { cout << ans[i] << endl; }
}