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

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 200005
#define inf 1e9
#define eps 1e-10
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

const ll mod = 1000000007;
vector<int> mp[maxn];
ll fac[maxn], inv[maxn], siz[maxn], n, ans[maxn];

inline ll pw(ll a, ll b) {
  ll ans = 1, base = a;
  while (b) {
    if (b & 1)
      ans = (ans * base) % mod;
    base = (base * base) % mod;
    b >>= 1;
  }
  return ans;
}

inline void dfs1(int u, int fa) {
  siz[u] = 1;
  for (int i = 0; i < mp[u].size(); i++) {
    int v = mp[u][i];
    if (v == fa)
      continue;
    dfs1(v, u);
    siz[u] += siz[v];
  }
}

inline void dfs2(int u, int fa) {
  for (int i = 0; i < mp[u].size(); i++) {
    int v = mp[u][i];
    if (v == fa)
      continue;
    ans[v] = (ans[u] * pw(n - siz[v], mod - 2) % mod) * siz[v] % mod;
    dfs2(v, u);
  }
}

int main() {
  n = read();
  fac[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= maxn - 5; i++)
    fac[i] = (fac[i - 1] * i) % mod;
  for (int i = 1; i <= maxn - 5; i++)
    inv[i] = pw(fac[i], mod - 2);
  for (int i = 1; i < n; i++) {
    int u = read(), v = read();
    mp[u].push_back(v), mp[v].push_back(u);
  }
  dfs1(1, 0);
  ll tmp = fac[n];
  for (int i = 1; i <= n; i++)
    tmp = (tmp * pw(siz[i], mod - 2)) % mod;
  ans[1] = tmp;
  dfs2(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%lld\\n", ans[i]);
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 100005
#define inf 1e9
#define eps 1e-10
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

const ll mod = 1000000007;
vector<int> mp[maxn];
ll fac[maxn], inv[maxn], siz[maxn], n, ans[maxn];

inline ll pw(ll a, ll b) {
  ll ans = 1, base = a;
  while (b) {
    if (b & 1)
      ans = (ans * base) % mod;
    base = (base * base) % mod;
    b >>= 1;
  }
  return ans;
}

inline void dfs1(int u, int fa) {
  siz[u] = 1;
  for (int i = 0; i < mp[u].size(); i++) {
    int v = mp[u][i];
    if (v == fa)
      continue;
    dfs1(v, u);
    siz[u] += siz[v];
  }
}

inline void dfs2(int u, int fa) {
  for (int i = 0; i < mp[u].size(); i++) {
    int v = mp[u][i];
    if (v == fa)
      continue;
    ans[v] = (ans[u] * pw(n - siz[v], mod - 2) % mod) * siz[v] % mod;
    dfs2(v, u);
  }
}

int main() {
  n = read();
  fac[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= maxn - 5; i++)
    fac[i] = (fac[i - 1] * i) % mod;
  for (int i = 1; i <= maxn - 5; i++)
    inv[i] = pw(fac[i], mod - 2);
  for (int i = 1; i < n; i++) {
    int u = read(), v = read();
    mp[u].push_back(v), mp[v].push_back(u);
  }
  dfs1(1, 0);
  ll tmp = fac[n];
  for (int i = 1; i <= n; i++)
    tmp = (tmp * pw(siz[i], mod - 2)) % mod;
  ans[1] = tmp;
  dfs2(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%lld\\n", ans[i]);
  return 0;
}