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

// #include <fstream>
#include <cstring>
#include <iostream>
#include <vector>

// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define MOD 1000000007
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = (b - 1); i >= a; i--)
#define RF0(i, b) RFO(i, 0, b)
#define lli long long int
#define pii pair<int, int>
#define add(a, b) ((int)(((lli)a + b) % MOD))
#define mul(a, b) ((int)(((lli)a * b) % MOD))

// ifstream cin ("cinput.in");
// ofstream cout ("coutput.out");

int N, a, b;
vector<int> graph[200000];
int fct[200001][2];
int dp[200000];
int nbel[200000];

int mod_exp(int x, int y) {
  int res = 1;
  x = x % MOD;
  while (y > 0) {
    if (y & 1)
      res = mul(res, x);
    y = y >> 1;
    x = mul(x, x);
  }
  return res;
}

int bc(int n, int k) { return mul(mul(fct[n][0], fct[k][1]), fct[n - k][1]); }

int dfs(int node, int par) { // returns # below
  vector<int> ks;
  dp[node] = 1;
  nbel[node] = 0;
  for (int ne : graph[node]) {
    if (ne == par)
      continue;
    int res = dfs(ne, node);
    nbel[node] += res;
    ks.emplace_back(res);
  }
  int nbl2 = nbel[node];
  for (int ne : graph[node]) {
    if (ne == par)
      continue;
    int k = *(--ks.end());
    ks.pop_back();
    dp[node] = mul(mul(dp[ne], bc(nbl2, k)), dp[node]);
    nbl2 -= k;
  }
  return nbel[node] + 1;
}

void dfs2(int node, int par) {
  if (par != -1) {
    // dp[par]/bc(N-1, nbel[node]+1)*bc(N,N-nbel[node]-1)
    dp[node] = mul(dp[par], mul(mod_exp(bc(N - 1, nbel[node] + 1), MOD - 2),
                                bc(N - 1, N - nbel[node] - 1)));
  }
  for (int ne : graph[node]) {
    if (ne == par)
      continue;
    dfs2(ne, node);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  fct[0][0] = 1;
  fct[0][1] = mod_exp(1, MOD - 2);
  FOR(i, 1, 200001) {
    fct[i][0] = mul(fct[i - 1][0], i);
    fct[i][1] = mod_exp(fct[i][0], MOD - 2);
  }
  cin >> N;
  F0R(i, N - 1) {
    cin >> a >> b;
    graph[a - 1].emplace_back(b - 1);
    graph[b - 1].emplace_back(a - 1);
  }
  dfs(0, -1);
  dfs2(0, -1);
  F0R(i, N) cout << dp[i] << '\\n';
}

Error: 0

Fixed submission:

// #include <fstream>
#include <cstring>
#include <iostream>
#include <vector>

// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;

#define MOD 1000000007
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = (b - 1); i >= a; i--)
#define RF0(i, b) RFO(i, 0, b)
#define lli long long int
#define pii pair<int, int>
#define add(a, b) ((int)(((lli)a + b) % MOD))
#define mul(a, b) ((int)(((lli)a * b) % MOD))

// ifstream cin ("cinput.in");
// ofstream cout ("coutput.out");

int N, a, b;
vector<int> graph[200000];
int fct[200000][2];
int dp[200000];
int nbel[200000];

int mod_exp(int x, int y) {
  int res = 1;
  x = x % MOD;
  while (y > 0) {
    if (y & 1)
      res = mul(res, x);
    y = y >> 1;
    x = mul(x, x);
  }
  return res;
}

int bc(int n, int k) { return mul(mul(fct[n][0], fct[k][1]), fct[n - k][1]); }

int dfs(int node, int par) { // returns # below
  vector<int> ks;
  dp[node] = 1;
  nbel[node] = 0;
  for (int ne : graph[node]) {
    if (ne == par)
      continue;
    int res = dfs(ne, node);
    nbel[node] += res;
    ks.emplace_back(res);
  }
  int nbl2 = nbel[node];
  for (int ne : graph[node]) {
    if (ne == par)
      continue;
    int k = *(--ks.end());
    ks.pop_back();
    dp[node] = mul(mul(dp[ne], bc(nbl2, k)), dp[node]);
    nbl2 -= k;
  }
  return nbel[node] + 1;
}

void dfs2(int node, int par) {
  if (par != -1) {
    // dp[par]/bc(N-1, nbel[node]+1)*bc(N,N-nbel[node]-1)
    dp[node] = mul(dp[par], mul(mod_exp(bc(N - 1, nbel[node] + 1), MOD - 2),
                                bc(N - 1, N - nbel[node] - 1)));
  }
  for (int ne : graph[node]) {
    if (ne == par)
      continue;
    dfs2(ne, node);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  fct[0][0] = 1;
  fct[0][1] = mod_exp(1, MOD - 2);
  FOR(i, 1, 200001) {
    fct[i][0] = mul(fct[i - 1][0], i);
    fct[i][1] = mod_exp(fct[i][0], MOD - 2);
  }
  cin >> N;
  F0R(i, N - 1) {
    cin >> a >> b;
    graph[a - 1].emplace_back(b - 1);
    graph[b - 1].emplace_back(a - 1);
  }
  dfs(0, -1);
  dfs2(0, -1);
  F0R(i, N) cout << dp[i] << '\\n';
}