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

#include <bits/stdc++.h>

using namespace std;

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

constexpr int MOD = 1000000007;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

template <class T> string to_string(T s);
template <class S, class T> string to_string(pair<S, T> p);
string to_string(string s) { return s; }
string to_string(const char s[]) { return to_string(string(s)); }

template <class T> string to_string(T v) {
  if (v.empty())
    return "{}";
  string ret = "{";
  for (auto x : v)
    ret += to_string(x) + ",";
  ret.back() = '}';
  return ret;
}
template <class S, class T> string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + "," + to_string(p.second) + "}";
}

void debug() { cerr << endl; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}

struct IO {
#ifdef _WIN32
  inline char getchar_unlocked() { return getchar(); }
  inline void putchar_unlocked(char c) { putchar(c); }
#endif
  string separator = " ";
  template <class T> inline void read(T &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c != '-' && (c < '0' || '9' < c));
    bool minus = 0;
    if (c == '-') {
      minus = 1;
      c = getchar_unlocked();
    }
    x = 0;
    while ('0' <= c && c <= '9') {
      x *= 10;
      x += c - '0';
      c = getchar_unlocked();
    }
    if (minus)
      x = -x;
  }
  inline void read(string &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c == ' ' || c == '\\n');
    x.clear();
    do {
      x += c;
      c = getchar_unlocked();
    } while (c != ' ' && c != '\\n' && c != EOF);
  }
  template <class T> inline void read(vector<T> &v) {
    for (auto &x : v)
      read(x);
  }
  template <class Head, class... Tail>
  inline void read(Head &head, Tail &...tail) {
    read(head);
    read(tail...);
  }
  template <class T> inline void write(T x) {
    char buf[32];
    int p = 0;
    if (x < 0) {
      x = -x;
      putchar_unlocked('-');
    }
    if (x == 0)
      putchar_unlocked('0');
    while (x > 0) {
      buf[p++] = (x % 10) + '0';
      x /= 10;
    }
    while (p) {
      putchar_unlocked(buf[--p]);
    }
  }
  inline void write(string x) {
    for (char c : x)
      putchar_unlocked(c);
  }
  inline void write(const char s[]) {
    for (int i = 0; s[i] != 0; ++i)
      putchar_unlocked(s[i]);
  }
  template <class T> inline void write(vector<T> v) {
    for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) {
      write(*itr);
      write(separator);
    }
    write(v.back());
  }
  template <class Head, class... Tail>
  inline void write(Head head, Tail... tail) {
    write(head);
    write(separator);
    write(tail...);
  }
  template <class Head, class... Tail>
  inline void writeln(Head head, Tail... tail) {
    write(head, tail...);
    write("\\n");
  }
  void set_separator(string s) { separator = s; }
} io;

template <int MOD = 1000000007> struct Math {
  vector<long long> fact, factinv, inv;
  Math(int n = 100000) {
    fact.resize(n + 1);
    factinv.resize(n + 1);
    inv.resize(n + 1);
    fact[0] = fact[1] = 1;
    factinv[0] = factinv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i <= n; ++i) {
      fact[i] = fact[i - 1] * i % MOD;
      inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
      factinv[i] = factinv[i - 1] * inv[i] % MOD;
    }
  }
  long long C(int n, int r) {
    if (n < r || n < 0 || r < 0) {
      return 0;
    } else {
      return fact[n] * (factinv[r] * factinv[n - r] % MOD) % MOD;
    }
  }
  long long P(int n, int r) {
    if (n < r || n < 0 || r < 0) {
      return 0;
    } else {
      return fact[n] * factinv[n - r] % MOD;
    }
  }
  long long H(int n, int r) { return C(n + r - 1, r); }
};

Math<> math(200000);

vector<vector<int>> G;
vector<map<int, Pll>> dat;
vector<ll> ans;
Pll dfs1(int u, int par) {
  ll cnt = 1;
  ll pt = 1;
  for (int v : G[u]) {
    if (v != par) {
      auto t = dfs1(v, u);
      cnt += t.first;
      pt = pt * t.second % MOD;
      pt = pt * math.factinv[t.first] % MOD;
    }
  }
  pt = pt * math.fact[cnt - 1] % MOD;
  dat[u][par] = Pll(cnt, pt);
  return dat[u][par];
}
ll N;

long long extGCD(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long d = extGCD(b, a % b, y, x); // 再帰的に解く
  y -= a / b * x;
  return d;
}

inline long long mod(long long a, long long m) { return (a % m + m) % m; }

long long modinv(long long a, long long m) {
  long long x, y;
  extGCD(a, m, x, y);
  return mod(x, m);
}

void dfs2(int u, int par) {
  ll cnt = 1;
  ll pt = 1;
  ans[u] = math.fact[N - 1];
  for (int v : G[u]) {
    ans[u] = ans[u] * dat[v][u].second % MOD;
    ans[u] = ans[u] * math.factinv[dat[v][u].first] % MOD;
  }
  for (int v : G[u]) {
    if (v != par) {
      dat[u][v] =
          Pll(N - dat[v][u].first, (((ans[u] * math.factinv[N - 1] % MOD) *
                                     math.fact[N - 1 - dat[v][u].first] % MOD) *
                                    math.fact[dat[v][u].first] % MOD) *
                                       modinv(dat[v][u].second, MOD) % MOD);
      dfs2(v, u);
    }
  }
}

int main() {
  io.read(N);
  G.resize(N);
  dat.resize(N);
  ans.resize(N);
  REP(i, N - 1) {
    int a, b;
    io.read(a, b);
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }
  dfs1(0, -1);
  dfs2(0, -1);
  REP(i, N) io.writeln(ans[i]);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

constexpr int MOD = 1000000007;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

template <class T> string to_string(T s);
template <class S, class T> string to_string(pair<S, T> p);
string to_string(string s) { return s; }
string to_string(const char s[]) { return to_string(string(s)); }

template <class T> string to_string(T v) {
  if (v.empty())
    return "{}";
  string ret = "{";
  for (auto x : v)
    ret += to_string(x) + ",";
  ret.back() = '}';
  return ret;
}
template <class S, class T> string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + "," + to_string(p.second) + "}";
}

void debug() { cerr << endl; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}

struct IO {
#ifdef _WIN32
  inline char getchar_unlocked() { return getchar(); }
  inline void putchar_unlocked(char c) { putchar(c); }
#endif
  string separator = " ";
  template <class T> inline void read(T &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c != '-' && (c < '0' || '9' < c));
    bool minus = 0;
    if (c == '-') {
      minus = 1;
      c = getchar_unlocked();
    }
    x = 0;
    while ('0' <= c && c <= '9') {
      x *= 10;
      x += c - '0';
      c = getchar_unlocked();
    }
    if (minus)
      x = -x;
  }
  inline void read(string &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c == ' ' || c == '\\n');
    x.clear();
    do {
      x += c;
      c = getchar_unlocked();
    } while (c != ' ' && c != '\\n' && c != EOF);
  }
  template <class T> inline void read(vector<T> &v) {
    for (auto &x : v)
      read(x);
  }
  template <class Head, class... Tail>
  inline void read(Head &head, Tail &...tail) {
    read(head);
    read(tail...);
  }
  template <class T> inline void write(T x) {
    char buf[32];
    int p = 0;
    if (x < 0) {
      x = -x;
      putchar_unlocked('-');
    }
    if (x == 0)
      putchar_unlocked('0');
    while (x > 0) {
      buf[p++] = (x % 10) + '0';
      x /= 10;
    }
    while (p) {
      putchar_unlocked(buf[--p]);
    }
  }
  inline void write(string x) {
    for (char c : x)
      putchar_unlocked(c);
  }
  inline void write(const char s[]) {
    for (int i = 0; s[i] != 0; ++i)
      putchar_unlocked(s[i]);
  }
  template <class T> inline void write(vector<T> v) {
    for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) {
      write(*itr);
      write(separator);
    }
    write(v.back());
  }
  template <class Head, class... Tail>
  inline void write(Head head, Tail... tail) {
    write(head);
    write(separator);
    write(tail...);
  }
  template <class Head, class... Tail>
  inline void writeln(Head head, Tail... tail) {
    write(head, tail...);
    write("\\n");
  }
  void set_separator(string s) { separator = s; }
} io;

template <int MOD = 1000000007> struct Math {
  vector<long long> fact, factinv, inv;
  Math(int n = 100000) {
    fact.resize(n + 1);
    factinv.resize(n + 1);
    inv.resize(n + 1);
    fact[0] = fact[1] = 1;
    factinv[0] = factinv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i <= n; ++i) {
      fact[i] = fact[i - 1] * i % MOD;
      inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
      factinv[i] = factinv[i - 1] * inv[i] % MOD;
    }
  }
  long long C(int n, int r) {
    if (n < r || n < 0 || r < 0) {
      return 0;
    } else {
      return fact[n] * (factinv[r] * factinv[n - r] % MOD) % MOD;
    }
  }
  long long P(int n, int r) {
    if (n < r || n < 0 || r < 0) {
      return 0;
    } else {
      return fact[n] * factinv[n - r] % MOD;
    }
  }
  long long H(int n, int r) { return C(n + r - 1, r); }
};

Math<> math(200000);

vector<vector<int>> G;
vector<map<int, Pll>> dat;
vector<ll> ans;
Pll dfs1(int u, int par) {
  ll cnt = 1;
  ll pt = 1;
  for (int v : G[u]) {
    if (v != par) {
      auto t = dfs1(v, u);
      cnt += t.first;
      pt = pt * t.second % MOD;
      pt = pt * math.factinv[t.first] % MOD;
    }
  }
  pt = pt * math.fact[cnt - 1] % MOD;
  dat[u][par] = Pll(cnt, pt);
  return dat[u][par];
}
ll N;

void dfs2(int u, int par) {
  ll cnt = 1;
  ll pt = 1;
  ans[u] = math.fact[N - 1];
  for (int v : G[u]) {
    ans[u] = ans[u] * dat[v][u].second % MOD;
    ans[u] = ans[u] * math.factinv[dat[v][u].first] % MOD;
  }
  for (int v : G[u]) {
    if (v != par) {
      dat[u][v] =
          Pll(N - dat[v][u].first, (((ans[u] * math.factinv[N - 1] % MOD) *
                                     math.fact[N - 1 - dat[v][u].first] % MOD) *
                                    math.fact[dat[v][u].first] % MOD) *
                                       math.inv[dat[v][u].second] % MOD);
      dfs2(v, u);
    }
  }
}

int main() {
  io.read(N);
  G.resize(N);
  dat.resize(N);
  ans.resize(N);
  REP(i, N - 1) {
    int a, b;
    io.read(a, b);
    G[a - 1].push_back(b - 1);
    G[b - 1].push_back(a - 1);
  }
  dfs1(0, -1);
  // debug(dat);
  dfs2(0, -1);
  // debug(dat);
  // debug(ans);
  return 0;
}