Problem p02822 (C++)

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

class ModInt {
public:
  static int _BM;
  long long int m_value;
  ModInt() { m_value = 0; }

  ModInt(int v) { m_value = (long long int)v; }

  ModInt(const ModInt &t) { m_value = t.getint(); }

  int operator+(const ModInt &t) {
    return ModInt::modadd(getint(), t.getint());
  }

  int operator+(int t) { return ModInt::modadd(getint(), t); }

  int operator-(const ModInt &t) {
    return ModInt::modminus(getint(), t.getint());
  }
  int operator-(int t) { return ModInt::modminus(getint(), t); }

  int operator*(const ModInt &t) {
    return ModInt::modmul(getint(), t.getint());
  }
  int operator*(int t) { return ModInt::modmul(getint(), t); }

  ModInt &operator+=(const ModInt &t) {
    (*this) += t.getint();
    return (*this);
  }

  ModInt &operator*=(const ModInt &t) {
    (*this) *= t.getint();
    return (*this);
  }

  ModInt &operator-=(const ModInt &t) {
    (*this) -= t.getint();
    return (*this);
  }

  ModInt &operator+=(int v) {
    m_value += v;
    if (m_value >= _BM)
      m_value -= _BM;
    return *this;
  }

  ModInt &operator-=(int v) {
    m_value -= v;
    if (m_value < 0)
      m_value += _BM;
    return *this;
  }

  ModInt &operator*=(int v) {
    m_value *= v;
    m_value %= _BM;
    return *this;
  }

  static int modadd(int a, int b) {
    ModInt t(a);
    t += b;
    return t.getint();
  }

  static int modminus(int a, int b) {
    ModInt t(a);
    t -= b;
    return t.getint();
  }

  static int modmul(int a, int b) {
    ModInt t(a);
    t *= b;
    return t.getint();
  }

  // a^b;
  static int modpow(int a, int b) {
    ModInt ans(1);
    int heap[32];
    heap[0] = a;
    for (int bit = 0; b >= (1 << bit); bit++) {
      if (bit > 0)
        heap[bit] = modmul(heap[bit - 1], heap[bit - 1]);
      if (b & (1 << bit)) {
        ans *= heap[bit];
      }
    }
    return ans.getint();
  }

  static int reciprocal(int a) { return modpow(a, _BM - 2); }

  static int combination(int n, int r) {
    int *fac = (int *)malloc(sizeof(int) * (n + 1));
    int *facinv = (int *)malloc(sizeof(int) * (n + 1));
    fac[0] = 1;
    for (int k = 1; k <= n; k++)
      fac[k] = modmul(k, fac[k - 1]);
    facinv[n] = reciprocal(fac[n]);
    for (int k = n; k > 0; k--)
      facinv[k - 1] = modmul(k, facinv[k]);

    ModInt ans(fac[n]);
    ans *= facinv[r];
    ans *= facinv[n - r];

    free(fac);
    free(facinv);
    return ans.getint();
  }

  int getint() const { return (int)m_value; }

private:
  void _mod() {
    if (m_value < 0)
      m_value %= _BM;
  }
};

int ModInt::_BM = 1000000007;

typedef struct {
  int parent;
  vector<int> children;
  int childtotal;
} NODE;

NODE nodes[20000];
vector<int> connection[20000];

int countnode(int index, int parent) {
  int count = 0;
  nodes[index].parent = parent;

  for (int j = 0; j < (int)connection[index].size(); j++) {
    if (connection[index][j] != parent) {
      int cc = countnode(connection[index][j], index);
      nodes[index].children.push_back(connection[index][j]);
      count += cc;
    }
  }
  nodes[index].childtotal = count + 1;
  return count + 1;
}

int checktree(int index, int total) {
  ModInt ans = 0;

  for (int i = 0; i < (int)nodes[index].children.size(); i++) {
    ans += checktree(nodes[index].children[i], total);
  }

  ModInt p0 = ModInt::reciprocal(ModInt::modpow(2, total - 1));
  ModInt ng = p0;
  if ((int)nodes[index].children.size() == 0 ||
      ((int)nodes[index].children.size() == 1 && nodes[index].parent < 0))
    return ans.getint();
  for (int i = 0; i < (int)nodes[index].children.size(); i++) {
    ModInt p1 = 1;
    p1 -= ModInt::reciprocal(
        ModInt::modpow(2, nodes[nodes[index].children[i]].childtotal));
    p1 *= ModInt::reciprocal(ModInt::modpow(
        2, total - 1 - nodes[nodes[index].children[i]].childtotal));

    ng += p1;
  }
  if (nodes[index].parent >= 0) {
    ModInt p1 =
        ModInt::reciprocal(ModInt::modpow(2, nodes[index].childtotal - 1));
    p1 -= p0;
    ng += p1;
  }
  ModInt ok = 1;
  ok -= ng;

  ok *= ModInt::reciprocal(2);
  ans += ok;
  return ans.getint();
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a;
    cin >> b;
    a--;
    b--;
    connection[a].push_back(b);
    connection[b].push_back(a);
  }

  countnode(0, -1);
  cout << checktree(0, n);
}

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

class ModInt {
public:
  static int _BM;
  long long int m_value;
  ModInt() { m_value = 0; }

  ModInt(int v) { m_value = (long long int)v; }

  ModInt(const ModInt &t) { m_value = t.getint(); }

  int operator+(const ModInt &t) {
    return ModInt::modadd(getint(), t.getint());
  }

  int operator+(int t) { return ModInt::modadd(getint(), t); }

  int operator-(const ModInt &t) {
    return ModInt::modminus(getint(), t.getint());
  }
  int operator-(int t) { return ModInt::modminus(getint(), t); }

  int operator*(const ModInt &t) {
    return ModInt::modmul(getint(), t.getint());
  }
  int operator*(int t) { return ModInt::modmul(getint(), t); }

  ModInt &operator+=(const ModInt &t) {
    (*this) += t.getint();
    return (*this);
  }

  ModInt &operator*=(const ModInt &t) {
    (*this) *= t.getint();
    return (*this);
  }

  ModInt &operator-=(const ModInt &t) {
    (*this) -= t.getint();
    return (*this);
  }

  ModInt &operator+=(int v) {
    m_value += v;
    if (m_value >= _BM)
      m_value -= _BM;
    return *this;
  }

  ModInt &operator-=(int v) {
    m_value -= v;
    if (m_value < 0)
      m_value += _BM;
    return *this;
  }

  ModInt &operator*=(int v) {
    m_value *= v;
    m_value %= _BM;
    return *this;
  }

  static int modadd(int a, int b) {
    ModInt t(a);
    t += b;
    return t.getint();
  }

  static int modminus(int a, int b) {
    ModInt t(a);
    t -= b;
    return t.getint();
  }

  static int modmul(int a, int b) {
    ModInt t(a);
    t *= b;
    return t.getint();
  }

  // a^b;
  static int modpow(int a, int b) {
    ModInt ans(1);
    int heap[32];
    heap[0] = a;
    for (int bit = 0; b >= (1 << bit); bit++) {
      if (bit > 0)
        heap[bit] = modmul(heap[bit - 1], heap[bit - 1]);
      if (b & (1 << bit)) {
        ans *= heap[bit];
      }
    }
    return ans.getint();
  }

  static int reciprocal(int a) { return modpow(a, _BM - 2); }

  static int combination(int n, int r) {
    int *fac = (int *)malloc(sizeof(int) * (n + 1));
    int *facinv = (int *)malloc(sizeof(int) * (n + 1));
    fac[0] = 1;
    for (int k = 1; k <= n; k++)
      fac[k] = modmul(k, fac[k - 1]);
    facinv[n] = reciprocal(fac[n]);
    for (int k = n; k > 0; k--)
      facinv[k - 1] = modmul(k, facinv[k]);

    ModInt ans(fac[n]);
    ans *= facinv[r];
    ans *= facinv[n - r];

    free(fac);
    free(facinv);
    return ans.getint();
  }

  int getint() const { return (int)m_value; }

private:
  void _mod() {
    if (m_value < 0)
      m_value %= _BM;
  }
};

int ModInt::_BM = 1000000007;

typedef struct {
  int parent;
  vector<int> children;
  int childtotal;
} NODE;

NODE nodes[200004];
vector<int> connection[200004];

int countnode(int index, int parent) {
  int count = 0;
  nodes[index].parent = parent;

  for (int j = 0; j < (int)connection[index].size(); j++) {
    if (connection[index][j] != parent) {
      int cc = countnode(connection[index][j], index);
      nodes[index].children.push_back(connection[index][j]);
      count += cc;
    }
  }
  nodes[index].childtotal = count + 1;
  return count + 1;
}

int checktree(int index, int total) {
  ModInt ans = 0;

  for (int i = 0; i < (int)nodes[index].children.size(); i++) {
    ans += checktree(nodes[index].children[i], total);
  }

  ModInt p0 = ModInt::reciprocal(ModInt::modpow(2, total - 1));
  ModInt ng = p0;
  if ((int)nodes[index].children.size() == 0 ||
      ((int)nodes[index].children.size() == 1 && nodes[index].parent < 0))
    return ans.getint();
  for (int i = 0; i < (int)nodes[index].children.size(); i++) {
    ModInt p1 = 1;
    p1 -= ModInt::reciprocal(
        ModInt::modpow(2, nodes[nodes[index].children[i]].childtotal));
    p1 *= ModInt::reciprocal(ModInt::modpow(
        2, total - 1 - nodes[nodes[index].children[i]].childtotal));

    ng += p1;
  }
  if (nodes[index].parent >= 0) {
    ModInt p1 =
        ModInt::reciprocal(ModInt::modpow(2, nodes[index].childtotal - 1));
    p1 -= p0;
    ng += p1;
  }
  ModInt ok = 1;
  ok -= ng;

  ok *= ModInt::reciprocal(2);
  ans += ok;
  return ans.getint();
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a;
    cin >> b;
    a--;
    b--;
    connection[a].push_back(b);
    connection[b].push_back(a);
  }

  countnode(0, -1);
  cout << checktree(0, n);
}