Problem p03181 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
// int MOD = 1000000007;
int N, M;
vector<vector<int>> g;
vector<vector<int>> dp;
vector<vector<int>> sum1;
vector<vector<int>> sum2;
vector<int> X;
map<pair<int, int>, int> mp;

int solve(int a, int x) {

  // cerr << a << " " << x << endl;
  if (dp[a][x] == -1) {
    int ne = g[a][x];

    if (X[ne] == -1) {
      X[ne] = a;
      dp[a][x] = 1;
      for (int i = 0; i < g[ne].size(); i++) {
        if (g[ne][i] != a) {
          dp[a][x] = (dp[a][x] * solve(ne, i)) % M;
        }
      }
      dp[a][x]++;
    } else {
      solve(ne, mp[make_pair(ne, X[ne])]);

      for (int i = 0; i < g[ne].size(); i++) {
        sum1[ne][i + 1] = (sum1[ne][i] * dp[ne][i]) % M;
      }
      for (int i = (int)g[ne].size() - 1; i >= 0; i--) {
        sum2[ne][i] = (sum2[ne][i + 1] * dp[ne][i]) % M;
      }

      X[ne] = -2;
    }
    if (X[ne] == -2) {
      int idx = mp[make_pair(ne, a)];
      dp[a][x] = (sum1[ne][idx] * sum2[ne][idx + 1]) % M;
      dp[a][x]++;
    }
  }
  return dp[a][x];
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  g.resize(N);
  dp.resize(N);
  sum1.resize(N);
  sum2.resize(N);
  X.resize(N, -1);
  int res = 1;
  int a, b;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
    dp[a].push_back(-1);
    dp[b].push_back(-1);
    sum1[a].push_back(1);
    sum1[b].push_back(1);
    sum2[a].push_back(1);
    sum2[b].push_back(1);
  }
  for (int i = 0; i < N; i++) {
    sum1[i].push_back(1);
    sum2[i].push_back(1);
  }
  for (int i = 0; i < N; i++) {
    res = 1;
    for (int j = 0; j < g[i].size(); j++) {
      mp[make_pair(i, g[i][j])] = j;
    }
  }
  for (int i = 0; i < N; i++) {
    res = 1;
    for (int j = 0; j < g[i].size(); j++) {
      res = (res * solve(i, j)) % M;
    }
    cout << res << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
// int MOD = 1000000007;
int N, M;
vector<vector<int>> g;
vector<vector<int>> dp;
vector<vector<int>> sum1;
vector<vector<int>> sum2;
vector<int> X;
map<pair<int, int>, int> mp;

int solve(int a, int x) {

  // cerr << a << " " << x << endl;
  if (dp[a][x] == -1) {
    int ne = g[a][x];

    if (X[ne] == -1) {
      X[ne] = a;
      dp[a][x] = 1;
      for (int i = 0; i < g[ne].size(); i++) {
        if (g[ne][i] != a) {
          dp[a][x] = (dp[a][x] * solve(ne, i)) % M;
        }
      }
      dp[a][x]++;
    } else if (X[ne] != -2) {
      solve(ne, mp[make_pair(ne, X[ne])]);

      for (int i = 0; i < g[ne].size(); i++) {
        sum1[ne][i + 1] = (sum1[ne][i] * dp[ne][i]) % M;
      }
      for (int i = (int)g[ne].size() - 1; i >= 0; i--) {
        sum2[ne][i] = (sum2[ne][i + 1] * dp[ne][i]) % M;
      }

      X[ne] = -2;
    }
    if (X[ne] == -2) {
      int idx = mp[make_pair(ne, a)];
      dp[a][x] = (sum1[ne][idx] * sum2[ne][idx + 1]) % M;
      dp[a][x]++;
    }
  }
  return dp[a][x];
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  g.resize(N);
  dp.resize(N);
  sum1.resize(N);
  sum2.resize(N);
  X.resize(N, -1);
  int res = 1;
  int a, b;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
    dp[a].push_back(-1);
    dp[b].push_back(-1);
    sum1[a].push_back(1);
    sum1[b].push_back(1);
    sum2[a].push_back(1);
    sum2[b].push_back(1);
  }
  for (int i = 0; i < N; i++) {
    sum1[i].push_back(1);
    sum2[i].push_back(1);
  }
  for (int i = 0; i < N; i++) {
    res = 1;
    for (int j = 0; j < g[i].size(); j++) {
      mp[make_pair(i, g[i][j])] = j;
    }
  }
  for (int i = 0; i < N; i++) {
    res = 1;
    for (int j = 0; j < g[i].size(); j++) {
      res = (res * solve(i, j)) % M;
    }
    cout << res << endl;
  }
}