#include struct FastIO { char buf[20000000]; char *ptr = buf; void read() { int size; size = fread(buf, 1, sizeof(buf) - 1, stdin); buf[size] = '\0'; ptr = buf; } FastIO () { read(); } void inc() { ptr++; // if (++ptr == buf + sizeof(buf) || !*ptr) read(); } template Integer read() { bool neg = false; Integer res = 0; while ((*ptr < '0' || *ptr > '9') && *ptr != '-') inc(); if (*ptr == '-') neg = true, inc(); while (*ptr >= '0' && *ptr <= '9') res = res * 10 + *ptr - '0', inc(); return neg ? -res : res; } } fastio; #define ri fastio.read #define rs64 fastio.read std::vector > hen; int main() { int n = ri(); int degree[n]; memset(degree, 0, sizeof(degree)); bool used[n]; memset(used, 0, sizeof(used)); std::pair hens[n - 1]; for (int i = 0; i + 1 < n; i++) { hens[i].first = ri() - 1; hens[i].second = ri() - 1; degree[hens[i].first]++; used[hens[i].second] = true; } int root = std::find(used, used + n, 0) - used; hen.resize(n); for (int i = 0; i < n; i++) hen[i].resize(degree[i]); for (auto i : hens) hen[i.first][--degree[i.first]] = i.second; int64_t res = 0; std::vector cur, next; cur.reserve(n); next.reserve(n); cur.push_back(root); for (int i = 0; cur.size(); i++) { res += (int64_t) i * (i + 1) / 2 * cur.size(); next.clear(); for (auto j : cur) next.insert(next.end(), hen[j].begin(), hen[j].end()); std::swap(cur, next); } printf("%d\n", (int) (res % 1000000007)); return 0; }