Problem p03181 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;

const int len = 1e5 + 5;
int n, m;
int par[len];
vector<ii> adj[len];
vector<int> dp[len], pref[len], suf[len];

int add(int a, int b) { return (a + b) % m; }

int mul(int a, int b) { return (a * 1LL * b) % m; }

void dfs1(int u, int x) {
  dp[u][x] = 1;
  for (int j = 1; j < adj[u].size(); j++) {
    ii v = adj[u][j];
    if (v.fi == par[u])
      continue;

    par[v.fi] = u;
    dfs1(v.fi, v.se);
    dp[u][x] = mul(dp[u][x], dp[v.fi][v.se]);
  }
  dp[u][x] = add(dp[u][x], 1);
}

void dfs2(int u) {
  pref[u][0] = 1;
  for (int j = 1; j < adj[u].size(); j++) {
    ii v = adj[u][j];
    pref[u][j] = mul(pref[u][j - 1], dp[v.fi][v.se]);
  }

  suf[u][adj[u].size()] = 1;
  for (int j = adj[u].size() - 1; j >= 1; j--) {
    ii v = adj[u][j];
    suf[u][j] = mul(suf[u][j + 1], dp[v.fi][v.se]);
  }

  for (int j = 1; j < adj[u].size(); j++) {
    ii v = adj[u][j];
    dp[u][j] = add(mul(pref[u][j - 1], suf[u][j + 1]), 1);
    if (v.fi != par[u])
      dfs2(v.fi);
  }

  dp[u][0] = add(suf[u][1], 1);
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    adj[i].pb(mp(i, 0));
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    adj[a].pb(mp(b, adj[b].size()));
    adj[b].pb(mp(a, adj[a].size() - 1));
  }

  for (int i = 1; i <= n; i++) {
    dp[i].resize(adj[i].size());
    pref[i].resize(adj[i].size());
    suf[i].resize(adj[i].size());
  }

  dfs1(1, 0);
  dfs2(1);

  for (int i = 1; i <= n; i++)
    printf("%d\n", add(dp[i][0], m - 1));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;

const int len = 1e5 + 5;
int n, m;
int par[len];
vector<ii> adj[len];
vector<int> dp[len], pref[len], suf[len];

int add(int a, int b) { return (a + b) % m; }

int mul(int a, int b) { return (a * 1LL * b) % m; }

void dfs1(int u, int x) {
  dp[u][x] = 1;
  for (int j = 1; j < adj[u].size(); j++) {
    ii v = adj[u][j];
    if (v.fi == par[u])
      continue;

    par[v.fi] = u;
    dfs1(v.fi, v.se);
    dp[u][x] = mul(dp[u][x], dp[v.fi][v.se]);
  }
  dp[u][x] = add(dp[u][x], 1);
}

void dfs2(int u) {
  pref[u][0] = 1;
  for (int j = 1; j < adj[u].size(); j++) {
    ii v = adj[u][j];
    pref[u][j] = mul(pref[u][j - 1], dp[v.fi][v.se]);
  }

  suf[u][adj[u].size()] = 1;
  for (int j = adj[u].size() - 1; j >= 1; j--) {
    ii v = adj[u][j];
    suf[u][j] = mul(suf[u][j + 1], dp[v.fi][v.se]);
  }

  for (int j = 1; j < adj[u].size(); j++) {
    ii v = adj[u][j];
    dp[u][j] = add(mul(pref[u][j - 1], suf[u][j + 1]), 1);
    if (v.fi != par[u])
      dfs2(v.fi);
  }

  dp[u][0] = add(suf[u][1], 1);
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    adj[i].pb(mp(i, 0));
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    adj[a].pb(mp(b, adj[b].size()));
    adj[b].pb(mp(a, adj[a].size() - 1));
  }

  for (int i = 1; i <= n; i++) {
    dp[i].resize(adj[i].size());
    pref[i].resize(adj[i].size());
    suf[i].resize(adj[i].size() + 1);
  }

  dfs1(1, 0);
  dfs2(1);

  for (int i = 1; i <= n; i++)
    printf("%d\n", add(dp[i][0], m - 1));
  return 0;
}