Problem p03181 (C++)

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

#define pb push_back
#define fi first
#define se second
#define ll long long
#define pq priority_queue
#define mp make_pair
#define pii pair<int, int>
#define mod 998244353

int lowbit(int x) { return x & (-x); }
int n, m;
vector<int> edge[100010];
int ans[100010];
int ANS[100010];
vector<int> son[100010];
vector<int> pre[100010], suf[100010];

int dfs(int node, int lst) {
  ans[node] = 1;
  for (int i = 0; i < edge[node].size(); i++) {
    if (edge[node][i] == lst)
      continue;
    son[node].pb(edge[node][i]);
    ans[node] = (ll)(ans[node]) * (ll)(dfs(edge[node][i], node) + 1) % m;
    //		cout<<node<<" "<<ans[node]<<endl;
  }
  for (int i = 0; i < son[node].size(); i++) {
    if (i == 0)
      pre[node].pb((ans[son[node][i]] + 1) % m);
    else
      pre[node].pb((ll)pre[node][i - 1] * (ll)(ans[son[node][i]] + 1) % m);
  }
  for (int i = son[node].size() - 1; i >= 0; i--) {
    if (i == son[node].size() - 1)
      suf[node].pb((ans[son[node][i]] + 1) % m);
    else
      suf[node].pb((ll)suf[node][i - 1] * (ll)(ans[son[node][i]] + 1) % m);
  }
  reverse(suf[node].begin(), suf[node].end());
  return ans[node];
}

void getANS(int node, int lst, int cnt) {
  ANS[node] = (ll)ans[node] * (ll)(cnt + 1) % m;
  for (int i = 0; i < son[node].size(); i++) {
    ll tmp = 1ll;
    if (i)
      tmp *= (ll)pre[node][i - 1];
    if (i < son[node].size() - 1)
      tmp *= (ll)suf[node][i + 1];
    tmp %= m;
    //		cout<<son[node][i]<<" "<<tmp<<endl;
    tmp *= (ll)(cnt + 1), tmp %= m;
    getANS(son[node][i], node, tmp);
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    edge[x].pb(y);
    edge[y].pb(x);
  }
  cout << dfs(1, -1) << endl;
  getANS(1, -1, 0);
  for (int i = 2; i <= n; i++)
    cout << ANS[i] << endl;
  return 0;
}


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

#define pb push_back
#define fi first
#define se second
#define ll long long
#define pq priority_queue
#define mp make_pair
#define pii pair<int, int>
#define mod 998244353

int lowbit(int x) { return x & (-x); }
int n, m;
vector<int> edge[100010];
int ans[100010];
int ANS[100010];
vector<int> son[100010];
vector<int> pre[100010], suf[100010];

int dfs(int node, int lst) {
  ans[node] = 1;
  for (int i = 0; i < edge[node].size(); i++) {
    if (edge[node][i] == lst)
      continue;
    son[node].pb(edge[node][i]);
    ans[node] = (ll)(ans[node]) * (ll)(dfs(edge[node][i], node) + 1) % m;
    //		cout<<node<<" "<<ans[node]<<endl;
  }
  for (int i = 0; i < son[node].size(); i++) {
    if (i == 0)
      pre[node].pb((ans[son[node][i]] + 1) % m);
    else
      pre[node].pb((ll)pre[node][i - 1] * (ll)(ans[son[node][i]] + 1) % m);
  }
  for (int i = son[node].size() - 1; i >= 0; i--) {
    if (i == son[node].size() - 1)
      suf[node].pb((ans[son[node][i]] + 1) % m);
    else
      suf[node].pb((ll)suf[node][son[node].size() - 1 - i - 1] *
                   (ll)(ans[son[node][i]] + 1) % m);
  }
  reverse(suf[node].begin(), suf[node].end());
  return ans[node];
}

void getANS(int node, int lst, int cnt) {
  ANS[node] = (ll)ans[node] * (ll)(cnt + 1) % m;
  for (int i = 0; i < son[node].size(); i++) {
    ll tmp = 1ll;
    if (i)
      tmp *= (ll)pre[node][i - 1];
    if (i < son[node].size() - 1)
      tmp *= (ll)suf[node][i + 1];
    tmp %= m;
    //		cout<<son[node][i]<<" "<<tmp<<endl;
    tmp *= (ll)(cnt + 1), tmp %= m;
    getANS(son[node][i], node, tmp);
  }
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    edge[x].pb(y);
    edge[y].pb(x);
  }
  cout << dfs(1, -1) << endl;
  getANS(1, -1, 0);
  for (int i = 2; i <= n; i++)
    cout << ANS[i] << endl;
  return 0;
}