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

#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \\
  int(X);                                                                      \\
  scanf("%d", &X)
#define DRII(X, Y)                                                             \\
  int X, Y;                                                                    \\
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \\
  int X, Y, Z;                                                                 \\
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \\
  int ___T, case_n = 1;                                                        \\
  scanf("%d ", &___T);                                                         \\
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int, int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int, int>>
#define PLL pair<long long, long long>
#define VPLL vector<pair<long long, long long>>
#define F first
#define S second
typedef long long LL;
using namespace std;
const int MOD = 1000000007;
const int SIZE = 1 << 20;
VI e[SIZE];
LL fac[SIZE], inv[SIZE];
LL mypow(LL x, LL y) {
  LL res = 1;
  while (y) {
    if (y & 1)
      res = res * x % MOD;
    y >>= 1;
    x = x * x % MOD;
  }
  return res;
}
LL C(int x, int y) {
  if (y < 0 || y > x)
    return 0;
  return fac[x] * inv[y] % MOD * inv[x - y] % MOD;
}
void build() {
  fac[0] = 1;
  REPP(i, 1, SIZE) fac[i] = fac[i - 1] * i % MOD;
  inv[SIZE - 1] = mypow(fac[SIZE - 1], MOD - 2);
  for (int i = SIZE - 2; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % MOD;
}
LL BIT[SIZE];
void ins(int x, LL v) {
  for (; x; x -= x & -x)
    BIT[x] = BIT[x] * v % MOD;
}
LL qq(int x) {
  LL res = 1;
  for (; x < SIZE; x += x & -x)
    res = res * BIT[x] % MOD;
  return res;
}
int ll[SIZE], rr[SIZE], id, N;
LL INV(int x) { return mypow(x, MOD - 2); }
void dfs0(int x, int lt) {
  ll[x] = rr[x] = ++id;
  REP(i, SZ(e[x])) {
    int y = e[x][i];
    if (y == lt)
      continue;
    dfs0(y, x);
    rr[x] = rr[y];
  }
}
int dfs(int x, int lt) {
  int num = 1;
  LL me_inv = 1;
  VI sons;
  REP(i, SZ(e[x])) {
    int y = e[x][i];
    sons.PB(0);
    if (y == lt)
      continue;
    int res = dfs(y, x);
    sons[i] = res;
    num += res;
    me_inv = me_inv * inv[res] % MOD;
  }
  LL v = me_inv * inv[N - num] % MOD * fac[N - 1] % MOD;
  ins(ll[x], v);
  ins(ll[x] - 1, INV(v));
  v = me_inv * fac[num - 1] % MOD;
  ins(N, v);
  ins(rr[x], INV(v));
  ins(ll[x] - 1, v);
  REP(i, SZ(e[x])) {
    int y = e[x][i];
    if (y == lt)
      continue;
    v = me_inv * fac[sons[i]] % MOD * inv[N - num] % MOD *
        fac[N - sons[i] - 1] % MOD;
    ins(rr[y], v);
    ins(ll[y] - 1, INV(v));
  }
  return num;
}
int main() {
  REP(i, SIZE) BIT[i] = 1;
  build();
  RI(N);
  REPP(i, 1, N) {
    DRII(x, y);
    e[x].PB(y);
    e[y].PB(x);
  }
  dfs0(1, 1);
  dfs(1, 1);
  for (int i = 1; i <= N; i++) {
    cout << qq(ll[i]) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \\
  int(X);                                                                      \\
  scanf("%d", &X)
#define DRII(X, Y)                                                             \\
  int X, Y;                                                                    \\
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \\
  int X, Y, Z;                                                                 \\
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \\
  int ___T, case_n = 1;                                                        \\
  scanf("%d ", &___T);                                                         \\
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int, int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int, int>>
#define PLL pair<long long, long long>
#define VPLL vector<pair<long long, long long>>
#define F first
#define S second
typedef long long LL;
using namespace std;
const int MOD = 1000000007;
const int SIZE = 1e5 + 10;
VI e[SIZE];
void ADD(LL &x, LL v) { x = (x + v) % MOD; }
LL fac[SIZE], inv[SIZE];
LL mypow(LL x, LL y) {
  LL res = 1;
  while (y) {
    if (y & 1)
      res = res * x % MOD;
    y >>= 1;
    x = x * x % MOD;
  }
  return res;
}
LL C(int x, int y) {
  if (y < 0 || y > x)
    return 0;
  return fac[x] * inv[y] % MOD * inv[x - y] % MOD;
}
void build() {
  assert(MOD >= SIZE);
  fac[0] = 1;
  REPP(i, 1, SIZE) fac[i] = fac[i - 1] * i % MOD;
  inv[SIZE - 1] = mypow(fac[SIZE - 1], MOD - 2);
  for (int i = SIZE - 2; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % MOD;
}
LL BIT[SIZE];
void ins(int x, LL v) {
  for (; x; x -= x & -x)
    BIT[x] = BIT[x] * v % MOD;
}
LL qq(int x) {
  LL res = 1;
  for (; x < SIZE; x += x & -x)
    res = res * BIT[x] % MOD;
  return res;
}
int ll[SIZE], rr[SIZE], id, N;
LL INV(int x) { return mypow(x, MOD - 2); }
void dfs0(int x, int lt) {
  ll[x] = rr[x] = ++id;
  REP(i, SZ(e[x])) {
    int y = e[x][i];
    if (y == lt)
      continue;
    dfs0(y, x);
    rr[x] = rr[y];
  }
}
int dfs(int x, int lt) {
  int num = 1;
  LL me_inv = 1;
  VI sons;
  REP(i, SZ(e[x])) {
    int y = e[x][i];
    sons.PB(0);
    if (y == lt)
      continue;
    int res = dfs(y, x);
    sons[i] = res;
    num += res;
    me_inv = me_inv * inv[res] % MOD;
  }
  LL v = me_inv * inv[N - num] % MOD * fac[N - 1] % MOD;
  ins(ll[x], v);
  ins(ll[x] - 1, INV(v));
  v = me_inv * fac[num - 1] % MOD;
  ins(N, v);
  ins(rr[x], INV(v));
  ins(ll[x] - 1, v);
  REP(i, SZ(e[x])) {
    int y = e[x][i];
    if (y == lt)
      continue;
    v = me_inv * fac[sons[i]] % MOD * inv[N - num] % MOD *
        fac[N - sons[i] - 1] % MOD;
    ins(rr[y], v);
    ins(ll[y] - 1, INV(v));
  }
  return num;
}
int main() {
  REP(i, SIZE) BIT[i] = 1;
  build();
  RI(N);
  REPP(i, 1, N) {
    DRII(x, y);
    e[x].PB(y);
    e[y].PB(x);
  }
  dfs0(1, 1);
  dfs(1, 1);
  REPP(i, 1, N + 1) { printf("%lld\\n", qq(i)); }
  return 0;
}