Problem p02822 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

constexpr int MOD = 1000000007;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

inline int popcount(ll x) { return __builtin_popcountll(x); }
inline int div2num(ll x) { return __builtin_ctzll(x); }
inline bool bit(ll x, int b) { return (x >> b) & 1; }

template <class T> string to_string(T s);
template <class S, class T> string to_string(pair<S, T> p);
string to_string(string s) { return s; }
string to_string(const char s[]) { return to_string(string(s)); }

template <class T> string to_string(T v) {
  if (v.empty())
    return "{}";
  string ret = "{";
  for (auto x : v)
    ret += to_string(x) + ",";
  ret.back() = '}';
  return ret;
}

template <class S, class T> string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + ":" + to_string(p.second) + "}";
}
void debug() { cerr << endl; }

template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}

struct IO {
#ifdef _WIN32
  inline char getchar_unlocked() { return getchar(); }
  inline void putchar_unlocked(char c) { putchar(c); }
#endif
  std::string separator = " ";
  template <class T> inline void read(T &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c != '-' && (c < '0' || '9' < c));
    bool minus = 0;
    if (c == '-') {
      minus = 1;
      c = getchar_unlocked();
    }
    x = 0;
    while ('0' <= c && c <= '9') {
      x *= 10;
      x += c - '0';
      c = getchar_unlocked();
    }
    if (minus)
      x = -x;
  }
  inline void read(std::string &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c == ' ' || c == '\n');
    x.clear();
    do {
      x += c;
      c = getchar_unlocked();
    } while (c != ' ' && c != '\n' && c != EOF);
  }
  template <class T> inline void read(std::vector<T> &v) {
    for (auto &x : v)
      read(x);
  }
  template <class S, class T> inline void read(std::pair<S, T> &p) {
    read(p.first);
    read(p.second);
  }
  template <class Head, class... Tail>
  inline void read(Head &head, Tail &...tail) {
    read(head);
    read(tail...);
  }
  template <class T> inline void write(T x) {
    char buf[32];
    int p = 0;
    if (x < 0) {
      x = -x;
      putchar_unlocked('-');
    }
    if (x == 0)
      putchar_unlocked('0');
    while (x > 0) {
      buf[p++] = (x % 10) + '0';
      x /= 10;
    }
    while (p) {
      putchar_unlocked(buf[--p]);
    }
  }
  inline void write(std::string x) {
    for (char c : x)
      putchar_unlocked(c);
  }
  inline void write(const char s[]) {
    for (int i = 0; s[i] != 0; ++i)
      putchar_unlocked(s[i]);
  }
  template <class T> inline void write(std::vector<T> v) {
    if (v.empty())
      return;
    for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) {
      write(*itr);
      write(separator);
    }
    write(v.back());
  }
  template <class Head, class... Tail>
  inline void write(Head head, Tail... tail) {
    write(head);
    write(separator);
    write(tail...);
  }
  template <class Head, class... Tail>
  inline void writeln(Head head, Tail... tail) {
    write(head, tail...);
    write("\n");
  }
  void set_separator(std::string s) { separator = s; }
} io;

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt<MOD>;

template <class T> struct Rerooting {
  function<T(T, T)> merge;
  function<T(T)> prop;
  T ident;
  vector<vector<int>> G;
  vector<vector<T>> dat, cuml, cumr;
  vector<T> val;
  vector<int> parent_id;
  Rerooting(vector<vector<int>> _G, function<T(T, T)> _merge,
            function<T(T)> _prop, T _ident)
      : G(_G), merge(_merge), prop(_prop), ident(_ident), dat(_G.size()),
        cuml(_G.size()), cumr(_G.size()), parent_id(_G.size()), val(_G.size()) {
  }
  T dfs1(int u, int par) {
    dat[u].resize(G[u].size());
    cuml[u].resize(G[u].size() + 1);
    cumr[u].resize(G[u].size() + 1);
    cuml[u].front() = cumr[u].back() = ident;
    for (int i = 0; i < G[u].size(); ++i) {
      if (G[u][i] == par) {
        parent_id[u] = i;
        continue;
      }
      dat[u][i] = dfs1(G[u][i], u);
      cuml[u][i + 1] = merge(cuml[u][i], dat[u][i]);
    }
    for (int i = (int)G[u].size() - 1; i > parent_id[u]; --i) {
      dat[u][i] = dfs1(G[u][i], u);
      cumr[u][i] = merge(cumr[u][i + 1], dat[u][i]);
    }
    T ret = par == -1 ? ident
                      : merge(cuml[u][parent_id[u]], cumr[u][parent_id[u] + 1]);
    return prop(ret);
  }
  void dfs2(int u, int par) {
    val[u] = cuml[u].back();
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (v != par) {
        dat[v][parent_id[v]] = prop(merge(cuml[u][i], cumr[u][i + 1]));
        for (int j = parent_id[v]; j < G[v].size(); ++j) {
          cuml[v][j + 1] = merge(cuml[v][j], dat[v][j]);
        }
        for (int j = parent_id[v]; j >= 0; --j) {
          cumr[v][j] = merge(cumr[v][j + 1], dat[v][j]);
        }
        dfs2(v, u);
      }
    }
  }
  void solve() {
    dfs1(0, -1);
    dfs2(0, -1);
  }
};

int main() {
  ll N;
  io.read(N);
  vector<vector<int>> G(N);
  REP(i, N - 1) {
    int A, B;
    io.read(A, B);
    G[A - 1].push_back(B - 1);
    G[B - 1].push_back(A - 1);
  }
  Rerooting<ll> rr(
      G, [](ll l, ll r) { return l + r; }, [](ll a) { return a + 1; }, 0);
  rr.solve();
  modint ans = 0;
  vector<modint> pow2(N + 1);
  pow2[0] = 1;
  FOR(i, 1, N + 1) pow2[i] = pow2[i - 1] * 2;
  REP(i, N) {
    modint t = pow2[N - 1] - 1;
    REP(j, G[i].size()) t -= pow2[rr.dat[i][j]] - 1;
    ans += t;
  }
  ans /= pow2[N];
  io.writeln(ans.x);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

constexpr int MOD = 1000000007;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

inline int popcount(ll x) { return __builtin_popcountll(x); }
inline int div2num(ll x) { return __builtin_ctzll(x); }
inline bool bit(ll x, int b) { return (x >> b) & 1; }

template <class T> string to_string(T s);
template <class S, class T> string to_string(pair<S, T> p);
string to_string(string s) { return s; }
string to_string(const char s[]) { return to_string(string(s)); }

template <class T> string to_string(T v) {
  if (v.empty())
    return "{}";
  string ret = "{";
  for (auto x : v)
    ret += to_string(x) + ",";
  ret.back() = '}';
  return ret;
}

template <class S, class T> string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + ":" + to_string(p.second) + "}";
}
void debug() { cerr << endl; }

template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << to_string(head) << " ";
  debug(tail...);
}

struct IO {
#ifdef _WIN32
  inline char getchar_unlocked() { return getchar(); }
  inline void putchar_unlocked(char c) { putchar(c); }
#endif
  std::string separator = " ";
  template <class T> inline void read(T &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c != '-' && (c < '0' || '9' < c));
    bool minus = 0;
    if (c == '-') {
      minus = 1;
      c = getchar_unlocked();
    }
    x = 0;
    while ('0' <= c && c <= '9') {
      x *= 10;
      x += c - '0';
      c = getchar_unlocked();
    }
    if (minus)
      x = -x;
  }
  inline void read(std::string &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c == ' ' || c == '\n');
    x.clear();
    do {
      x += c;
      c = getchar_unlocked();
    } while (c != ' ' && c != '\n' && c != EOF);
  }
  template <class T> inline void read(std::vector<T> &v) {
    for (auto &x : v)
      read(x);
  }
  template <class S, class T> inline void read(std::pair<S, T> &p) {
    read(p.first);
    read(p.second);
  }
  template <class Head, class... Tail>
  inline void read(Head &head, Tail &...tail) {
    read(head);
    read(tail...);
  }
  template <class T> inline void write(T x) {
    char buf[32];
    int p = 0;
    if (x < 0) {
      x = -x;
      putchar_unlocked('-');
    }
    if (x == 0)
      putchar_unlocked('0');
    while (x > 0) {
      buf[p++] = (x % 10) + '0';
      x /= 10;
    }
    while (p) {
      putchar_unlocked(buf[--p]);
    }
  }
  inline void write(std::string x) {
    for (char c : x)
      putchar_unlocked(c);
  }
  inline void write(const char s[]) {
    for (int i = 0; s[i] != 0; ++i)
      putchar_unlocked(s[i]);
  }
  template <class T> inline void write(std::vector<T> v) {
    if (v.empty())
      return;
    for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) {
      write(*itr);
      write(separator);
    }
    write(v.back());
  }
  template <class Head, class... Tail>
  inline void write(Head head, Tail... tail) {
    write(head);
    write(separator);
    write(tail...);
  }
  template <class Head, class... Tail>
  inline void writeln(Head head, Tail... tail) {
    write(head, tail...);
    write("\n");
  }
  void set_separator(std::string s) { separator = s; }
} io;

template <int mod> struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

using modint = ModInt<MOD>;

template <class T> struct Rerooting {
  function<T(T, T)> merge;
  function<T(T)> prop;
  T ident;
  vector<vector<int>> G;
  vector<vector<T>> dat, cuml, cumr;
  vector<T> val;
  vector<int> parent_id;
  Rerooting(vector<vector<int>> _G, function<T(T, T)> _merge,
            function<T(T)> _prop, T _ident)
      : G(_G), merge(_merge), prop(_prop), ident(_ident), dat(_G.size()),
        cuml(_G.size()), cumr(_G.size()), parent_id(_G.size()), val(_G.size()) {
  }
  T dfs1(int u, int par) {
    dat[u].resize(G[u].size());
    cuml[u].resize(G[u].size() + 1);
    cumr[u].resize(G[u].size() + 1);
    cuml[u].front() = cumr[u].back() = ident;
    for (int i = 0; i < G[u].size(); ++i) {
      if (G[u][i] == par) {
        parent_id[u] = i;
        break;
      }
      dat[u][i] = dfs1(G[u][i], u);
      cuml[u][i + 1] = merge(cuml[u][i], dat[u][i]);
    }
    for (int i = (int)G[u].size() - 1; i > parent_id[u]; --i) {
      dat[u][i] = dfs1(G[u][i], u);
      cumr[u][i] = merge(cumr[u][i + 1], dat[u][i]);
    }
    T ret = par == -1 ? ident
                      : merge(cuml[u][parent_id[u]], cumr[u][parent_id[u] + 1]);
    return prop(ret);
  }
  void dfs2(int u, int par) {
    val[u] = cuml[u].back();
    for (int i = 0; i < G[u].size(); ++i) {
      int v = G[u][i];
      if (v != par) {
        dat[v][parent_id[v]] = prop(merge(cuml[u][i], cumr[u][i + 1]));
        for (int j = parent_id[v]; j < G[v].size(); ++j) {
          cuml[v][j + 1] = merge(cuml[v][j], dat[v][j]);
        }
        for (int j = parent_id[v]; j >= 0; --j) {
          cumr[v][j] = merge(cumr[v][j + 1], dat[v][j]);
        }
        dfs2(v, u);
      }
    }
  }
  void solve() {
    dfs1(0, -1);
    dfs2(0, -1);
  }
};

int main() {
  ll N;
  io.read(N);
  vector<vector<int>> G(N);
  REP(i, N - 1) {
    int A, B;
    io.read(A, B);
    G[A - 1].push_back(B - 1);
    G[B - 1].push_back(A - 1);
  }
  Rerooting<ll> rr(
      G, [](ll l, ll r) { return l + r; }, [](ll a) { return a + 1; }, 0);
  rr.solve();
  modint ans = 0;
  vector<modint> pow2(N + 1);
  pow2[0] = 1;
  FOR(i, 1, N + 1) pow2[i] = pow2[i - 1] * 2;
  REP(i, N) {
    modint t = pow2[N - 1] - 1;
    REP(j, G[i].size()) t -= pow2[rr.dat[i][j]] - 1;
    ans += t;
  }
  ans /= pow2[N];
  io.writeln(ans.x);
  return 0;
}