Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <chrono>
using namespace std;
using namespace std::chrono;
// using namespace __gnu_pbds;
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define M 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \
  for (long blockTime = 0;                                                     \
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \
       debug("%s time : %.4fs", d,                                             \
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 3e18
#define sm -2e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;

// #define ordered_set tree<pii, null_type,less<pii>,
// rb_tree_tag,tree_order_statistics_node_update>

void pr_init() {

#ifndef ONLINE_JUDGE
  freopen("gin.txt", "r", stdin);
  freopen("gout.txt", "w", stdout);

#endif
}

// dp[i] -- > number of ways to color the subtree rooted at i
int sub[100005], dp[100005];
vector<vii> gp;
int n, m;
void dfs1(int u, int p) {
  sub[u] = 1;
  for (auto x : gp[u])
    if (x != p) {
      dfs1(x, u);
      sub[u] = ((sub[x] + 1) % m * sub[u] % m) % m;
    }
}

void dfs2(int u, int p, int vl) {
  dp[u] = (sub[u] % m * (vl + 1) % m) % m;

  vii ar, suffix, prefix;
  for (int i = 0; i < sz(gp[u]); i++) {
    if (gp[u][i] != p)
      ar.pb(sub[gp[u][i]] + 1);
    else
      ar.pb(1);
  }

  int s = sz(ar);
  suffix.resize(s);
  prefix.resize(s);
  prefix[0] = 1;
  suffix[s - 1] = 1;
  for (int i = 1; i < s; i++) {
    prefix[i] = (prefix[i - 1] % m * ar[i - 1] % m) % m;
  }
  for (int i = s - 2; i >= 0; i--) {
    suffix[i] = (suffix[i + 1] % m * ar[i + 1] % m) % m;
  }

  for (int i = 0; i < sz(gp[u]); i++)
    if (gp[u][i] != p) {
      int cvl = ((vl + 1) % m * suffix[i] % m) % m;
      cvl = (cvl % m * prefix[i] % m) % m;
      dfs2(gp[u][i], u, cvl);
    }
}
void solve() {
  cin >> n >> m;
  gp.assign(n + 1, vii());

  forf(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    gp[u].pb(v);
    gp[v].pb(u);
  }
  dfs1(1, -1);
  dfs2(1, -1, 0);
  for (int i = 1; i <= n; i++) {
    cout << dp[i] % m << "\n";
  }
}

int32_t main() {
  pr_init();
  // fastio;

  auto start = high_resolution_clock::now();

  solve();
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);
  // cout << "Time taken by function: "
  //       << duration.count() << " microseconds" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
#include <chrono>
using namespace std;
using namespace std::chrono;
// using namespace __gnu_pbds;
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define M 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \
  for (long blockTime = 0;                                                     \
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \
       debug("%s time : %.4fs", d,                                             \
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 3e18
#define sm -2e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;

// #define ordered_set tree<pii, null_type,less<pii>,
// rb_tree_tag,tree_order_statistics_node_update>

void pr_init() {

#ifndef ONLINE_JUDGE
  freopen("gin.txt", "r", stdin);
  freopen("gout.txt", "w", stdout);

#endif
}

// dp[i] -- > number of ways to color the subtree rooted at i
int sub[100005], dp[100005];
vector<vii> gp;
int n, m;
void dfs1(int u, int p) {
  sub[u] = 1;
  for (auto x : gp[u])
    if (x != p) {
      dfs1(x, u);
      sub[u] = ((sub[x] + 1) % m * sub[u] % m) % m;
    }
}

void dfs2(int u, int p, int vl) {
  dp[u] = (sub[u] % m * (vl + 1) % m) % m;

  vii ar, suffix, prefix;
  for (int i = 0; i < sz(gp[u]); i++) {
    if (gp[u][i] != p)
      ar.pb(sub[gp[u][i]] + 1);
    else
      ar.pb(1);
  }

  int s = sz(ar);
  suffix.resize(s);
  prefix.resize(s);
  prefix[0] = 1;
  suffix[s - 1] = 1;
  for (int i = 1; i < s; i++) {
    prefix[i] = (prefix[i - 1] % m * ar[i - 1] % m) % m;
  }
  for (int i = s - 2; i >= 0; i--) {
    suffix[i] = (suffix[i + 1] % m * ar[i + 1] % m) % m;
  }

  for (int i = 0; i < sz(gp[u]); i++)
    if (gp[u][i] != p) {
      int cvl = ((vl + 1) % m * suffix[i] % m) % m;
      cvl = (cvl % m * prefix[i] % m) % m;
      dfs2(gp[u][i], u, cvl);
    }
}
void solve() {
  cin >> n >> m;
  gp.assign(n + 1, vii());

  if (n == 1) {
    cout << "1\n";
    return;
  }
  forf(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    gp[u].pb(v);
    gp[v].pb(u);
  }
  dfs1(1, -1);
  dfs2(1, -1, 0);
  for (int i = 1; i <= n; i++) {
    cout << dp[i] % m << "\n";
  }
}

int32_t main() {
  pr_init();
  // fastio;

  auto start = high_resolution_clock::now();

  solve();
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);
  // cout << "Time taken by function: "
  //       << duration.count() << " microseconds" << endl;
}