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

// #include "pch.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define ll long long
#define fri(n) for (i = 0; i < (n); i++)
#define frj(n) for (j = 0; j < (n); i++)
#define min(p, q) ((p) < (q) ? (p) : (q))
#define max(p, q) ((p) > (q) ? (p) : (q))
#define INF 1000000000000000000 // 10^18
#define INFINT 2000000001       // 2*10^9+1
#define MOD 1000000007
#define MODANOTHER 998244353
#define PI acos(-1)

#define vi vector<int>
#define vvi vector<vector<int>>
#define vvvi vector<vector<vector<int>>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vvvd vector<vector<vector<double>>>

#define int long long

using namespace std;

const int MAX = 510000;

ll mod_inverse(int a, int M) {
  int x = M - 2;
  ll temp = a;
  ll ret = 1;
  // a^(MOD-1)==1(mod MOD)を利用
  // a^-1==a^(MOD-2) (mod MOD)を繰り返し二乗法で求める

  while (x != 0) {
    if (x % 2 == 1) {
      ret = (ret * temp) % M;
    }
    temp = (temp * temp) % M;
    x = x / 2;
  }
  return ret;
}

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

signed compare_ll(const void *a, const void *b) {
  ll *A = (ll *)a;
  ll *B = (ll *)b;
  if (*A < *B)
    return 1;
  if (*A > *B)
    return -1;
  return 0;
}

int makeGraph(int x, vvi &gtemp, vvi &graph, vi &come) {
  int i;

  come[x] = 1;

  for (i = 0; i < gtemp[x].size(); i++) {
    if (come[gtemp[x][i]] == 1)
      continue;
    graph[x].push_back(gtemp[x][i]);
    makeGraph(gtemp[x][i], gtemp, graph, come);
  }

  return 0;
}

struct pret_t {
  int vertex;
  int patan;
};

pret_t search(int x, vvi &graph, vector<vector<pret_t>> &vvpret) {
  int i;
  int temp;
  int sum;
  pret_t pret;
  pret_t ptemp;
  vector<pret_t> vptemp;

  for (i = 0; i < graph[x].size(); i++) {
    ptemp = search(graph[x][i], graph, vvpret);
    vptemp.push_back(ptemp);
  }
  vvpret[x] = vptemp;

  pret.vertex = 1;
  pret.patan = 1;

  for (i = 0; i < vptemp.size(); i++) {
    pret.vertex += vptemp[i].vertex;
  }

  temp = pret.vertex - 1;

  for (i = 0; i < vptemp.size(); i++) {
    pret.patan *= COM(temp, vptemp[i].vertex);
    pret.patan %= MOD;
    pret.patan *= vptemp[i].patan;
    pret.patan %= MOD;
    temp -= vptemp[i].vertex;
  }

  return pret;
}

int inverse(int x, pret_t prev, vi &vans, vvi &graph,
            vector<vector<pret_t>> &vvpret) {
  int i;
  int temp;
  pret_t pret;
  pret_t ptemp;
  vector<pret_t> vptemp;

  vptemp = vvpret[x];
  if (x != 1)
    vptemp.push_back(prev);

  pret.vertex = 1;
  pret.patan = 1;

  for (i = 0; i < vptemp.size(); i++) {
    pret.vertex += vptemp[i].vertex;
  }

  temp = pret.vertex - 1;

  for (i = 0; i < vptemp.size(); i++) {
    pret.patan *= COM(temp, vptemp[i].vertex);
    pret.patan %= MOD;
    pret.patan *= vptemp[i].patan;
    pret.patan %= MOD;
    temp -= vptemp[i].vertex;
  }

  vans[x] = pret.patan;

  for (i = 0; i < graph[x].size(); i++) {
    ptemp.vertex = pret.vertex - vptemp[i].vertex;
    ptemp.patan =
        pret.patan * mod_inverse(COM(pret.vertex - 1, vptemp[i].vertex), MOD);
    ptemp.patan %= MOD;
    ptemp.patan = ptemp.patan * mod_inverse(vptemp[i].patan, MOD);
    ptemp.patan %= MOD;
    inverse(graph[x][i], ptemp, vans, graph, vvpret);
  }

  return 0;
}

signed main(void) {
  // 変数の宣言
  int n, m;
  int h, w;
  int x, y;
  static int a[200010];
  static int b[200010];
  static int v[100010];
  static int p[100010];

  // よく使う変数
  int i, j, k, l;
  int flag = 0;
  int ans = 0;
  int count = 0;
  int temp = 0;
  int temp1 = 0;
  int temp2 = 0;
  int temp3 = 0;
  vector<int> vec;
  vector<int> vtemp;
  int max = 0;
  int min = INFINT;
  int len = 0;
  int sum = 0;
  int ok = 0;
  int ng = 0;
  char dummy;
  static char stemp[100010];
  vvi graph;

  // データの読み込み

  scanf("%lld", &n);
  // scanf_s("%lld",&n);

  // scanf("%s",&s);
  // scanf_s("%s",&s,20);

  for (i = 0; i < n - 1; i++) {
    scanf("%lld %lld", &a[i], &b[i]);
    // scanf_s("%lld %lld",&a[i],&b[i]);
  }

  //	printf("nは%dです\\n", n);
  //	printf("データの読み込み終了\\n");
  // 実際の処理

  //	clock_t start=clock();
  COMinit();

  vvi gtemp;
  vi come(n + 1, 0);

  for (i = 0; i <= n; i++) {
    graph.push_back(vtemp);
    gtemp.push_back(vtemp);
  }

  for (i = 0; i < n - 1; i++) {
    gtemp[a[i]].push_back(b[i]);
    gtemp[b[i]].push_back(a[i]);
  }

  makeGraph(1, gtemp, graph, come);
  fill(come.begin(), come.end(), 0); // comeは多分もう使わない？

  pret_t ptemp;
  vector<pret_t> vpret;
  vector<vector<pret_t>> vvpret(n + 1, vpret);

  ptemp = search(1, graph, vvpret);

  // printf("%lld",ptemp.patan);

  vi vans(n + 1, 0);

  inverse(1, ptemp, vans, graph, vvpret);

  for (i = 1; i <= n; i++) {
    printf("%lld\\n", vans[i]);
  }

  //	clock_t end=clock();

  //	printf("計算部分終了\\n");

  // 出力

  // printf("%d",(d[i-1][j-1]+1)/2);

  //	printf("time=%lf",(double)(end-start)/CLOCKS_PER_SEC);
  //	printf("結果の出力終了\\n");

  return 0;
}


Error: 0

Fixed submission:

// #include "pch.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define ll long long
#define fri(n) for (i = 0; i < (n); i++)
#define frj(n) for (j = 0; j < (n); i++)
#define min(p, q) ((p) < (q) ? (p) : (q))
#define max(p, q) ((p) > (q) ? (p) : (q))
#define INF 1000000000000000000 // 10^18
#define INFINT 2000000001       // 2*10^9+1
#define MOD 1000000007
#define MODANOTHER 998244353
#define PI acos(-1)

#define vi vector<int>
#define vvi vector<vector<int>>
#define vvvi vector<vector<vector<int>>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vvvd vector<vector<vector<double>>>

#define int long long

using namespace std;

const int MAX = 510000;

ll mod_inverse(int a, int M) {
  int x = M - 2;
  ll temp = a;
  ll ret = 1;
  // a^(MOD-1)==1(mod MOD)を利用
  // a^-1==a^(MOD-2) (mod MOD)を繰り返し二乗法で求める

  while (x != 0) {
    if (x % 2 == 1) {
      ret = (ret * temp) % M;
    }
    temp = (temp * temp) % M;
    x = x / 2;
  }
  return ret;
}

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

signed compare_ll(const void *a, const void *b) {
  ll *A = (ll *)a;
  ll *B = (ll *)b;
  if (*A < *B)
    return 1;
  if (*A > *B)
    return -1;
  return 0;
}

int makeGraph(int x, vvi &gtemp, vvi &graph, vi &come) {
  int i;

  come[x] = 1;

  for (i = 0; i < gtemp[x].size(); i++) {
    if (come[gtemp[x][i]] == 1)
      continue;
    graph[x].push_back(gtemp[x][i]);
    makeGraph(gtemp[x][i], gtemp, graph, come);
  }

  return 0;
}

struct pret_t {
  int vertex;
  int patan;
};

pret_t search(int x, vvi &graph, vector<vector<pret_t>> &vvpret) {
  int i;
  int temp;
  int sum;
  pret_t pret;
  pret_t ptemp;
  vector<pret_t> vptemp;

  for (i = 0; i < graph[x].size(); i++) {
    ptemp = search(graph[x][i], graph, vvpret);
    vptemp.push_back(ptemp);
  }
  vvpret[x] = vptemp;

  pret.vertex = 1;
  pret.patan = 1;

  for (i = 0; i < vptemp.size(); i++) {
    pret.vertex += vptemp[i].vertex;
  }

  temp = pret.vertex - 1;

  for (i = 0; i < vptemp.size(); i++) {
    pret.patan *= COM(temp, vptemp[i].vertex);
    pret.patan %= MOD;
    pret.patan *= vptemp[i].patan;
    pret.patan %= MOD;
    temp -= vptemp[i].vertex;
  }

  return pret;
}

int inverse(int x, pret_t prev, vi &vans, vvi &graph,
            vector<vector<pret_t>> &vvpret) {
  int i;
  int temp;
  pret_t pret;
  pret_t ptemp;
  vector<pret_t> vptemp;

  vptemp = vvpret[x];
  if (x != 1)
    vptemp.push_back(prev);

  pret.vertex = 1;
  pret.patan = 1;

  for (i = 0; i < vptemp.size(); i++) {
    pret.vertex += vptemp[i].vertex;
  }

  temp = pret.vertex - 1;

  for (i = 0; i < vptemp.size(); i++) {
    pret.patan *= COM(temp, vptemp[i].vertex);
    pret.patan %= MOD;
    pret.patan *= vptemp[i].patan;
    pret.patan %= MOD;
    temp -= vptemp[i].vertex;
  }

  vans[x] = pret.patan;

  for (i = 0; i < graph[x].size(); i++) {
    ptemp.vertex = pret.vertex - vptemp[i].vertex;
    ptemp.patan =
        pret.patan * mod_inverse(COM(pret.vertex - 1, vptemp[i].vertex), MOD);
    ptemp.patan %= MOD;
    ptemp.patan = ptemp.patan * mod_inverse(vptemp[i].patan, MOD);
    ptemp.patan %= MOD;
    inverse(graph[x][i], ptemp, vans, graph, vvpret);
  }

  return 0;
}

signed main(void) {
  // 変数の宣言
  int n, m;
  int h, w;
  int x, y;
  static int a[100010];
  static int b[100010];
  static int v[100010];
  static int p[100010];

  // よく使う変数
  int i, j, k, l;
  int flag = 0;
  int ans = 0;
  int count = 0;
  int temp = 0;
  int temp1 = 0;
  int temp2 = 0;
  int temp3 = 0;
  vector<int> vec;
  vector<int> vtemp;
  int max = 0;
  int min = INFINT;
  int len = 0;
  int sum = 0;
  int ok = 0;
  int ng = 0;
  char dummy;
  static char stemp[100010];
  vvi graph;

  // データの読み込み

  scanf("%lld", &n);
  // scanf_s("%lld",&n);

  // scanf("%s",&s);
  // scanf_s("%s",&s,20);

  for (i = 0; i < n - 1; i++) {
    scanf("%lld %lld", &a[i], &b[i]);
    // scanf_s("%lld %lld",&a[i],&b[i]);
  }

  //	printf("nは%dです\\n", n);
  //	printf("データの読み込み終了\\n");
  // 実際の処理

  //	clock_t start=clock();
  COMinit();

  vvi gtemp;
  vi come(n + 1, 0);

  for (i = 0; i <= n; i++) {
    graph.push_back(vtemp);
    gtemp.push_back(vtemp);
  }

  for (i = 0; i < n - 1; i++) {
    gtemp[a[i]].push_back(b[i]);
    gtemp[b[i]].push_back(a[i]);
  }

  makeGraph(1, gtemp, graph, come);
  fill(come.begin(), come.end(), 0); // comeは多分もう使わない？

  pret_t ptemp;
  vector<pret_t> vpret;
  vector<vector<pret_t>> vvpret(n + 1, vpret);

  ptemp = search(1, graph, vvpret);

  // printf("%lld",ptemp.patan);

  vi vans(n + 1, 0);

  inverse(1, ptemp, vans, graph, vvpret);

  for (i = 1; i <= n; i++) {
    printf("%lld\\n", vans[i]);
  }

  //	clock_t end=clock();

  //	printf("計算部分終了\\n");

  // 出力

  // printf("%d",(d[i-1][j-1]+1)/2);

  //	printf("time=%lf",(double)(end-start)/CLOCKS_PER_SEC);
  //	printf("結果の出力終了\\n");

  return 0;
}