Problem p02822 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "-1"                                                               \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
#define PI (acos(-1))
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------
ll modpow(ll a, ll n = MOD - 2, ll mod = MOD) { // a^n % MOD の計算
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
vector<ll> bubunode[100010];
struct edge {
  ll to;
};
struct graph {
  ll N;
  vector<vector<edge>> G;
  vector<bool> used;
  // vector<vector<ll>> bubunode;

  graph(ll n) : N(n), G(N), used(N, false) {}

  void add_edge(ll s, ll t) {
    G[s].emplace_back((edge){t});
    G[t].emplace_back((edge){s}); // 無向ならON
  }

  ll dfs(ll v = 0) {
    used[v] = true;
    ll ct = 1;
    for (auto E : G[v]) {
      if (!used[E.to]) {
        ll x = dfs(E.to);
        bubunode[v].emplace_back(x);
        ct += x;
      }
    }
    ll y = N - ct;
    if (y > 0)
      bubunode[v].emplace_back(y);
    return ct;
  }
};

//--MAIN-----------------------------------
void Main() {
  ll N;
  cin >> N;
  graph g(N);
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    g.add_edge(--a, --b);
  }
  g.dfs();
  ll ans = 0;
  ll ans3 = modpow(2, N - 1) - 1;
  rep(i, N) {
    auto &ve = bubunode[i];
    if (ve.size() <= 1)
      continue;
    ll ans2 = ans3;
    for (auto &x : ve) {
      ans2 -= modpow(2, x) - 1;
      ans2 += MOD;
      ans2 %= MOD;
    }
    ans += ans2;
    ans %= MOD;
  }
  ll gya = modpow(2, N);
  gya = modpow(gya);
  ans *= gya;
  ans %= MOD;
  cout << ans << "\n";
}

//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------


Fixed submission:
#include <bits/stdc++.h>
#define SORT(x) sort((x).begin(), (x).end())
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define reps(i, m, n) for (ll i = m; i < n; i++)
#define repr(i, m, n) for (ll i = m; i >= n; i--)
#define de(x) cout << #x << "=" << x << endl;
template <class T> bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define dame                                                                   \
  {                                                                            \
    cout << "-1"                                                               \
         << "\n";                                                              \
    return;                                                                    \
  }
#define INF2 1000000000000000037
#define INF 1000000007
#define MOD 1000000007
#define PI (acos(-1))
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//--GLOBAL---------------------------------
ll modpow(ll a, ll n = MOD - 2, ll mod = MOD) { // a^n % MOD の計算
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
vector<ll> bubunode[200010];
struct edge {
  ll to;
};
struct graph {
  ll N;
  vector<vector<edge>> G;
  vector<bool> used;
  // vector<vector<ll>> bubunode;

  graph(ll n) : N(n), G(N), used(N, false) {}

  void add_edge(ll s, ll t) {
    G[s].emplace_back((edge){t});
    G[t].emplace_back((edge){s}); // 無向ならON
  }

  ll dfs(ll v = 0) {
    used[v] = true;
    ll ct = 1;
    for (auto E : G[v]) {
      if (!used[E.to]) {
        ll x = dfs(E.to);
        bubunode[v].emplace_back(x);
        ct += x;
      }
    }
    ll y = N - ct;
    if (y > 0)
      bubunode[v].emplace_back(y);
    return ct;
  }
};

//--MAIN-----------------------------------
void Main() {
  ll N;
  cin >> N;
  graph g(N);
  rep(i, N - 1) {
    ll a, b;
    cin >> a >> b;
    g.add_edge(--a, --b);
  }
  g.dfs();
  ll ans = 0;
  ll ans3 = modpow(2, N - 1) - 1;
  rep(i, N) {
    auto &ve = bubunode[i];
    if (ve.size() <= 1)
      continue;
    ll ans2 = ans3;
    for (auto &x : ve) {
      ans2 -= modpow(2, x) - 1;
      ans2 += MOD;
      ans2 %= MOD;
    }
    ans += ans2;
    ans %= MOD;
  }
  ll gya = modpow(2, N);
  gya = modpow(gya);
  ans *= gya;
  ans %= MOD;
  cout << ans << "\n";
}

//--START----------------------------------
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
//-----------------------------------------