#include using namespace std; const long long MOD = 1000000007; int main(){ int N; cin >> N; vector>> E(N); for (int i = 0; i < N - 1; i++){ int u, v, w; cin >> u >> v >> w; u--; v--; E[u].push_back(make_pair(w, v)); E[v].push_back(make_pair(w, u)); } vector p(N, -1); vector>> c(N); queue Q; Q.push(0); vector b; while (!Q.empty()){ int v = Q.front(); Q.pop(); b.push_back(v); for (auto P : E[v]){ int w = P.second; if (w != p[v]){ p[w] = v; c[v].push_back(P); Q.push(w); } } } reverse(b.begin(), b.end()); vector dp1(N, 1); vector dp2(N, 0); vector dp3(N, 0); long long ans = 0; for (int v : b){ vector> T; for (auto P : c[v]){ int w = P.second; long long A = dp1[w]; long long B = (dp2[w] + dp1[w] * P.first) % MOD; long long C = (dp3[w] + dp2[w] * P.first * 2 + dp1[w] * P.first % MOD * P.first) % MOD; T.push_back(make_tuple(A, B, C)); dp1[v] += A; dp1[v] %= MOD; dp2[v] += B; dp2[v] %= MOD; dp3[v] += C; dp3[v] %= MOD; } ans += dp3[v]; int cnt = T.size(); long long Asum = 0, Bsum = 0, Csum = 0; for (int i = 0; i < cnt; i++){ ans += get<0>(T[i]) * Csum % MOD; ans += get<1>(T[i]) * Bsum * 2 % MOD; ans += get<2>(T[i]) * Asum % MOD; ans %= MOD; Asum += get<0>(T[i]); Asum %= MOD; Bsum += get<1>(T[i]); Bsum %= MOD; Csum += get<2>(T[i]); Csum %= MOD; } } ans %= MOD; cout << ans << endl; }