Problem p02822 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define f first
#define s second
#define pb push_back
#define reset(a, b) memset(a, b, sizeof a);
#define ALL(x) x.begin(), x.end()

template <typename T> void cetak(T t) { cout << t << ')' << endl; }
template <typename T, typename... V> void cetak(T t, V... v) {
  cout << t;
  if (sizeof...(v))
    cout << ", ";
  cetak(v...);
}

#define debug(x...)                                                            \
  cout << '(' << #x << ") = (";                                                \
  cetak(x)
#define sz(x) (int)(x.size())
#define lt(x) (int)(x.length())

const int MOD = 1e9 + 7;

int add(int x, int y) {
  int ret = x + y;
  if (ret >= MOD)
    ret -= MOD;
  return ret;
}
int sub(int x, int y) {
  int ret = x - y;
  if (ret < 0)
    ret += MOD;
  return ret;
}
int mul(int x, int y) { return (long long)x * y % MOD; }

const int mx = 1e5 + 10;

int n, sz[mx];
vector<int> g[mx];

int POW(int a, int b) {
  int ret = 1;
  while (b > 0) {
    if (b & 1)
      ret = mul(ret, a);
    b >>= 1;
    a = mul(a, a);
  }
  return ret;
}

int ans;

void dfs(int now, int par) {
  sz[now] = 1;
  for (int i : g[now]) {
    if (i != par) {
      dfs(i, now);
      sz[now] += sz[i];
    }
  }
  vector<int> subtree;
  int gak_masuk = 1; // semua nya putih
  if (n - sz[now] > 0)
    subtree.pb(n - sz[now]);
  for (int i : g[now]) {
    if (i != par)
      subtree.pb(sz[i]);
  }
  for (int i : subtree) {
    int hitam = sub(POW(2, i), 1);
    gak_masuk = add(gak_masuk, hitam);
  }
  int total = POW(2, n - 1);
  total = sub(total, gak_masuk);
  ans = add(ans, total);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1, 0);
  ans = mul(ans, POW(POW(2, n), MOD - 2));
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define f first
#define s second
#define pb push_back
#define reset(a, b) memset(a, b, sizeof a);
#define ALL(x) x.begin(), x.end()

template <typename T> void cetak(T t) { cout << t << ')' << endl; }
template <typename T, typename... V> void cetak(T t, V... v) {
  cout << t;
  if (sizeof...(v))
    cout << ", ";
  cetak(v...);
}

#define debug(x...)                                                            \
  cout << '(' << #x << ") = (";                                                \
  cetak(x)
#define sz(x) (int)(x.size())
#define lt(x) (int)(x.length())

const int MOD = 1e9 + 7;

int add(int x, int y) {
  int ret = x + y;
  if (ret >= MOD)
    ret -= MOD;
  return ret;
}
int sub(int x, int y) {
  int ret = x - y;
  if (ret < 0)
    ret += MOD;
  return ret;
}
int mul(int x, int y) { return (long long)x * y % MOD; }

const int mx = 2e5 + 10;

int n, sz[mx];
vector<int> g[mx];

int POW(int a, int b) {
  int ret = 1;
  while (b > 0) {
    if (b & 1)
      ret = mul(ret, a);
    b >>= 1;
    a = mul(a, a);
  }
  return ret;
}

int ans;

void dfs(int now, int par) {
  sz[now] = 1;
  for (int i : g[now]) {
    if (i != par) {
      dfs(i, now);
      sz[now] += sz[i];
    }
  }
  vector<int> subtree;
  int gak_masuk = 1; // semua nya putih
  if (n - sz[now] > 0)
    subtree.pb(n - sz[now]);
  for (int i : g[now]) {
    if (i != par)
      subtree.pb(sz[i]);
  }
  for (int i : subtree) {
    int hitam = sub(POW(2, i), 1);
    gak_masuk = add(gak_masuk, hitam);
  }
  int total = POW(2, n - 1);
  total = sub(total, gak_masuk);
  ans = add(ans, total);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  dfs(1, 0);
  ans = mul(ans, POW(POW(2, n), MOD - 2));
  cout << ans << endl;
}