Problem p02822 (C++)

Buggy submission (status: Runtime Error):
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
ll dist[100010] = {};
vector<ll> tree[200010];

void dfs(ll cur, ll par) {
  dist[cur] = 1;
  for (auto &e : tree[cur]) {
    if (e == par)
      continue;
    dfs(e, cur);
    dist[cur] += dist[e];
  }
}

ll ans = 0;
ll N;

ll power(ll x, ll n) {
  if (n == 0)
    return 1LL;
  ll res = power(x * x % MOD, n / 2);
  if (n % 2 == 1)
    res = res * x % MOD;
  return res;
}

void dfs2(ll cur, ll par) {
  if (tree[cur].size() > 1) {
    ll res = power(2, N - 1);
    for (auto &e : tree[cur]) {
      res -= power(2, dist[e]) - 1;
      res = (res + MOD) % MOD;
    }
    res = (res - 1 + MOD) % MOD;
    ans += res;
    ans %= MOD;
  }
  for (auto &e : tree[cur]) {
    if (e == par)
      continue;
    ll tmp = dist[e];
    dist[cur] = dist[cur] - dist[e];
    dist[e] = N;
    dfs2(e, cur);
    dist[e] = tmp;
    dist[cur] = N;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  ll A[200010], B[200010];

  rep(i, 0, N - 1) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    tree[A[i]].push_back(B[i]);
    tree[B[i]].push_back(A[i]);
  }
  dfs(0, -1);
  dfs2(0, -1);
  ll v = power(2, N);
  ans *= power(v, MOD - 2);
  ans %= MOD;
  print(ans);
}

Fixed submission:
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
ll dist[200010] = {};
vector<ll> tree[200010];

void dfs(ll cur, ll par) {
  dist[cur] = 1;
  for (auto &e : tree[cur]) {
    if (e == par)
      continue;
    dfs(e, cur);
    dist[cur] += dist[e];
  }
}

ll ans = 0;
ll N;

ll power(ll x, ll n) {
  if (n == 0)
    return 1LL;
  ll res = power(x * x % MOD, n / 2);
  if (n % 2 == 1)
    res = res * x % MOD;
  return res;
}

void dfs2(ll cur, ll par) {
  if (tree[cur].size() > 1) {
    ll res = power(2, N - 1);
    for (auto &e : tree[cur]) {
      res -= power(2, dist[e]) - 1;
      res = (res + MOD) % MOD;
    }
    res = (res - 1 + MOD) % MOD;
    ans += res;
    ans %= MOD;
  }
  for (auto &e : tree[cur]) {
    if (e == par)
      continue;
    ll tmp = dist[e];
    dist[cur] = dist[cur] - dist[e];
    dist[e] = N;
    dfs2(e, cur);
    dist[e] = tmp;
    dist[cur] = N;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  ll A[200010], B[200010];

  rep(i, 0, N - 1) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
    tree[A[i]].push_back(B[i]);
    tree[B[i]].push_back(A[i]);
  }
  dfs(0, -1);
  dfs2(0, -1);
  ll v = power(2, N);
  ans *= power(v, MOD - 2);
  ans %= MOD;
  print(ans);
}