#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair Node; const ll MOD = 1000000007; const int MAX_N = 1000010; vector E[MAX_N]; int P[MAX_N]; int N; int find_root(int v) { if (P[v] == -1) return v; return find_root(P[v]); } ll dfs(int v, int depth) { ll len = 0; for (int u : E[v]) { len += depth * (depth + 1) / 2; len %= MOD; len += dfs(u, depth + 1); len %= MOD; } return len; } int main() { memset(P, -1, sizeof(P)); cin >> N; for (int i = 0; i < N - 1; ++i) { int a, b; cin >> a >> b; E[a].push_back(b); P[b] = a; } int root = find_root(1); ll ans = 0; // cout << dfs(root, 1) << endl; queue que; que.push(Node(root, 1)); while (not que.empty()) { Node node = que.front(); int v = node.first; int depth = node.second; que.pop(); for (int u : E[v]) { ans += depth * (depth + 1) / 2; ans %= MOD; que.push(Node(u, depth + 1)); } } cout << ans << endl; return 0; }