Problem p02822 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, int> pli;
typedef pair<lint, lint> pll;
typedef complex<double> xy_t;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr lint mod = 1e9 + 7;
constexpr lint INF = mod * mod;
constexpr int MAX = 100010;

struct mint {
  using i64 = int_fast64_t;
  i64 a;
  static constexpr i64 mod = 1e9 + 7;
  mint(const i64 a_ = 0) : a(a_) {}
  mint inv() {
    i64 t = 1, n = mod - 2, x = a;
    while (n) {
      if (n & 1)
        (t *= x) %= mod;
      (x *= x) %= mod;
      n >>= 1;
    }
    mint ret(t);
    return ret;
  }
  bool operator==(const mint &x) { return a == x.a; }
  bool operator!=(const mint &x) { return a != x.a; }
  mint operator+(const mint &x) {
    mint ret;
    ret.a = a + x.a;
    if (ret.a > mod)
      ret.a -= mod;
    return ret;
  }
  mint operator-(const mint &x) {
    mint ret;
    ret.a = a - x.a;
    if (ret.a < 0)
      ret.a += mod;
    return ret;
  }
  mint operator*(const mint &x) {
    mint ret;
    ret.a = a * x.a % mod;
    return ret;
  }
  mint operator/(mint &x) {
    mint ret;
    ret.a = a * x.inv().a % mod;
    return ret;
  }
  mint operator^(lint n) {
    mint ret = 1, x = a;
    while (n) {
      if (n & 1)
        (ret.a *= x.a) %= mod;
      (x.a *= x.a) %= mod;
      n >>= 1;
    }
    return ret;
  }
  mint &operator+=(const mint &x) {
    a += x.a;
    if (a >= mod)
      a -= mod;
    return *this;
  }
  mint &operator-=(const mint &x) {
    a -= x.a;
    if (a < 0)
      a += mod;
    return *this;
  }
  mint &operator*=(const mint &x) {
    (a *= x.a) %= mod;
    return *this;
  }
  mint &operator/=(mint &x) {
    (a *= x.inv().a) %= mod;
    return *this;
  }
  mint &operator^=(lint n) {
    lint ret = 1;
    while (n) {
      if (n & 1)
        (ret *= a) %= mod;
      (a *= a) %= mod;
      n >>= 1;
    }
    a = ret;
    return *this;
  }
};

vector<mint> fact;
vector<mint> revfact;

void setfact(int n) {
  fact.resize(n + 1);
  revfact.resize(n + 1);
  fact[0] = 1;
  rep(i, n) fact[i + 1] = fact[i] * mint(i + 1);

  revfact[n] = fact[n].inv();
  for (int i = n - 1; i >= 0; i--)
    revfact[i] = revfact[i + 1] * mint(i + 1);
}

mint getC(int n, int r) {
  if (n < r)
    return 0;
  return fact[n] * revfact[r] * revfact[n - r];
}

int n;
vector<int> G[MAX];
int sz[MAX];
vector<int> sz_child[MAX];
mint ans;

void dfs(int v, int pv) {
  sz[v] = 1;
  mint t = (mint(2) ^ (n - 1)) - 1;
  for (int nv : G[v])
    if (nv != pv) {
      dfs(nv, v);
      sz[v] += sz[nv];
      t -= (mint(2) ^ sz[nv]) - 1;
    }
  t -= (mint(2) ^ (n - sz[v])) - 1;
  ans += t;
}

int main() {
  scanf("%d", &n);
  rep(i, n - 1) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  ans = 0;
  dfs(0, -1);
  mint div = mint(2) ^ n;
  printf("%lld\n", (ans / div).a);
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, int> pli;
typedef pair<lint, lint> pll;
typedef complex<double> xy_t;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr lint mod = 1e9 + 7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

struct mint {
  using i64 = int_fast64_t;
  i64 a;
  static constexpr i64 mod = 1e9 + 7;
  mint(const i64 a_ = 0) : a(a_) {}
  mint inv() {
    i64 t = 1, n = mod - 2, x = a;
    while (n) {
      if (n & 1)
        (t *= x) %= mod;
      (x *= x) %= mod;
      n >>= 1;
    }
    mint ret(t);
    return ret;
  }
  bool operator==(const mint &x) { return a == x.a; }
  bool operator!=(const mint &x) { return a != x.a; }
  mint operator+(const mint &x) {
    mint ret;
    ret.a = a + x.a;
    if (ret.a > mod)
      ret.a -= mod;
    return ret;
  }
  mint operator-(const mint &x) {
    mint ret;
    ret.a = a - x.a;
    if (ret.a < 0)
      ret.a += mod;
    return ret;
  }
  mint operator*(const mint &x) {
    mint ret;
    ret.a = a * x.a % mod;
    return ret;
  }
  mint operator/(mint &x) {
    mint ret;
    ret.a = a * x.inv().a % mod;
    return ret;
  }
  mint operator^(lint n) {
    mint ret = 1, x = a;
    while (n) {
      if (n & 1)
        (ret.a *= x.a) %= mod;
      (x.a *= x.a) %= mod;
      n >>= 1;
    }
    return ret;
  }
  mint &operator+=(const mint &x) {
    a += x.a;
    if (a >= mod)
      a -= mod;
    return *this;
  }
  mint &operator-=(const mint &x) {
    a -= x.a;
    if (a < 0)
      a += mod;
    return *this;
  }
  mint &operator*=(const mint &x) {
    (a *= x.a) %= mod;
    return *this;
  }
  mint &operator/=(mint &x) {
    (a *= x.inv().a) %= mod;
    return *this;
  }
  mint &operator^=(lint n) {
    lint ret = 1;
    while (n) {
      if (n & 1)
        (ret *= a) %= mod;
      (a *= a) %= mod;
      n >>= 1;
    }
    a = ret;
    return *this;
  }
};

vector<mint> fact;
vector<mint> revfact;

void setfact(int n) {
  fact.resize(n + 1);
  revfact.resize(n + 1);
  fact[0] = 1;
  rep(i, n) fact[i + 1] = fact[i] * mint(i + 1);

  revfact[n] = fact[n].inv();
  for (int i = n - 1; i >= 0; i--)
    revfact[i] = revfact[i + 1] * mint(i + 1);
}

mint getC(int n, int r) {
  if (n < r)
    return 0;
  return fact[n] * revfact[r] * revfact[n - r];
}

int n;
vector<int> G[MAX];
int sz[MAX];
vector<int> sz_child[MAX];
mint ans;

void dfs(int v, int pv) {
  sz[v] = 1;
  mint t = (mint(2) ^ (n - 1)) - 1;
  for (int nv : G[v])
    if (nv != pv) {
      dfs(nv, v);
      sz[v] += sz[nv];
      t -= (mint(2) ^ sz[nv]) - 1;
    }
  t -= (mint(2) ^ (n - sz[v])) - 1;
  ans += t;
}

int main() {
  scanf("%d", &n);
  rep(i, n - 1) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  ans = 0;
  dfs(0, -1);
  mint div = mint(2) ^ n;
  printf("%lld\n", (ans / div).a);
}