Language: C++

#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

void dfs1(const vector<vector<long>> &adj_lists, long M, vector<long> &dp1s,
          long i, vector<long> &us) {
  const long N = adj_lists.size();
  long dp = 1;
  us[i] = 1;
  for (long j : adj_lists[i]) {
    if (!us[j]) {
      dfs1(adj_lists, M, dp1s, j, us);
      dp = dp * (dfs1(adj_lists, M, dp1s, j, us) + 1) % M;
    //   dp = dp * (dp1s[j] + 1);
    }
  }
  dp1s[i] = dp;
}

void dfs2(const vector<vector<long>> &adj_lists, long M, vector<long> &dp1s,
          vector<long> &dp2s, long i, vector<long> &us) {
  const long N = adj_lists.size();
  // calculate dp1s[i] and dps2[i]
  long dp = 1;
  us[i] = 1;
  for (long j : adj_lists[i]) {
    dp = dp * (dp1s[j] + 1) % M;
    // dp = dp * (dp1s[j] + 1);
  }
  dp1s[i] = dp;
  dp2s[i] = dp;

//   printf("i = %ld\n", i);
//   for (long j = 0; j < N; j++) {
//     printf("j = %ld, dp1s[j] = %ld\n", j, dp1s[j]);
//   }

  // calculate accls, accrs to facilitate calculation of
  // dp1s[i] when calling dfs2 on i's neighbours
  const long K = adj_lists[i].size();
  vector<long> accls(K, 0), accrs(K, 0);
  for (long k = 0; k < K; k++) {
    const long j = adj_lists[i][k];
    accls[k] = dp1s[j] + 1;
    accrs[k] = dp1s[j] + 1;
  }
  for (long k = 1; k < K; k++) {
    accls[k] = accls[k] * accls[k - 1] % M;
  }
  for (long k = K - 2; k >= 0; k--) {
    accrs[k] = accrs[k] * accrs[k + 1] % M;
  }
  for (long k = 0; k < K; k++) {
    const long j = adj_lists[i][k];
    if (!us[j]) {
      dp1s[i] = (k > 0 ? accls[k - 1] : 1) * (k + 1 < K ? accrs[k + 1] : 1) % M;
      dfs2(adj_lists, M, dp1s, dp2s, j, us);
    }
  }
}

int main() {
  // read inputs
  long N, M;
  vector<long> xs, ys;
  scanf("%ld %ld", &N, &M);
  for (long i = 0; i < N - 1; i++) {
    long x, y;
    scanf("%ld %ld", &x, &y);
    x--;  // NOTE : modified input
    y--;  // NOTE : modified input
    xs.push_back(x);
    ys.push_back(y);
  }

  // create tree
  vector<vector<long>> adj_lists(N, vector<long>());
  for (long i = 0; i < N - 1; i++) {
    const long x = xs[i], y = ys[i];
    adj_lists[x].push_back(y);
    adj_lists[y].push_back(x);
  }

  // solve dp
  vector<long> dp1s(N, -1), dp2s(N, -1);
  // calculate dp1s
  vector<long> us(N, 0);
  dfs1(adj_lists, M, dp1s, 0, us);
  //   for (long i = 0; i < N; i++) {
  //     printf("i = %ld, dp1s[i] = %ld\n", i, dp1s[i]);
  //   }

  // calculate dp2s
  for (long i = 0; i < N; i++) {
    us[i] = 0;
  }
  dfs2(adj_lists, M, dp1s, dp2s, 0, us);
  //   for (long i = 0; i < N; i++) {
  //     printf("i = %ld, dp2s[i] = %ld\n", i, dp2s[i]);
  //   }

  // print answer
  for (long i = 0; i < N; i++) {
    printf("%ld\n", dp2s[i]);
  }
}

Compiler output:
a.cc: In function 'void dfs1(const std::vector<std::vector<long int> >&, long int, std::vector<long int>&, long int, std::vector<long int>&)':
a.cc:15:50: error: invalid operands of types 'void' and 'int' to binary 'operator+'
   15 |       dp = dp * (dfs1(adj_lists, M, dp1s, j, us) + 1) % M;
      |                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
      |                      |                             |
      |                      void                          int