Problem p02728 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #include<bits/stdc++.h>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int mod = 1e9 + 7;
const int IMAX = 200010;
constexpr ll modpow(ll A, ll B) {
  ll kotae = 1;
  while (B > 0) {
    if (B & 1)
      kotae = kotae * A % mod;
    A = A * A % mod;
    B >>= 1;
  }
  return kotae;
}
class unko {
public:
  int fac[IMAX + 1], ifac[IMAX + 1], inv[IMAX + 1];
  constexpr unko() : fac(), ifac(), inv() {
    fac[0] = 1;
    rep1(i, IMAX) fac[i] = ll(fac[i - 1]) * i % mod;
    ifac[IMAX] = modpow(fac[IMAX], mod - 2);
    for (int i = IMAX; i > 0; i--)
      ifac[i - 1] = ll(ifac[i]) * i % mod;
    inv[0] = 1;
    rep1(i, IMAX) inv[i] = ll(ifac[i]) * fac[i - 1] % mod;
  }
  int comb(int A, int B) {
    return ll(fac[A]) * ifac[B] % mod * ifac[A - B] % mod;
  }
} f;
const int MAX = 120000;
class shuturyoku_unko {
public:
  char C[MAX * 6 + 10];
  int K[MAX + 10];
  constexpr shuturyoku_unko() : C(), K() {
    auto di = C + 1;
    C[0] = '0';
    K[0] = 0;
    K[1] = 1;
    rep1(i, MAX) {
      int X = i;
      int keta = 0;
      char c[10] = {};
      while (X) {
        *(c + keta) = '0' + X % 10;
        X /= 10;
        keta++;
      }
      for (int j = keta - 1; j >= 0; j--)
        *di++ = (*(c + j));
      K[i + 1] = K[i] + keta;
    }
  }
} g;

int N;
const int VN = 200000;
int to[VN * 2 + 2], ne[VN * 2 + 2], he[VN + 1];
int n[VN + 1];
void dfs(int v) {
  n[v]++;
  int mae = 0;
  for (int i = he[v]; i; i = ne[i]) {
    int t = to[i];
    if (n[t]) {
      if (mae)
        ne[mae] = ne[i];
      else
        he[v] = ne[i];
    } else {
      dfs(t);
      n[v] += n[t];
    }
    mae = i;
  }
}
void dfs2(int v) {
  for (int i = he[v]; i; i = ne[i]) {
    int t = to[i];
    n[t] = ll(n[v]) * n[t] % mod * f.inv[N - n[t]] % mod;
    dfs2(t);
  }
}

const int CM = 2200000;
char cn[CM], *ci = cn;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
  ll tmp = *(ll *)ci;
  int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
  tmp = tmp << dig & ma1;
  tmp = tmp * 10 + (tmp >> 8) & ma2;
  tmp = tmp * 100 + (tmp >> 16) & ma3;
  tmp = tmp * 10000 + (tmp >> 32) & ma4;
  ci += (64 - dig >> 3) + 1;
  return tmp;
}
char *di = cn;
inline void putint(int X) {
  int ue = X / 100000;
  if (ue) {
    for (int i = g.K[ue]; i < g.K[ue + 1]; i++)
      *di++ = g.C[i];
    int sita = X - ue * 100000ll;
    rep(i, 5 - g.K[sita + 1] + g.K[sita]) *di++ = '0';
    for (int i = g.K[sita]; i < g.K[sita + 1]; i++)
      *di++ = g.C[i];
  } else
    for (int i = g.K[X]; i < g.K[X + 1]; i++)
      *di++ = g.C[i];
  *di++ = '\n';
}

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  fread(cn, 1, CM, stdin);
  N = getint();
  int k = 2;
  rep1(i, N - 1) {
    int a = getint(), b = getint();

    to[k] = b;
    ne[k] = he[a];
    he[a] = k++;
    to[k] = a;
    ne[k] = he[b];
    he[b] = k++;
  }
  dfs(1);
  ll tmp = 1;
  rep1(i, N) tmp = (tmp * n[i]) % mod;
  n[1] = modpow(tmp, mod - 2) * f.fac[N] % mod;
  for (int i = he[1]; i; i = ne[i]) {
    int t = to[i];
    n[t] = ll(n[1]) * n[t] % mod * f.inv[N - n[t]] % mod;
    dfs2(t);
  }
  rep1(i, N) putint(n[i]);
  fwrite(cn, 1, di - cn, stdout);

  Would you please return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #include<bits/stdc++.h>
#include <cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int mod = 1e9 + 7;
const int IMAX = 200010;
constexpr ll modpow(ll A, ll B) {
  ll kotae = 1;
  while (B > 0) {
    if (B & 1)
      kotae = kotae * A % mod;
    A = A * A % mod;
    B >>= 1;
  }
  return kotae;
}
class unko {
public:
  int fac[IMAX + 1], ifac[IMAX + 1], inv[IMAX + 1];
  constexpr unko() : fac(), ifac(), inv() {
    fac[0] = 1;
    rep1(i, IMAX) fac[i] = ll(fac[i - 1]) * i % mod;
    ifac[IMAX] = modpow(fac[IMAX], mod - 2);
    for (int i = IMAX; i > 0; i--)
      ifac[i - 1] = ll(ifac[i]) * i % mod;
    inv[0] = 1;
    rep1(i, IMAX) inv[i] = ll(ifac[i]) * fac[i - 1] % mod;
  }
  int comb(int A, int B) {
    return ll(fac[A]) * ifac[B] % mod * ifac[A - B] % mod;
  }
} f;
const int MAX = 120000;
class shuturyoku_unko {
public:
  char C[MAX * 6 + 10];
  int K[MAX + 10];
  constexpr shuturyoku_unko() : C(), K() {
    auto di = C + 1;
    C[0] = '0';
    K[0] = 0;
    K[1] = 1;
    rep1(i, MAX) {
      int X = i;
      int keta = 0;
      char c[10] = {};
      while (X) {
        *(c + keta) = '0' + X % 10;
        X /= 10;
        keta++;
      }
      for (int j = keta - 1; j >= 0; j--)
        *di++ = (*(c + j));
      K[i + 1] = K[i] + keta;
    }
  }
} g;

int N;
const int VN = 200000;
int to[VN * 2 + 2], ne[VN * 2 + 2], he[VN + 1];
int n[VN + 1];
void dfs(int v) {
  n[v]++;
  int mae = 0;
  for (int i = he[v]; i; i = ne[i]) {
    int t = to[i];
    if (n[t]) {
      if (mae)
        ne[mae] = ne[i];
      else
        he[v] = ne[i];
    } else {
      dfs(t);
      n[v] += n[t];
    }
    mae = i;
  }
}
void dfs2(int v) {
  for (int i = he[v]; i; i = ne[i]) {
    int t = to[i];
    n[t] = ll(n[v]) * n[t] % mod * f.inv[N - n[t]] % mod;
    dfs2(t);
  }
}

const int CM = 2800000;
char cn[CM], *ci = cn;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
  ll tmp = *(ll *)ci;
  int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
  tmp = tmp << dig & ma1;
  tmp = tmp * 10 + (tmp >> 8) & ma2;
  tmp = tmp * 100 + (tmp >> 16) & ma3;
  tmp = tmp * 10000 + (tmp >> 32) & ma4;
  ci += (64 - dig >> 3) + 1;
  return tmp;
}
char *di = cn;
inline void putint(int X) {
  int ue = X / 100000;
  if (ue) {
    for (int i = g.K[ue]; i < g.K[ue + 1]; i++)
      *di++ = g.C[i];
    int sita = X - ue * 100000ll;
    rep(i, 5 - g.K[sita + 1] + g.K[sita]) *di++ = '0';
    for (int i = g.K[sita]; i < g.K[sita + 1]; i++)
      *di++ = g.C[i];
  } else
    for (int i = g.K[X]; i < g.K[X + 1]; i++)
      *di++ = g.C[i];
  *di++ = '\n';
}

int main() {
  // cin.tie(0);
  // ios::sync_with_stdio(false);

  fread(cn, 1, CM, stdin);
  N = getint();
  int k = 2;
  rep1(i, N - 1) {
    int a = getint(), b = getint();

    to[k] = b;
    ne[k] = he[a];
    he[a] = k++;
    to[k] = a;
    ne[k] = he[b];
    he[b] = k++;
  }
  dfs(1);
  ll tmp = 1;
  rep1(i, N) tmp = (tmp * n[i]) % mod;
  n[1] = modpow(tmp, mod - 2) * f.fac[N] % mod;
  for (int i = he[1]; i; i = ne[i]) {
    int t = to[i];
    n[t] = ll(n[1]) * n[t] % mod * f.inv[N - n[t]] % mod;
    dfs2(t);
  }
  rep1(i, N) putint(n[i]);
  fwrite(cn, 1, di - cn, stdout);

  Would you please return 0;
}