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 repr(i, n) for (int i = (n - 1); i >= 0; --i)

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

struct mint {
  long long x;
  mint(long long x = 0) : x(x % MOD + (0 <= x ? 0 : 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 -= a.x) < 0)
      x += MOD;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint &a) const { return mint(x) += a; }
  mint operator-(const mint &a) const { return mint(x) -= a; }
  mint operator*(const mint &a) const { return mint(x) *= a; }

  // for prime mod
  mint inv() const {
    long long a = x, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return mint(u);
  }
  mint &operator/=(const mint &a) { return *this *= a.inv(); }
  mint operator/(const mint &a) const { return mint(x) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

ll fac[MAX], finv[MAX], inv[MAX];
void init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// グラフ
vector<int> G[MAX];

// データの構造体
struct Data {
  mint a;
  int s;
};

Data f(Data x, Data y) {
  mint res = x.a * y.a;
  res *= fac[x.s + y.s];
  res /= fac[x.s] * fac[y.s];
  return Data{res, x.s + y.s};
}

// dp[i][j]:=頂点iの結果
vector<Data> dp[MAX];

// deg[i]:=頂点iの子の数
int deg[MAX];

// pi[i]:=頂点iの親の index
int pi[MAX];

// ans[i]:=頂点iの解
mint ans[MAX];

Data dfs1(int v, int p = -1) {
  deg[v] = G[v].size();
  dp[v].resize(deg[v]);

  Data res = {1, 0};
  rep(i, deg[v]) {
    int u = G[v][i];
    if (u == p) {
      pi[v] = i;
      continue;
    }
    dp[v][i] = dfs1(u, v);
    res = f(res, dp[v][i]);
  }
  res.s++;
  return res;
}

void dfs2(int v, Data p_res = Data{1, 0}, int p = -1) {
  if (p != -1)
    dp[v][pi[v]] = p_res;

  vector<Data> dpl(deg[v] + 1);
  dpl[0].a = 1;
  rep(i, deg[v]) dpl[i + 1] = f(dpl[i], dp[v][i]);

  vector<Data> dpr(deg[v] + 1);
  dpr[deg[v]].a = 1;
  repr(i, deg[v]) dpr[i] = f(dpr[i + 1], dp[v][i]);

  ans[v] = dpr[0].a;

  rep(i, deg[v]) {
    int u = G[v][i];
    if (u == p)
      continue;
    Data d = f(dpl[i], dpr[i + 1]);
    d.s++;
    dfs2(u, d, v);
  }
}

int main() {
  int N;
  cin >> N;

  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  init();
  dfs1(0);
  dfs2(0);

  rep(i, N) { cout << ans[i] << endl; }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

typedef long long ll;
typedef pair<int, int> P;

// const int MAX = 200005;
const int MAX = 40;
const int INF = 1001001001;
const int MOD = 1000000007;

struct mint {
  long long x;
  mint(long long x = 0) : x(x % MOD + (0 <= x ? 0 : 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 -= a.x) < 0)
      x += MOD;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint &a) const { return mint(x) += a; }
  mint operator-(const mint &a) const { return mint(x) -= a; }
  mint operator*(const mint &a) const { return mint(x) *= a; }

  // for prime mod
  mint inv() const {
    long long a = x, b = MOD, u = 1, v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return mint(u);
  }
  mint &operator/=(const mint &a) { return *this *= a.inv(); }
  mint operator/(const mint &a) const { return mint(x) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

ll fac[MAX], finv[MAX], inv[MAX];
void init() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// グラフ
vector<int> G[MAX];

// データの構造体
struct Data {
  mint a;
  int s;
};

Data f(Data x, Data y) {
  mint res = x.a * y.a;
  res *= fac[x.s + y.s];
  res /= fac[x.s] * fac[y.s];
  return Data{res, x.s + y.s};
}

// dp[i][j]:=頂点iの結果
vector<Data> dp[MAX];

// deg[i]:=頂点iの子の数
int deg[MAX];

// pi[i]:=頂点iの親の index
int pi[MAX];

// ans[i]:=頂点iの解
mint ans[MAX];

Data dfs1(int v, int p = -1) {
  deg[v] = G[v].size();
  dp[v].resize(deg[v]);

  Data res = {1, 0};
  rep(i, deg[v]) {
    int u = G[v][i];
    if (u == p) {
      pi[v] = i;
      continue;
    }
    dp[v][i] = dfs1(u, v);
    res = f(res, dp[v][i]);
  }
  res.s++;
  return res;
}

void dfs2(int v, Data p_res = Data{1, 0}, int p = -1) {
  if (p != -1)
    dp[v][pi[v]] = p_res;

  vector<Data> dpl(deg[v] + 1);
  dpl[0].a = 1;
  rep(i, deg[v]) dpl[i + 1] = f(dpl[i], dp[v][i]);

  vector<Data> dpr(deg[v] + 1);
  dpr[deg[v]].a = 1;
  repr(i, deg[v]) dpr[i] = f(dpr[i + 1], dp[v][i]);

  ans[v] = dpr[0].a;

  rep(i, deg[v]) {
    int u = G[v][i];
    if (u == p)
      continue;
    Data d = f(dpl[i], dpr[i + 1]);
    d.s++;
    dfs2(u, d, v);
  }
}

int main() {
  int N;
  cin >> N;

  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  init();
  dfs1(0);
  dfs2(0);

  rep(i, N) { cout << ans[i] << endl; }
}