#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; using mint = modint1000000007; const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; //const bool debug = false; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair<long long,long long> template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } struct ReRooting { struct Edge {//★★ int to; mint cost; Edge(int _to, mint _cost) :to(_to), cost(_cost) {} }; struct Data {//★★ mint sump; mint sum; mint count; Data() :sump(0), sum(0), count(0) {} Data(mint _sump, mint _sum, mint _count) :sump(_sump), sum(_sum), count(_count) {} }; vector<vector<Edge>>g; vector<Data>dp; vector<Data>dpR; int sz; ReRooting(int _sz) :sz(_sz) { g.resize(sz); dp.reserve(sz); dpR.resize(sz); } void ReadGraph() {//★★ rep(i, sz - 1) { int a, b, c; cin >> a >> b >> c; a--; b--; g[a].push_back({ b,c }); g[b].push_back({ a,c }); } } Data merge(const Data &lh, const Data &rh, mint cost) {//★★ Data ret = lh; ret.sump += rh.sump + rh.sum * cost * 2 + cost * cost * (rh.count + 1); ret.sum += rh.sum + cost * (rh.count + 1); ret.count += rh.count + 1; return ret; } Data mergeR(const Data &lh, const Data &rh, mint cost) {//★★ Data ret = lh; ret.sump -= rh.sump + rh.sum * cost * 2 + cost * cost * (rh.count + 1); ret.sum -= rh.sum + cost * (rh.count + 1); ret.count -= rh.count + 1; return ret; } void dfs(int v, int p = -1) { Data val; for (auto e : g[v]) { if (p == e.to)continue; dfs(e.to, v); val = merge(val, dp[e.to], e.cost); } dp[v] = val; } void dfsR(int v, int p = -1, mint cost = 0) { dpR[v] = dp[v]; if (-1 != p) { Data sub = mergeR(dpR[p], dp[v], cost); dpR[v] = merge(dpR[v], sub, cost); } for (auto e : g[v]) { if (p == e.to)continue; dfsR(e.to, v, e.cost); } } void output() {//★★ mint ans = 0; rep(i, sz)ans += dpR[i].sump; ans *= inv_mod(2, mod); cout << ans.val() << endl; } void solve() { ReadGraph(); dfs(0); dfsR(0); output(); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; ReRooting reroot(n); reroot.solve(); return 0; }