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

// agrawal117
// chahatagrawal117
#include <bits/stdc++.h>
#define endl '\\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 300005
vector<ll> v[MAX];
ll in[MAX];
ll out[MAX];
ll dp[MAX];
ll n;
void dfs(int node, int par) {
  dp[node] = 1;
  in[node] = 1LL;
  for (auto i : v[node]) {
    if (i ^ par) {
      dfs(i, node);
      dp[node] += dp[i];
      dp[node] %= mod;
      in[node] = (in[node] * in[i]) % mod;
    }
  }
  dp[node] %= mod;
  in[node] = (in[node] * dp[node]) % mod;
}
ll power(ll a, ll b) {
  a %= mod;
  ll res = 1LL;
  while (b > 0) {
    if (b & 1LL)
      res = (res * a) % mod;
    b >>= 1;
    a = (a * a) % mod;
  }
  return res % mod;
}
ll inv(ll a) { return power(a, mod - 2) % mod; }
void bfs(int node, int par) {
  ll mult = 1LL;
  for (auto i : v[node]) {
    if (i ^ par)
      mult = (mult * in[i]) % mod;
  }
  for (auto i : v[node]) {
    if (i ^ par) {
      ll sub = n - dp[i];
      out[i] = (((sub * out[node]) % mod) * mult) % mod;
      out[i] = (out[i] * inv(in[i])) % mod;
      bfs(i, node);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(1, 1);
  out[1] = 1LL;
  bfs(1, 1);
  ll ans = 0;
  ll fact = 1;
  for (int i = 1; i <= n; i++)
    fact = (fact * i) % mod;
  for (int i = 1; i <= n; i++) {
    ll val = in[i];
    val = (val * inv(dp[i])) % mod;
    val = (val * n) % mod;
    val = (val * out[i]) % mod;
    ll f = inv(val);
    f = (fact * f) % mod;
    cout << f << endl;
    ans = (ans + f) % mod;
  }
  // cout<<an`s<<endl;
}


Error: 0

Fixed submission:

// agrawal117
// chahatagrawal117
#include <bits/stdc++.h>
#define endl '\\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 100005
vector<ll> v[MAX];
ll in[MAX];
ll out[MAX];
ll dp[MAX];
ll n;
void dfs(int node, int par) {
  dp[node] = 1;
  in[node] = 1LL;
  for (auto i : v[node]) {
    if (i ^ par) {
      dfs(i, node);
      dp[node] += dp[i];
      dp[node] %= mod;
      in[node] = (in[node] * in[i]) % mod;
    }
  }
  dp[node] %= mod;
  in[node] = (in[node] * dp[node]) % mod;
}
ll power(ll a, ll b) {
  a %= mod;
  ll res = 1LL;
  while (b > 0) {
    if (b & 1LL)
      res = (res * a) % mod;
    b >>= 1;
    a = (a * a) % mod;
  }
  return res % mod;
}
ll inv(ll a) { return power(a, mod - 2) % mod; }
void bfs(int node, int par) {
  ll mult = 1LL;
  for (auto i : v[node]) {
    if (i ^ par)
      mult = (mult * in[i]) % mod;
  }
  for (auto i : v[node]) {
    if (i ^ par) {
      ll sub = n - dp[i];
      out[i] = (((sub * out[node]) % mod) * mult) % mod;
      out[i] = (out[i] * inv(in[i])) % mod;
      bfs(i, node);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n - 1; i++) {
    ll a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(1, 1);
  out[1] = 1LL;
  bfs(1, 1);
  ll ans = 0;
  ll fact = 1;
  for (int i = 1; i <= n; i++)
    fact = (fact * i) % mod;
  for (int i = 1; i <= n; i++) {
    ll val = in[i];
    val = (val * inv(dp[i])) % mod;
    val = (val * n) % mod;
    val = (val * out[i]) % mod;
    ll f = inv(val);
    f = (fact * f) % mod;
    cout << f << endl;
    ans = (ans + f) % mod;
  }
  // cout<<an`s<<endl;
}