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

#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;

using namespace std;

const int NMAX = 2 * 1e5;
int N;
vector<vector<int>> to(NMAX);
map<pair<int, int>, ll> dpv;
map<pair<int, int>, ll> treenum;

ll fact[NMAX + 1], invfact[NMAX + 1];

long pow(long a, long b) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 0) {
    long d = pow(a, b / 2);
    return (d * d) % MOD;
  } else {
    long d = pow(a, b - 1);
    return (a * d) % MOD;
  }
}

void dfs(ll v, ll parent) {
  if (dpv[{parent, v}] != 0) {
    return;
  }

  ll ans = 1;
  ll num = 1;

  for (int i = 0; i < to[v].size(); i++) {
    if (to[v][i] != parent and to[v][i] != v) {
      dfs(to[v][i], v);
      num += treenum[{v, to[v][i]}];
      ans = ans * dpv[{v, to[v][i]}] % MOD;
      ans = ans * invfact[treenum[{v, to[v][i]}]] % MOD;
    }
  }

  treenum[{parent, v}] = num;
  treenum[{v, v}] = N;
  dpv[{parent, v}] = ans * fact[num - 1] % MOD;
}

void bfs(ll v, ll parent) {
  if (dpv[{parent, parent}] == 0) {
    treenum[{parent, v}] = N - treenum[{v, parent}];
    ll vl = dpv[{v, v}] * invfact[treenum[{v, v}] - 1] % MOD;
    vl = vl * fact[treenum[{v, v}] - 1 - treenum[{v, parent}]] % MOD;
    vl = vl * fact[treenum[{v, parent}]] % (MOD);
    dpv[{parent, parent}] = vl * invfact[treenum[{parent, v}]] % MOD;
    dpv[{parent, parent}] =
        dpv[{parent, parent}] * invfact[treenum[{v, parent}] - 1] % MOD;
    dpv[{parent, parent}] = dpv[{parent, parent}] * fact[N - 1] % (MOD);
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  cin >> N;

  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  fact[0] = 1;
  invfact[0] = 1;
  for (ll i = 0; i < NMAX; i++) {
    fact[i + 1] = fact[i] * (i + 1) % MOD;
  }
  invfact[NMAX] = pow(fact[NMAX], MOD - 2);
  for (ll i = NMAX - 1; i > 0; i--) {
    if (i == 0)
      exit(1);
    invfact[i] = invfact[i + 1] * (i + 1) % MOD;
  }

  dfs(1, 1);

  queue<pair<int, int>> bfs_queue;

  for (ll i = 0; i < to[1].size(); i++) {
    pair<int, int> pr = make_pair(1, to[1][i]);
    bfs_queue.push(pr);
  }

  while (bfs_queue.empty() == false) {
    pair<int, int> p = bfs_queue.front();
    bfs_queue.pop();
    bfs(p.first, p.second);
    for (ll i = 0; i < to[p.second].size(); i++) {
      if (to[p.second][i] != p.first) {
        bfs_queue.push(make_pair(p.second, to[p.second][i]));
      }
    }
  }

  for (ll i = 1; i < N + 1; i++) {
    cout << dpv[{i, i}] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;

using namespace std;

const int NMAX = 2 * 1e5;
int N;
vector<vector<int>> to(NMAX);
vector<vector<ll>> dpv;
vector<vector<ll>> treenum;

ll fact[NMAX + 1], invfact[NMAX + 1];

long pow(long a, long b) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 0) {
    long d = pow(a, b / 2);
    return (d * d) % MOD;
  } else {
    long d = pow(a, b - 1);
    return (a * d) % MOD;
  }
}

void dfs(ll v, ll parent) {
  if (dpv[parent][v] != 0) {
    return;
  }

  ll ans = 1;
  ll num = 1;

  for (int i = 0; i < to[v].size(); i++) {
    if (to[v][i] != parent and to[v][i] != v) {
      dfs(to[v][i], v);
      num += treenum[v][to[v][i]];
      ans = ans * dpv[v][to[v][i]] % MOD;
      ans = ans * invfact[treenum[v][to[v][i]]] % MOD;
    }
  }

  treenum[parent][v] = num;
  treenum[v][v] = N;
  dpv[parent][v] = ans * fact[num - 1] % MOD;
}

void bfs(ll v, ll parent) {
  if (dpv[parent][parent] == 0) {
    treenum[parent][v] = N - treenum[v][parent];
    ll vl = dpv[v][v] * invfact[treenum[v][v] - 1] % MOD;
    vl = vl * fact[treenum[v][v] - 1 - treenum[v][parent]] % MOD;
    vl = vl * fact[treenum[v][parent]] % (MOD);
    dpv[parent][parent] = vl * invfact[treenum[parent][v]] % MOD;
    dpv[parent][parent] =
        dpv[parent][parent] * invfact[treenum[v][parent] - 1] % MOD;
    dpv[parent][parent] = dpv[parent][parent] * fact[N - 1] % (MOD);
  }
}

int main() {

  cin >> N;

  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  dpv.assign(N + 1, vector<ll>(N + 1, 0));
  treenum.assign(N + 1, vector<ll>(N + 1, 0));

  fact[0] = 1;
  invfact[0] = 1;
  for (ll i = 0; i < NMAX; i++) {
    fact[i + 1] = fact[i] * (i + 1) % MOD;
  }
  invfact[NMAX] = pow(fact[NMAX], MOD - 2);
  for (ll i = NMAX - 1; i > 0; i--) {
    if (i == 0)
      exit(1);
    invfact[i] = invfact[i + 1] * (i + 1) % MOD;
  }

  dfs(1, 1);

  queue<pair<int, int>> bfs_queue;

  for (ll i = 0; i < to[1].size(); i++) {
    pair<int, int> pr = make_pair(1, to[1][i]);
    bfs_queue.push(pr);
  }

  while (bfs_queue.empty() == false) {
    pair<int, int> p = bfs_queue.front();
    bfs_queue.pop();
    bfs(p.first, p.second);
    for (ll i = 0; i < to[p.second].size(); i++) {
      if (to[p.second][i] != p.first) {
        bfs_queue.push(make_pair(p.second, to[p.second][i]));
      }
    }
  }

  for (ll i = 1; i < N + 1; i++) {
    cout << dpv[i][i] << endl;
  }

  return 0;
}