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

#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 5;
const long long mod = 1e9 + 7;
void in(long long &x) {
  long long y = 1;
  char c = getchar();
  x = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      y = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  x *= y;
}
void o(long long x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    o(x / 10);
  putchar(x % 10 + '0');
}
long long read() {
  long long res = 0;
  long long y = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      y = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    res = (res << 1) + (res << 3) + c - '0';
    c = getchar();
  }
  return res * y;
}
long long ppo(long long x, long long p) {
  long long t = x, res = 1;
  while (p) {
    if (p & 1)
      res = (res * t) % mod;
    t = (t * t) % mod;
    p >>= 1;
  }
  return res;
}
struct edge {
  long long to, next, value;
} e[maxn * 2];
long long first[maxn * 2], cntedge = 0;
void addedge(long long fr, long long to) {
  e[++cntedge] = {to, first[fr]};
  first[fr] = cntedge;
  swap(fr, to);
  e[++cntedge] = {
      to,
      first[fr],
  };
  first[fr] = cntedge;
}
long long n;
long long sons[maxn];
long long f[maxn];
void dfs(long long u) {
  sons[u] = 1;
  f[u] = 1;
  for (long long i = first[u]; i; i = e[i].next) {
    long long v = e[i].to;
    if (!sons[v]) {
      dfs(v);
      sons[u] += sons[v];
      f[u] = f[u] * f[v] % mod;
    }
  }
  f[u] = f[u] * sons[u] % mod;
}
void cal(long long u, long long fa) {
  for (long long i = first[u]; i; i = e[i].next) {
    long long v = e[i].to;
    if (v != fa) {
      f[v] = (f[u] * (n - sons[v]) % mod * ppo(sons[v], mod - 2)) % mod;
      cal(v, u);
    }
  }
}
signed main() {
  in(n);
  long long jic = 1;
  for (long long i = 1; i <= n; i++)
    jic = (jic * i) % mod;
  for (long long i = 1; i <= n - 1; i++) {
    addedge(read(), read());
  }
  dfs(1);
  cal(1, 0);
  for (long long i = 1; i <= n; i++) {
    // cout<<ppo(2,mod-2)<<endl;
    o((jic * ppo(f[i], mod - 2)) % mod);
    putchar('\\n');
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 5;
const long long mod = 1e9 + 7;
void in(long long &x) {
  long long y = 1;
  char c = getchar();
  x = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      y = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  x *= y;
}
void o(long long x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    o(x / 10);
  putchar(x % 10 + '0');
}
long long read() {
  long long res = 0;
  long long y = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      y = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    res = (res << 1) + (res << 3) + c - '0';
    c = getchar();
  }
  return res * y;
}
long long ppo(long long x, long long p) {
  long long t = x, res = 1;
  while (p) {
    if (p & 1)
      res = (res * t) % mod;
    t = (t * t) % mod;
    p >>= 1;
  }
  return res;
}
struct edge {
  long long to, next, value;
} e[maxn];
long long first[maxn], cntedge = 0;
void addedge(long long fr, long long to) {
  e[++cntedge] = {to, first[fr]};
  first[fr] = cntedge;
  swap(fr, to);
  e[++cntedge] = {
      to,
      first[fr],
  };
  first[fr] = cntedge;
}
long long n;
long long sons[maxn];
long long f[maxn];
void dfs(long long u) {
  sons[u] = 1;
  f[u] = 1;
  for (long long i = first[u]; i; i = e[i].next) {
    long long v = e[i].to;
    if (!sons[v]) {
      dfs(v);
      sons[u] += sons[v];
      f[u] = f[u] * f[v] % mod;
    }
  }
  f[u] = f[u] * sons[u] % mod;
}
void cal(long long u, long long fa) {
  for (long long i = first[u]; i; i = e[i].next) {
    long long v = e[i].to;
    if (v != fa) {
      f[v] = (f[u] * (n - sons[v]) * ppo(sons[v], mod - 2)) % mod;
      cal(v, u);
    }
  }
}
signed main() {
  in(n);
  long long jic = 1;
  for (long long i = 1; i <= n; i++)
    jic = (jic * i) % mod;
  for (long long i = 1; i <= n - 1; i++) {
    addedge(read(), read());
  }
  dfs(1);
  cal(1, 0);
  for (long long i = 1; i <= n; i++) {
    // cout<<ppo(2,mod-2)<<endl;
    o((jic * ppo(f[i], mod - 2)) % mod);
    putchar('\\n');
  }
  return 0;
}