#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007ll
#define t tie
using ll = long long;
int n;
ll ans;
#define N 250010
struct edg {
int v;
ll w;
edg(int v, ll w): v(v), w(w) {}
};
vector <edg> g[N];
set<pair<int, ll> >a[N];
ll f[N];
void dfs(int u, int v, ll co) {
a[u].emplace(u, 1);
f[u] = 1 + (ll)(u - 1) * u / 2 % mod + (ll)(n - u) * (n - u + 1) / 2 % mod;
f[u] %= mod;
for (auto&& tmp : g[u]) {
int i = tmp.v;
ll w = tmp.w;
if (i == v) continue;
dfs(i, u, w);
if (u == 1) continue;
if (a[i].size() > a[u].size()) {
swap(a[i], a[u]);
swap(f[i], f[u]);
}
for (auto j = a[i].begin(); j != a[i].end(); ++j) {
auto it = a[u].upper_bound(*j);
auto pt = prev(it);
ll cz = (it == a[u].end() ? n + 1 : it -> fi) - (it == a[u].begin() ? 1 : pt -> fi + pt -> se);
ll clz = j -> fi - (it == a[u].begin() ? 1 : pt -> fi + pt -> se);
ll ctz = (it == a[u].end() ? n + 1 : it -> fi) - j -> fi - j -> se;
f[u] += clz * (clz + 1) / 2 % mod + ctz * (ctz + 1) / 2 % mod - cz * (cz + 1) / 2 % mod;
f[u] %= mod;
if (f[u] < 0) f[u] += mod;
pair<int, int> ins = *j;
f[u] += (ll)(ins.se) * (ins.se + 1) / 2 % mod;
f[u] %= mod;
if (it != a[u].begin() && pt -> fi + pt -> se == j -> fi) {
f[u] += -(ll)(pt -> se) * (pt -> se + 1) / 2 % mod + (ll)(pt -> se + j -> se) * (pt -> se + j -> se + 1) / 2 % mod - (ll)(ins.se) * (ins.se + 1) / 2 % mod;
f[u] %= mod;
if (f[u] < 0) f[u] += mod;
ins.fi = pt -> fi;
ins.se = pt -> se + j -> se;
a[u].erase(pt);
}
if (it != a[u].end() && ins.fi + ins.se == it -> fi) {
f[u] += -(ll)(ins.se) * (ins.se + 1) / 2 % mod + (ll)(ins.se + it -> se) * (ins.se + it -> se + 1) / 2 % mod - (ll)(it -> se) * (it -> se + 1) / 2 % mod;
f[u] %= mod;
if (f[u] < 0) f[u] += mod;
ins.se += it -> se;
a[u].erase(it);
}
a[u].emplace(ins);
}
}
if (u == 1) return;
ll zs = (ll)n * (n + 1) / 2 % mod - f[u];
if (zs < 0) zs += mod;
ans += zs * co % mod;
ans %= mod;
}
int maintenance_costs_sum(vector<int> U, vector<int> V, vector<int> W) {
n = U.size() + 1;
for (decltype(U.size()) i = 0; i < U.size(); ++i) {
g[U[i] + 1].emplace_back(V[i] + 1, W[i]);
g[V[i] + 1].emplace_back(U[i] + 1, W[i]);
}
dfs(1, 0, 0);
return ans;
}
int main() {
int tot = maintenance_costs_sum({0, 2, 2, 0}, {2, 1, 4, 3}, {2, 1, 1, 1});
cout << tot << endl;
return 0;
}