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

#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
vector<vector<int>> neighbours(200000);
int parent[200000];
int fact[200000];
pair<int, int> ways[200000];
int num_ways[200000];
long long gcdExtended(int a, int b, long long *x, long long *y);
long long modu(long long p) { return ((p % mod) + mod) % mod; }
// Function to find modulo inverse of a
int modInverse(int a, int m) {
  long long x, y;
  long long g = gcdExtended(a, m, &x, &y);
  if (g != 1)
    return 1;
  else {
    // m is added to handle negative x
    int res = (x % m + m) % m;
    return res;
  }
}

// C function for extended Euclidean Algorithm
long long gcdExtended(int a, int b, long long *x, long long *y) {
  // Base Case
  if (a == 0) {
    *x = 0, *y = 1;
    return (long long)b;
  }

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

  // Update x and y using results of recursive
  // call
  *x = y1 - x1 * (b / a);
  *y = x1;

  return gcd;
}
long long ncr(int p, int q) {
  long long x = modu(((long long)fact[p]) * modInverse(fact[q], mod));
  long long y = modu((x)*modInverse(fact[p - q], mod));
  return y;
}
void dfs(int node) {
  // cout<<"Node"<<endl;
  int num = 0;
  for (int j = 0; j < neighbours[node].size(); j++) {
    if (neighbours[node][j] != parent[node]) {
      parent[neighbours[node][j]] = node;
      dfs(neighbours[node][j]);

      num = num + ways[neighbours[node][j]].first;
    }
  }
  long long s = 1;
  int chosen = 0;
  for (int j = 0; j < neighbours[node].size(); j++) {
    if (neighbours[node][j] != parent[node]) {
      s = modu(modu(s * ways[neighbours[node][j]].second) *
               ncr(num - chosen, ways[neighbours[node][j]].first));
      chosen = chosen + ways[neighbours[node][j]].first;
    }
  }
  ways[node].second = (int)s;
  ways[node].first = num + 1;
}

int main() {

  int N;
  cin >> N;
  for (int j = 0; j < N - 1; j++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    neighbours[b].push_back(a);
    neighbours[a].push_back(b);
  }
  fact[0] = 1;
  for (int j = 1; j <= N; j++) {
    fact[j] = modu(((long long)fact[j - 1]) * j);
  }
  parent[0] = 0;
  dfs(0);
  //	cout<<"ways"<<endl;
  //	for(int jj=0;jj<N;jj++){
  //		cout<<ways[jj].second<<" ";
  //	}
  //	cout<<endl;
  int current = 0;
  num_ways[0] = ways[0].second;
  queue<int> q;
  q.push(current);
  while (!q.empty()) {
    current = q.front();
    //	cout<<current<<endl;
    int chosen = 0;
    if (current != 0) {
      long long s = (long long)num_ways[parent[current]];
      s = modu(s * modInverse(ncr(N - 1, ways[current].first), mod));
      s = modu(s * ncr(N - 1, ways[current].first - 1));
      num_ways[current] = (int)s;
    }
    q.pop();
    for (int j = 0; j < neighbours[current].size(); j++) {
      if (neighbours[current][j] != parent[current]) {
        q.push(neighbours[current][j]);
      }
    }
  }
  for (int j = 0; j < N; j++) {
    cout << num_ways[j] << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
vector<vector<int>> neighbours(200000);
int visited[200000];
int parent[200000];
int fact[200000];
pair<int, int> ways[20000];
int num_ways[200000];
long long gcdExtended(int a, int b, long long *x, long long *y);
long long modu(long long p) { return ((p % mod) + mod) % mod; }
// Function to find modulo inverse of a
int modInverse(int a, int m) {
  long long x, y;
  long long g = gcdExtended(a, m, &x, &y);
  if (g != 1)
    return 1;
  else {
    // m is added to handle negative x
    int res = (x % m + m) % m;
    return res;
  }
}

// C function for extended Euclidean Algorithm
long long gcdExtended(int a, int b, long long *x, long long *y) {
  // Base Case
  if (a == 0) {
    *x = 0, *y = 1;
    return (long long)b;
  }

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

  // Update x and y using results of recursive
  // call
  *x = y1 - x1 * (b / a);
  *y = x1;

  return gcd;
}
long long ncr(int p, int q) {
  long long x = modu(((long long)fact[p]) * modInverse(fact[q], mod));
  long long y = modu((x)*modInverse(fact[p - q], mod));
  return y;
}
pair<int, int> dfs(int node) {
  // cout<<"Node"<<endl;
  int num = 0;
  for (int j = 0; j < neighbours[node].size(); j++) {
    if (neighbours[node][j] != parent[node]) {
      parent[neighbours[node][j]] = node;
      pair<int, int> c = dfs(neighbours[node][j]);

      num = num + c.first;
    }
  }
  long long s = 1;
  int chosen = 0;
  for (int j = 0; j < neighbours[node].size(); j++) {
    if (neighbours[node][j] != parent[node]) {
      s = modu(modu(s * ways[neighbours[node][j]].second) *
               ncr(num - chosen, ways[neighbours[node][j]].first));
      chosen = chosen + ways[neighbours[node][j]].first;
    }
  }
  ways[node].second = (int)s;
  ways[node].first = num + 1;
  return make_pair(num + 1, (int)s);
}

int main() {

  int N;
  cin >> N;
  for (int j = 0; j < N - 1; j++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    neighbours[b].push_back(a);
    neighbours[a].push_back(b);
  }
  fact[0] = 1;
  for (int j = 1; j <= N; j++) {
    fact[j] = modu(((long long)fact[j - 1]) * j);
  }
  parent[0] = 0;
  dfs(0);
  //	cout<<"ways"<<endl;
  //	for(int jj=0;jj<N;jj++){
  //		cout<<ways[jj].second<<" ";
  //	}
  cout << endl;
  int current = 0;
  visited[current] = 1;
  num_ways[0] = ways[0].second;
  queue<int> q;
  q.push(current);
  while (!q.empty()) {
    current = q.front();
    int chosen = 0;
    if (current != 0) {
      int q = num_ways[parent[current]];
      long long s = (long long)q;
      s = modu(s * modInverse(ncr(N - 1, ways[current].first), mod));
      //			for(int
      //j=0;j<neighbours[parent[current]].size();j++){
      //				if(neighbours[parent[current]][j] !=
      //parent[parent[current]]){
      //
      //					chosen = chosen +
      //ways[neighbours[parent[current]][j]].first;
      //				}
      //			}
      s = modu(s * ncr(N - 1, ways[current].first - 1));
      //	cout<<" s1 "<<" "<<current<<" "<<s<<endl;
      //			int chosen = ways[current].first-1;
      //			for(int
      //j=0;j<neighbours[parent[current]].size();j++){
      //				if((neighbours[parent[current]][j] !=
      //parent[parent[current]]) && (neighbours[parent[current]][j] !=
      //current)){
      //					//cout<<" n "<<N-2-chosen<<"
      //"<<ways[neighbours[parent[current]][j]].first<<endl; 					s = modu(s*
      //ncr(N-2-chosen,ways[neighbours[parent[current]][j]].first)); 					chosen =
      //chosen + ways[neighbours[parent[current]][j]].first;
      //				}
      //			}
      //	cout<<" s2 "<<s<<endl;
      num_ways[current] = (int)s;
    }
    q.pop();
    for (int j = 0; j < neighbours[current].size(); j++) {
      if (!visited[neighbours[current][j]]) {
        q.push(neighbours[current][j]);
        visited[neighbours[current][j]] = 1;
      }
    }
  }
  for (int j = 0; j < N; j++) {
    cout << num_ways[j] << endl;
  }
  return 0;
}