Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, t) for (int i = s; i < t; i++)
#define RNG(i, s, t, u) for (int i = s; i < t; i += u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()

struct Edge {
  int from;
  int to;
  long cost;
};

vl dp(100010, -1);
vector<vector<Edge>> Graph(100010);
vl ans(100010, -1);

long dfs(int v, int pv, long m) {
  if (dp[v] != -1)
    return dp[v];
  dp[v] = 1;
  for (Edge ed : Graph[v]) {
    int nv = ed.to;
    if (nv == pv)
      continue;
    dp[v] = (dp[v] * (dfs(nv, v, m) + 1)) % m;
  }
  return dp[v];
}

void dfs2(int v, int pv, long rec, long m) {
  int s = Graph.at(v).size();
  vl acm(s + 1, 1);
  vl rev(s + 1, 1);
  rep(i, s) {
    if (Graph.at(v).at(i).to != pv) {
      acm.at(i + 1) = (acm.at(i) * (dp[Graph.at(v).at(i).to] + 1)) % m;
    } else {
      acm.at(i + 1) = (acm.at(i) * (rec + 1)) % m;
    }
    if (Graph.at(v).at(m - 1 - i).to != pv) {
      rev.at(s - 1 - i) =
          (rev.at(s - i) * (dp[Graph.at(v).at(s - 1 - i).to] + 1)) % m;
    } else {
      rev.at(s - 1 - i) = (rev.at(s - i) * (rec + 1)) % m;
    }
  }
  ans[v] = acm[s];
  rep(i, s) {
    int nv = Graph.at(v).at(i).to;
    if (nv == pv)
      continue;
    long r = acm[i] * rev[i + 1] % m;
    dfs2(nv, v, r, m);
  }
}

int main() {
  long n, m;
  cin >> n >> m;
  rep(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    Graph.at(x).push_back({x, y, 1});
    Graph.at(y).push_back({y, x, 1});
  }
  ans[0] = dfs(0, -1, m);
  dfs2(0, -1, 1, m);
  rep(i, n) { cout << ans[i] << endl; }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, t) for (int i = s; i < t; i++)
#define RNG(i, s, t, u) for (int i = s; i < t; i += u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()

struct Edge {
  int from;
  int to;
  long cost;
};

vl dp(100010, -1);
vector<vector<Edge>> Graph(100010);
vl ans(100010, -1);

long dfs(int v, int pv, long m) {
  if (dp[v] != -1)
    return dp[v];
  dp[v] = 1;
  for (Edge ed : Graph[v]) {
    int nv = ed.to;
    if (nv == pv)
      continue;
    dp[v] = (dp[v] * (dfs(nv, v, m) + 1)) % m;
  }
  return dp[v];
}

void dfs2(int v, int pv, long rec, long m) {
  int s = Graph.at(v).size();
  vl acm(s + 1, 1);
  vl rev(s + 1, 1);
  rep(i, s) {
    if (Graph.at(v).at(i).to != pv) {
      acm.at(i + 1) = (acm.at(i) * (dp[Graph.at(v).at(i).to] + 1)) % m;
    } else {
      acm.at(i + 1) = (acm.at(i) * (rec + 1)) % m;
    }
    if (Graph.at(v).at(s - 1 - i).to != pv) {
      rev.at(s - 1 - i) =
          (rev.at(s - i) * (dp[Graph.at(v).at(s - 1 - i).to] + 1)) % m;
    } else {
      rev.at(s - 1 - i) = (rev.at(s - i) * (rec + 1)) % m;
    }
  }
  ans[v] = acm[s];
  rep(i, s) {
    int nv = Graph.at(v).at(i).to;
    if (nv == pv)
      continue;
    long r = acm[i] * rev[i + 1] % m;
    dfs2(nv, v, r, m);
  }
}

int main() {
  long n, m;
  cin >> n >> m;
  rep(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    Graph.at(x).push_back({x, y, 1});
    Graph.at(y).push_back({y, x, 1});
  }
  ans[0] = dfs(0, -1, m);
  dfs2(0, -1, 1, m);
  rep(i, n) { cout << ans[i] << endl; }
}