Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

struct ModInt {
  using M = ModInt;
  long long v;
  ModInt(long long _v = 0) : v(_v % MOD + MOD) { norm(); }
  M &norm() {
    v = (v < MOD) ? v : v - MOD;
    return *this;
  }
  M operator+(const M &x) const { return M(v + x.v); }
  M operator-(const M &x) const { return M(v + MOD - x.v); }
  M operator*(const M &x) const { return M(v * x.v % MOD); }
  M operator/(const M &x) const { return M(v * x.inv().v); }
  M &operator+=(const M &x) { return *this = *this + x; }
  M &operator-=(const M &x) { return *this = *this - x; }
  M &operator*=(const M &x) { return *this = *this * x; }
  M &operator/=(const M &x) { return *this = *this / x; }
  friend istream &operator>>(istream &input, M &x) {
    return input >> x.v, x.norm(), input;
  }
  friend ostream &operator<<(ostream &output, const M &x) {
    return output << x.v;
  }
  M pow(long long n) const {
    M x(v), res(1);
    while (n) {
      if (n & 1)
        res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  M inv() const { return this->pow(MOD - 2); }

  static long long MOD;
  static vector<M> fact, finv;
  static void build(int n) {
    fact.assign(n + 1, 1);
    finv.assign(n + 1, 1);
    for (int i = 1; i < n + 1; i++)
      fact[i] = fact[i - 1] * M(i);
    for (int i = 0; i < n + 1; i++)
      finv[i] = fact[i].inv();
  }
  static M comb(int n, int k) {
    if (n < k || k < 0)
      return M(0);
    return fact[n] * finv[n - k] * finv[k];
  }
  static M extgcd(int a, int b, int *x, int *y) {
    M d(a);
    if (b) {
      d = extgcd(b, a % b, y, x);
      *y -= (a / b) * *x;
    } else {
      *x = 1, *y = 0;
    }
    return d;
  }
};
vector<ModInt> ModInt::fact = vector<ModInt>();
vector<ModInt> ModInt::finv = vector<ModInt>();
long long ModInt::MOD = 1e9 + 7;

const int maxn = 11;
ModInt dp[maxn][2];

int n;
vector<vector<int>> graph;
vector<bool> visited;

/*
木DP。
*/
void dfs(int i) {
  visited[i] = true;
  dp[i][0] = 1;
  dp[i][1] = 1;
  for (int j : graph[i]) {
    if (visited[j])
      continue;
    dfs(j);
    dp[i][0] *= dp[j][0] + dp[j][1];
    dp[i][1] *= dp[j][0];
  }
  return;
}

int main() {
  cin >> n;
  graph.resize(n);
  visited.resize(n);
  rep(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  dfs(0);
  cout << dp[0][0] + dp[0][1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

struct ModInt {
  using M = ModInt;
  long long v;
  ModInt(long long _v = 0) : v(_v % MOD + MOD) { norm(); }
  M &norm() {
    v = (v < MOD) ? v : v - MOD;
    return *this;
  }
  M operator+(const M &x) const { return M(v + x.v); }
  M operator-(const M &x) const { return M(v + MOD - x.v); }
  M operator*(const M &x) const { return M(v * x.v % MOD); }
  M operator/(const M &x) const { return M(v * x.inv().v); }
  M &operator+=(const M &x) { return *this = *this + x; }
  M &operator-=(const M &x) { return *this = *this - x; }
  M &operator*=(const M &x) { return *this = *this * x; }
  M &operator/=(const M &x) { return *this = *this / x; }
  friend istream &operator>>(istream &input, M &x) {
    return input >> x.v, x.norm(), input;
  }
  friend ostream &operator<<(ostream &output, const M &x) {
    return output << x.v;
  }
  M pow(long long n) const {
    M x(v), res(1);
    while (n) {
      if (n & 1)
        res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  M inv() const { return this->pow(MOD - 2); }

  static long long MOD;
  static vector<M> fact, finv;
  static void build(int n) {
    fact.assign(n + 1, 1);
    finv.assign(n + 1, 1);
    for (int i = 1; i < n + 1; i++)
      fact[i] = fact[i - 1] * M(i);
    for (int i = 0; i < n + 1; i++)
      finv[i] = fact[i].inv();
  }
  static M comb(int n, int k) {
    if (n < k || k < 0)
      return M(0);
    return fact[n] * finv[n - k] * finv[k];
  }
  static M extgcd(int a, int b, int *x, int *y) {
    M d(a);
    if (b) {
      d = extgcd(b, a % b, y, x);
      *y -= (a / b) * *x;
    } else {
      *x = 1, *y = 0;
    }
    return d;
  }
};
vector<ModInt> ModInt::fact = vector<ModInt>();
vector<ModInt> ModInt::finv = vector<ModInt>();
long long ModInt::MOD = 1e9 + 7;

const int maxn = 100001;
ModInt dp[maxn][2];

int n;
vector<vector<int>> graph;
vector<bool> visited;

/*
木DP。
*/
void dfs(int i) {
  visited[i] = true;
  dp[i][0] = 1;
  dp[i][1] = 1;
  for (int j : graph[i]) {
    if (visited[j])
      continue;
    dfs(j);
    dp[i][0] *= dp[j][0] + dp[j][1];
    dp[i][1] *= dp[j][0];
  }
  return;
}

int main() {
  cin >> n;
  graph.resize(n);
  visited.resize(n);
  rep(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  dfs(0);
  cout << dp[0][0] + dp[0][1] << endl;
  return 0;
}