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

#include <algorithm>
#include <complex>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <unordered_map>
#include <vector>
typedef long long int LL;
typedef unsigned long long int ULL;
using namespace std;

// 插入此處
namespace Number {
typedef long long int LL;
// 求取 a, b 的最大公因數
LL gcd(LL a, LL b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}
// 求取 a, b 的最大公因數 gcd
// 以及 x, y 使得 a*x + b*y = gcd
LL gcd_extend(LL a, LL b, LL *x, LL *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  LL x1, y1; // To store results of recursive call
  LL gcd = gcd_extend(b % a, a, &x1, &y1);

  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}
// 求 a_inv 使得 (a * a_inv) % m = gcd(a, m)
// 若 a, m 互質，即求 a 在 mod m 群的反元素 a_inv
LL mod_inv(LL a, LL m) {
  LL x, y;
  LL g = gcd_extend(a, m, &x, &y);
  // 處理 x 爲負的情形
  LL a_inv = (x % m + m) % m;
  return a_inv;
}
// 求 x^n % m
LL mod_pow(LL x, LL n, LL m) {
  if (n == 0) {
    return 1;
  } else if (n & 1) {
    LL half = mod_pow(x, n / 2, m);
    return (half * half % m) * x % m;
  } else {
    LL half = mod_pow(x, n / 2, m);
    return half * half % m;
  }
}
// 連乘
LL mul(LL from, LL to, LL m) {
  LL ret = 1;
  for (LL i = from; i <= to; i++) {
    ret *= i;
    ret %= m;
  }
  return ret;
}
// 求 (C n 取 k) % m
LL mod_comb(LL n, LL k, LL m) {
  LL mi = k < n - k ? k : n - k;
  LL up = mul(n - mi + 1, n, m);
  LL down = mul(1, mi, m);
  return (up * mod_inv(down, m)) % m;
}
} // namespace Number

int n;

const LL M = (LL)1e9 + 7;
vector<int> edges[200005];
// 上一個是誰, 節點 => 幾種變化
LL children[200005];
int parent[200005];

LL pers[200005];
LL persi[200005];

int get_children(int x) {
  // 包含自己
  int ret = 1;
  for (auto nxt : edges[x]) {
    if (nxt != parent[x]) {
      parent[nxt] = x;
      ret += get_children(nxt);
    }
  }
  children[x] = ret;
  return ret;
}

LL br_ans[200005];

LL as_root(int x) {
  LL total = children[x];
  LL ex = total - 1;
  LL ret = 1;
  ret *= pers[ex];
  ret %= M;
  for (auto nxt : edges[x]) {
    if (nxt == parent[x]) {
      continue;
    }
    int cn = children[nxt];
    ret *= persi[cn];
    ret %= M;
    ret *= as_root(nxt);
    ret %= M;
  }
  br_ans[x] = ret;
  return ret;
}

LL ans[200005];

void get_ans(int x, LL pans) {
  ans[x] = br_ans[x];
  LL ex = children[x] - 1;
  ans[x] *= persi[ex];
  ans[x] %= M;
  ans[x] *= pers[n - 1];
  ans[x] %= M;

  ans[x] *= persi[n - children[x]];
  ans[x] %= M;
  ans[x] *= pans;
  ans[x] %= M;
  for (auto nxt : edges[x]) {
    if (nxt == parent[x]) {
      continue;
    }
    LL _pans = ans[x];
    _pans *= persi[n - 1];
    _pans %= M;
    _pans *= pers[n - 1 - children[nxt]];
    _pans %= M;

    _pans *= pers[children[nxt]];
    _pans %= M;
    _pans *= Number::mod_inv(br_ans[nxt], M);
    _pans %= M;
    get_ans(nxt, _pans);
  }
}

int main() {
  scanf("%d", &n);
  pers[0] = 1;
  persi[0] = Number::mod_inv(pers[0], M);
  for (int i = 1; i <= n; i++) {
    pers[i] = pers[i - 1] * i % M;
    persi[i] = Number::mod_inv(pers[i], M);
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  get_children(1);
  as_root(1);
  get_ans(1, 1);
  for (int i = 1; i <= n; i++) {
    printf("%lld\\n", ans[i]);
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <complex>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <unordered_map>
#include <vector>
typedef long long int LL;
typedef unsigned long long int ULL;
using namespace std;

// 插入此處
namespace Number {
typedef long long int LL;
// 求取 a, b 的最大公因數
LL gcd(LL a, LL b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}
// 求取 a, b 的最大公因數 gcd
// 以及 x, y 使得 a*x + b*y = gcd
LL gcd_extend(LL a, LL b, LL *x, LL *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }

  LL x1, y1; // To store results of recursive call
  LL gcd = gcd_extend(b % a, a, &x1, &y1);

  *x = y1 - (b / a) * x1;
  *y = x1;

  return gcd;
}
// 求 a_inv 使得 (a * a_inv) % m = gcd(a, m)
// 若 a, m 互質，即求 a 在 mod m 群的反元素 a_inv
LL mod_inv(LL a, LL m) {
  LL x, y;
  LL g = gcd_extend(a, m, &x, &y);
  // 處理 x 爲負的情形
  LL a_inv = (x % m + m) % m;
  return a_inv;
}
// 求 x^n % m
LL mod_pow(LL x, LL n, LL m) {
  if (n == 0) {
    return 1;
  } else if (n & 1) {
    LL half = mod_pow(x, n / 2, m);
    return (half * half % m) * x % m;
  } else {
    LL half = mod_pow(x, n / 2, m);
    return half * half % m;
  }
}
// 連乘
LL mul(LL from, LL to, LL m) {
  LL ret = 1;
  for (LL i = from; i <= to; i++) {
    ret *= i;
    ret %= m;
  }
  return ret;
}
// 求 (C n 取 k) % m
LL mod_comb(LL n, LL k, LL m) {
  LL mi = k < n - k ? k : n - k;
  LL up = mul(n - mi + 1, n, m);
  LL down = mul(1, mi, m);
  return (up * mod_inv(down, m)) % m;
}
} // namespace Number

int n;

const LL M = (LL)1e9 + 7;
vector<int> edges[200005];
// 上一個是誰, 節點 => 幾種變化
LL children[200005];
int parent[200005];

LL pers[200005];
LL persi[200005];

int get_children(int x) {
  // 包含自己
  int ret = 1;
  for (auto nxt : edges[x]) {
    if (nxt != parent[x]) {
      parent[nxt] = x;
      ret += get_children(nxt);
    }
  }
  children[x] = ret;
  return ret;
}

LL br_ans[200005];

LL as_root(int x) {
  LL total = children[x];
  LL ex = total - 1;
  LL ret = 1;
  ret *= pers[ex];
  ret %= M;
  for (auto nxt : edges[x]) {
    if (nxt == parent[x]) {
      continue;
    }
    int cn = children[nxt];
    ret *= persi[cn];
    ret %= M;
    ret *= as_root(nxt);
    ret %= M;
  }
  br_ans[x] = ret;
  return ret;
}

LL ans[200005];

LL get_ans(int x, LL pans) {
  ans[x] = br_ans[x];
  LL ex = children[x] - 1;
  ans[x] *= persi[ex];
  ans[x] %= M;
  ans[x] *= pers[n - 1];
  ans[x] %= M;

  ans[x] *= persi[n - children[x]];
  ans[x] %= M;
  ans[x] *= pans;
  ans[x] %= M;
  for (auto nxt : edges[x]) {
    if (nxt == parent[x]) {
      continue;
    }
    LL _pans = ans[x];
    _pans *= persi[n - 1];
    _pans %= M;
    _pans *= pers[n - 1 - children[nxt]];
    _pans %= M;

    _pans *= pers[children[nxt]];
    _pans %= M;
    _pans *= Number::mod_inv(br_ans[nxt], M);
    _pans %= M;
    get_ans(nxt, _pans);
  }
}

int main() {
  scanf("%d", &n);
  pers[0] = 1;
  persi[0] = Number::mod_inv(pers[0], M);
  for (int i = 1; i <= n; i++) {
    pers[i] = pers[i - 1] * i % M;
    persi[i] = Number::mod_inv(pers[i], M);
  }
  int en = n + 1;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    edges[a].push_back(b);
    edges[b].push_back(a);
  }
  get_children(1);
  as_root(1);
  get_ans(1, 1);
  for (int i = 1; i <= n; i++) {
    printf("%lld\\n", ans[i]);
  }
}