#include #include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double #define INF 1000000000000000000 typedef pair pll; // modint (1000000007 で割ったあまりを扱う構造体) template struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) v += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; } constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; } constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; } constexpr Fp &operator+=(const Fp &r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp &operator-=(const Fp &r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp &operator*=(const Fp &r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp &operator/=(const Fp &r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator==(const Fp &r) const noexcept { return this->val == r.val; } constexpr bool operator!=(const Fp &r) const noexcept { return this->val != r.val; } friend constexpr ostream &operator<<(ostream &os, const Fp &x) noexcept { return os << x.val; } friend constexpr istream &operator>>(istream &is, Fp &x) noexcept { return is >> x.val; } friend constexpr Fp modpow(const Fp &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; const int MOD = 1000000007; using mint = Fp; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector> G(N, vector()); vector root(N, -1); rep(i, N - 1) { ll a, b; cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); root[b] = a; } ll s; rep(i, N) { if (root[i] == -1) { s = i; break; } } vector dist(N, -1); // 全頂点を「未訪問」に初期化 queue que; // 初期条件 (頂点 0 を初期ノードとする) dist[s] = 0; que.push(s); // 0 を橙色頂点にする // BFS 開始 (キューが空になるまで探索を行う) while (!que.empty()) { int v = que.front(); // キューから先頭頂点を取り出す que.pop(); // v から辿れる頂点をすべて調べる for (int nv : G[v]) { if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない // 新たな白色頂点 nv について距離情報を更新してキューに追加する dist[nv] = dist[v] + 1; que.push(nv); } } mint ans = 0; rep(i, N) { mint tmp = dist[i]; if (dist[i] >= 2) { tmp = (1 + dist[i]) * dist[i] / 2; } ans += tmp; } cout << ans << endl; }