Problem p02822 (C++)

Buggy submission (status: Runtime Error):
// Words are flowing out like endless rain into a paper cup
// They slither while they pass they slip away across the universe
// Pools of sorrow, waves of joy are drifting through my open mind
// Possessing and caressing me

#include <bits/stdc++.h>
using namespace std;

using LL = long long;

namespace _buff {

const size_t BUFF = 1 << 19;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
  if (ib == ie) {
    ib = ibuf;
    ie = ibuf + fread(ibuf, 1, BUFF, stdin);
  }
  return ib == ie ? -1 : *ib++;
}

} // namespace _buff

LL read() {
  using namespace _buff;
  LL ret = 0;
  bool pos = true;
  char c = getc();
  for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
    assert(~c);
  }
  if (c == '-') {
    pos = false;
    c = getc();
  }
  for (; c >= '0' && c <= '9'; c = getc()) {
    ret = (ret << 3) + (ret << 1) + (c ^ 48);
  }
  return pos ? ret : -ret;
}

const size_t N = 1e5 + 5;
const int MOD = 1e9 + 7;

int qpow(int base, int e) {
  int ret = 1;
  for (; e; e >>= 1) {
    if (e & 1) {
      ret = (LL)ret * base % MOD;
    }
    base = (LL)base * base % MOD;
  }
  return ret;
}

void iadd(int &dp, const int &val) {
  dp += val;
  if (dp >= MOD) {
    dp -= MOD;
  }
}

void ladd(int &dp, const LL &val) { dp = (dp + val) % MOD; }

int n, ans;
vector<int> g[N];
int siz[N];

int dfs(int u = 1, int p = 0) {
  vector<int> vc;
  siz[u] = 1;
  for (int v : g[u]) {
    if (v != p) {
      vc.emplace_back(dfs(v, u));
      siz[u] += siz[v];
    }
  }
  vc.emplace_back(n - siz[u]);
  int y = 0, z = n;
  for (int x : vc) {
    z -= x;
    ladd(ans, (LL)(MOD + 1 - qpow(2, MOD - 1 - x)) *
                  (MOD + 1 - qpow(2, MOD - 1 - y)) % MOD *
                  qpow(2, MOD - 1 - z));
    y += x;
  }
  return siz[u];
}

int main() {
  n = read();
  for (int i = 1; i < n; ++i) {
    int a = read(), b = read();
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  dfs();
  cout << ans;
  return 0;
}


Fixed submission:
// Words are flowing out like endless rain into a paper cup
// They slither while they pass they slip away across the universe
// Pools of sorrow, waves of joy are drifting through my open mind
// Possessing and caressing me

#include <bits/stdc++.h>
using namespace std;

using LL = long long;

namespace _buff {

const size_t BUFF = 1 << 19;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
  if (ib == ie) {
    ib = ibuf;
    ie = ibuf + fread(ibuf, 1, BUFF, stdin);
  }
  return ib == ie ? -1 : *ib++;
}

} // namespace _buff

LL read() {
  using namespace _buff;
  LL ret = 0;
  bool pos = true;
  char c = getc();
  for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
    assert(~c);
  }
  if (c == '-') {
    pos = false;
    c = getc();
  }
  for (; c >= '0' && c <= '9'; c = getc()) {
    ret = (ret << 3) + (ret << 1) + (c ^ 48);
  }
  return pos ? ret : -ret;
}

const size_t N = 2e5 + 5;
const int MOD = 1e9 + 7;

int qpow(int base, int e) {
  int ret = 1;
  for (; e; e >>= 1) {
    if (e & 1) {
      ret = (LL)ret * base % MOD;
    }
    base = (LL)base * base % MOD;
  }
  return ret;
}

void iadd(int &dp, const int &val) {
  dp += val;
  if (dp >= MOD) {
    dp -= MOD;
  }
}

void ladd(int &dp, const LL &val) { dp = (dp + val) % MOD; }

int n, ans;
vector<int> g[N];
int siz[N];

int dfs(int u = 1, int p = 0) {
  vector<int> vc;
  siz[u] = 1;
  for (int v : g[u]) {
    if (v != p) {
      vc.emplace_back(dfs(v, u));
      siz[u] += siz[v];
    }
  }
  vc.emplace_back(n - siz[u]);
  int y = 0, z = n;
  for (int x : vc) {
    z -= x;
    ladd(ans, (LL)(MOD + 1 - qpow(2, MOD - 1 - x)) *
                  (MOD + 1 - qpow(2, MOD - 1 - y)) % MOD *
                  qpow(2, MOD - 1 - z));
    y += x;
  }
  return siz[u];
}

int main() {
  n = read();
  for (int i = 1; i < n; ++i) {
    int a = read(), b = read();
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  dfs();
  cout << ans;
  return 0;
}