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

#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = j; i <= k; i++)
#define rep2(i, j, k) for (int i = j; i >= k; i--)
#define mo 1000000007
using namespace std;
template <typename T> void read(T &num) {
  char c = getchar();
  T f = 1;
  num = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    num = (num << 3) + (num << 1) + (c ^ 48);
    c = getchar();
  }
  num *= f;
}
template <typename T> void qwq(T x) {
  if (x > 9)
    qwq(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> void write(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  qwq(x);
  putchar('\\n');
}
int n;
int powe[200010];
int inv[200010];
inline int poww(int n, int m) {
  int temp = n;
  int tmp = m;
  int ret = 1;
  while (tmp) {
    if (tmp & 1)
      ret = (1ll * ret * temp) % mo;
    temp = (1ll * temp * temp) % mo;
    tmp >>= 1;
  }
  return ret;
}
struct wzy {
  int nxt, vertice;
} edge[400010];
int head[200010];
int len = 0;
inline void add_edge(int x, int y) {
  edge[++len].nxt = head[x];
  edge[len].vertice = y;
  head[x] = len;
  return;
}

int dp[200010];
int siz[200010];
int ans[200010];
inline void dfs(int u, int fa) {
  dp[u] = 1;
  siz[u] = 1;
  for (int i = head[u]; i; i = edge[i].nxt) {
    int nop = edge[i].vertice;
    if (nop == fa)
      continue;
    dfs(nop, u);
    siz[u] += siz[nop];
    dp[u] = (1ll * dp[u] * dp[nop]) % mo;
    dp[u] = (1ll * dp[u] * inv[siz[nop]]) % mo;
  }
  dp[u] = (1ll * dp[u] * powe[siz[u] - 1]) % mo;
  return;
}
inline void sol(int u, int fa) {
  ans[u] = 1;
  for (int i = head[u]; i; i = edge[i].nxt) {
    int nop = edge[i].vertice;
    if (nop != fa) {
      ans[u] = (1ll * ans[u] * dp[nop]) % mo;
      ans[u] = (1ll * ans[u] * inv[siz[nop]]) % mo;
    } else {
      int temp = (1ll * ans[fa] * inv[n - 1]) % mo;
      temp = (1ll * temp * powe[n - 1 - siz[u]]) % mo;
      temp = (1ll * temp * powe[siz[u]]) % mo;
      temp = (1ll * temp * poww(dp[u], mo - 2)) % mo;
      ans[u] = (1ll * ans[u] * temp) % mo;
      ans[u] = (1ll * ans[u] * inv[n - siz[u]]) % mo;
    }
  }
  ans[u] = (1ll * ans[u] * powe[n - 1]) % mo;

  for (int i = head[u]; i; i = edge[i].nxt) {
    if (edge[i].vertice != fa)
      sol(edge[i].vertice, u);
  }
  return;
}

int main() {
  read(n);
  powe[0] = powe[1] = 1;
  inv[1] = 1;
  rep(i, 2, n) powe[i] = (1ll * powe[i - 1] * i) % mo;
  inv[n] = poww(powe[n], mo - 2);
  rep2(i, n - 1, 2) inv[i] = (1ll * inv[i + 1] * (i + 1)) % mo;

  rep(i, 1, n - 1) {
    int a, b;
    read(a);
    read(b);
    add_edge(a, b);
    add_edge(b, a);
  }

  dfs(1, 0);
  ans[1] = dp[1];
  for (int i = head[1]; i; i = edge[i].nxt) {
    int nop = edge[i].vertice;
    sol(nop, 1);
  }
  rep(i, 1, n) write(ans[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = j; i <= k; i++)
#define rep2(i, j, k) for (int i = j; i >= k; i--)
#define mo 1000000007
using namespace std;
template <typename T> void read(T &num) {
  char c = getchar();
  T f = 1;
  num = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    num = (num << 3) + (num << 1) + (c ^ 48);
    c = getchar();
  }
  num *= f;
}
template <typename T> void qwq(T x) {
  if (x > 9)
    qwq(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> void write(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  qwq(x);
  putchar('\\n');
}
int n;
int powe[100010];
int inv[100010];
inline int poww(int n, int m) {
  int temp = n;
  int tmp = m;
  int ret = 1;
  while (tmp) {
    if (tmp & 1)
      ret = (1ll * ret * temp) % mo;
    temp = (1ll * temp * temp) % mo;
    tmp >>= 1;
  }
  return ret;
}
struct wzy {
  int nxt, vertice;
} edge[200010];
int head[100010];
int len = 0;
inline void add_edge(int x, int y) {
  edge[++len].nxt = head[x];
  edge[len].vertice = y;
  head[x] = len;
  return;
}

int dp[100010];
int siz[100010];
int ans[100010];
inline void dfs(int u, int fa) {
  dp[u] = 1;
  siz[u] = 1;
  for (int i = head[u]; i; i = edge[i].nxt) {
    int nop = edge[i].vertice;
    if (nop == fa)
      continue;
    dfs(nop, u);
    siz[u] += siz[nop];
    dp[u] = (1ll * dp[u] * dp[nop]) % mo;
    dp[u] = (1ll * dp[u] * inv[siz[nop]]) % mo;
  }
  dp[u] = (1ll * dp[u] * powe[siz[u] - 1]) % mo;
  return;
}
inline void sol(int u, int fa) {
  ans[u] = 1;
  for (int i = head[u]; i; i = edge[i].nxt) {
    int nop = edge[i].vertice;
    if (nop != fa) {
      ans[u] = (1ll * ans[u] * dp[nop]) % mo;
      ans[u] = (1ll * ans[u] * inv[siz[nop]]) % mo;
    } else {
      int temp = (1ll * ans[fa] * inv[n - 1]) % mo;
      temp = (1ll * temp * powe[n - 1 - siz[u]]) % mo;
      temp = (1ll * temp * powe[siz[u]]) % mo;
      temp = (1ll * temp * poww(dp[u], mo - 2)) % mo;
      ans[u] = (1ll * ans[u] * temp) % mo;
      ans[u] = (1ll * ans[u] * inv[n - siz[u]]) % mo;
    }
  }
  ans[u] = (1ll * ans[u] * powe[n - 1]) % mo;

  for (int i = head[u]; i; i = edge[i].nxt) {
    if (edge[i].vertice != fa)
      sol(edge[i].vertice, u);
  }
  return;
}

int main() {
  read(n);
  powe[0] = powe[1] = 1;
  inv[1] = 1;
  rep(i, 2, n) powe[i] = (1ll * powe[i - 1] * i) % mo;
  inv[n] = poww(powe[n], mo - 2);
  rep2(i, n - 1, 2) inv[i] = (1ll * inv[i + 1] * (i + 1)) % mo;

  rep(i, 1, n - 1) {
    int a, b;
    read(a);
    read(b);
    add_edge(a, b);
    add_edge(b, a);
  }

  dfs(1, 0);
  ans[1] = dp[1];
  for (int i = head[1]; i; i = edge[i].nxt) {
    int nop = edge[i].vertice;
    sol(nop, 1);
  }
  rep(i, 1, n) write(ans[i]);
  return 0;
}