Problem p02822 (C++)

Buggy submission (status: Runtime Error):
//----------------------------templates
#pragma GCC optimize("Ofast")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
//----------------------------
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define int ll

#define FOR(i, j, n) for (int i = (int)(j); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPS(i, n) for (int i = 1; i <= (int)(n); i++)
#define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REPNS(i, n) for (int i = (int)(n); i > 0; i--)

#define I(n) scanf("%lld", &(n))
#define LL(n) scanf("%lld", &(n))
#define pb(n) push_back((n))
#define mp(i, j) make_pair((i), (j))
#define eb(i, j) emplace_back((i), (j))
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())

#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vvi> vvvi;

const int mod = 1000000007;

//--------------------------------------------

ll powll(ll base, ll power) {
  ll ans = 1;
  while (power) {
    if (power & 1)
      ans = (base * ans) % mod;
    base = (base * base) % mod;
    power >>= 1;
  }
  return ans;
}

int n, a, b, ret;
vvi children;
bool visited[200010];

int f(int x) {
  visited[x] = true;
  if (sz(children[x]) == 1)
    return 1;
  vi nodes;
  for (auto v : children[x]) {
    if (visited[v])
      continue;
    nodes.pb(f(v));
  }
  int subtree = accumulate(all(nodes), 0LL) + 1;
  if (n - subtree)
    nodes.pb(n - subtree);

  // ^ -x = ^ mod-1-x
  int zerotree = powll(2, mod - n);
  int onetree = 0;
  for (auto nd : nodes) {
    int lit = 1 - powll(2, mod - 1 - nd) + mod;
    int dim = powll(2, mod - (n - nd));
    onetree = (onetree + lit * dim) % mod;
  }
  ret = (ret + (1 - zerotree - onetree + 2 * mod) * powll(2, mod - 2)) % mod;

  return subtree;
}

signed main() {
  I(n);
  children.resize(n);
  REP(i, n - 1) {
    I(a);
    I(b);
    a--;
    b--;
    children[a].pb(b);
    children[b].pb(a);
  }
  int st = 0;
  while (sz(children[st]) == 1)
    st++;
  cerr << st << endl;
  f(st);
  cout << ret << endl;
}

Fixed submission:
//----------------------------templates
#pragma GCC optimize("Ofast")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
//----------------------------
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define int ll

#define FOR(i, j, n) for (int i = (int)(j); i < (n); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPS(i, n) for (int i = 1; i <= (int)(n); i++)
#define REPN(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define REPNS(i, n) for (int i = (int)(n); i > 0; i--)

#define I(n) scanf("%lld", &(n))
#define LL(n) scanf("%lld", &(n))
#define pb(n) push_back((n))
#define mp(i, j) make_pair((i), (j))
#define eb(i, j) emplace_back((i), (j))
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define uniq(v) v.erase(unique(v.begin(), v.end()), v.end())

#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
typedef vector<vpi> vvpi;
typedef vector<vvi> vvvi;

const int mod = 1000000007;

//--------------------------------------------

ll powll(ll base, ll power) {
  ll ans = 1;
  while (power) {
    if (power & 1)
      ans = (base * ans) % mod;
    base = (base * base) % mod;
    power >>= 1;
  }
  return ans;
}

int n, a, b, ret;
vvi children;
bool visited[200010];

int f(int x) {
  visited[x] = true;
  if (sz(children[x]) == 1)
    return 1;
  vi nodes;
  for (auto v : children[x]) {
    if (visited[v])
      continue;
    nodes.pb(f(v));
  }
  int subtree = accumulate(all(nodes), 0LL) + 1;
  if (n - subtree)
    nodes.pb(n - subtree);

  // ^ -x = ^ mod-1-x
  int zerotree = powll(2, mod - n);
  int onetree = 0;
  for (auto nd : nodes) {
    int lit = 1 - powll(2, mod - 1 - nd) + mod;
    int dim = powll(2, mod - (n - nd));
    onetree = (onetree + lit * dim) % mod;
  }
  ret = (ret + (1 - zerotree - onetree + 2 * mod) * powll(2, mod - 2)) % mod;

  return subtree;
}

signed main() {
  I(n);
  if (n == 2) {
    cout << 0 << endl;
    return 0;
  }
  children.resize(n);
  REP(i, n - 1) {
    I(a);
    I(b);
    a--;
    b--;
    children[a].pb(b);
    children[b].pb(a);
  }
  int st = 0;
  while (sz(children[st]) == 1)
    st++;
  cerr << st << endl;
  f(st);
  cout << ret << endl;
}