#include #ifdef DEBUG #include #else #define dump(...) #endif /** * @title Modint * @docs mint.md */ template class ModInt{ public: constexpr static uint32_t MOD = M; uint64_t val; constexpr ModInt(): val(0){} constexpr ModInt(int64_t n){ if(n >= M) val = n % M; else if(n < 0) val = n % M + M; else val = n; } inline constexpr auto operator+(const ModInt &a) const {return ModInt(val + a.val);} inline constexpr auto operator-(const ModInt &a) const {return ModInt(val - a.val);} inline constexpr auto operator*(const ModInt &a) const {return ModInt(val * a.val);} inline constexpr auto operator/(const ModInt &a) const {return ModInt(val * a.inv().val);} inline constexpr auto& operator=(const ModInt &a){val = a.val; return *this;} inline constexpr auto& operator+=(const ModInt &a){if((val += a.val) >= M) val -= M; return *this;} inline constexpr auto& operator-=(const ModInt &a){if(val < a.val) val += M; val -= a.val; return *this;} inline constexpr auto& operator*=(const ModInt &a){(val *= a.val) %= M; return *this;} inline constexpr auto& operator/=(const ModInt &a){(val *= a.inv().val) %= M; return *this;} inline constexpr bool operator==(const ModInt &a) const {return val == a.val;} inline constexpr bool operator!=(const ModInt &a) const {return val != a.val;} inline constexpr auto& operator++(){*this += 1; return *this;} inline constexpr auto& operator--(){*this -= 1; return *this;} inline constexpr auto operator++(int){auto t = *this; *this += 1; return t;} inline constexpr auto operator--(int){auto t = *this; *this -= 1; return t;} inline constexpr static ModInt power(int64_t n, int64_t p){ if(p < 0) return power(n, -p).inv(); int64_t ret = 1, e = n % M; for(; p; (e *= e) %= M, p >>= 1) if(p & 1) (ret *= e) %= M; return ret; } inline constexpr static ModInt inv(int64_t a){ int64_t b = M, u = 1, v = 0; while(b){ int64_t t = a / b; a -= t * b; std::swap(a,b); u -= t * v; std::swap(u,v); } u %= M; if(u < 0) u += M; return u; } inline constexpr static auto frac(int64_t a, int64_t b){return ModInt(a) / ModInt(b);} inline constexpr auto power(int64_t p) const {return power(val, p);} inline constexpr auto inv() const {return inv(val);} friend inline constexpr auto operator-(const ModInt &a){return ModInt(-a.val);} friend inline constexpr auto operator+(int64_t a, const ModInt &b){return ModInt(a) + b;} friend inline constexpr auto operator-(int64_t a, const ModInt &b){return ModInt(a) - b;} friend inline constexpr auto operator*(int64_t a, const ModInt &b){return ModInt(a) * b;} friend inline constexpr auto operator/(int64_t a, const ModInt &b){return ModInt(a) / b;} friend std::istream& operator>>(std::istream &s, ModInt &a){s >> a.val; return s;} friend std::ostream& operator<<(std::ostream &s, const ModInt &a){s << a.val; return s;} template inline static auto div(){ static auto value = inv(N); return value; } explicit operator int32_t() const noexcept {return val;} explicit operator int64_t() const noexcept {return val;} }; using mint = ModInt<1000000007>; /** * @title Graph template * @docs graph_template.md */ template class Edge{ public: int from,to; Cost cost; Edge() {} Edge(int to, Cost cost): to(to), cost(cost){} Edge(int from, int to, Cost cost): from(from), to(to), cost(cost){} }; template using Graph = std::vector>>; template using Tree = std::vector>>; template void add_edge(C &g, int from, int to, T w = 1){ g[from].emplace_back(from, to, w); } template void add_undirected(C &g, int a, int b, T w = 1){ add_edge(g, a, b, w); add_edge(g, b, a, w); } /** * @title Fixed point combinator * @docs fix_point.md */ template struct FixPoint : F{ explicit constexpr FixPoint(F &&f) noexcept : F(std::forward(f)){} template constexpr auto operator()(Args &&... args) const { return F::operator()(*this, std::forward(args)...); } }; template inline constexpr auto make_fix_point(F &&f){ return FixPoint(std::forward(f)); } template inline constexpr auto make_fix_point(F &f){ return FixPoint(std::forward(f)); } int main(){ std::cin.tie(0); std::ios::sync_with_stdio(false); int N; while(std::cin >> N){ Tree tree(N); std::vector indeg(N); for(int i = 0; i < N-1; ++i){ int a, b; std::cin >> a >> b; --a, --b; add_edge(tree, a, b, 1); indeg[b] += 1; } int root = std::find(indeg.begin(), indeg.end(), 0) - indeg.begin(); std::vector sum(N); std::vector sub(N); auto f = make_fix_point( [&](auto &&f, int cur) -> mint{ mint ret = 0; sub[cur] = 1; for(auto &e : tree[cur]){ ret += f(e.to); sub[cur] += sub[e.to]; } ret += sub[cur] - 1; return sum[cur] = ret; } ); f(root); auto ans = std::accumulate(sum.begin(), sum.end(), mint(0)); dump(sub, sum); std::cout << ans << "\n"; } return 0; }