// https://atcoder.jp/contests/otemae2019/submissions/6651746 #include using namespace std; typedef long long ll; const ll mod = 1000000007; int N; vector> G[500000]; ll ans = 0; int subnum[500000]; int calc_subnum(int v, int from) { int ret = 1; for (int i = 0; i < G[v].size(); i++){ int to = G[v][i].second; if (to == from)continue; ret += calc_subnum(to, v); } return subnum[v] = ret; } //eを通るu-v pathの個数 void calc1(int v, int from, int l) { ans += (ll)subnum[v] * (N - subnum[v]) % mod * l % mod * l; ans %= mod; for (int i = 0; i < G[v].size(); i++) { int to = G[v][i].second; if (to == from)continue; calc1(to, v, G[v][i].first); } } //根とe'の間にe ll calc2(int v, int from) { ll ret = 0; for (int i = 0; i < G[v].size(); i++) { int to = G[v][i].second; if (to == from)continue; ll next = calc2(to, v); ans += next * (N - subnum[to]) % mod * G[v][i].first; ans %= mod; ret += next; ret += subnum[to] * (ll)G[v][i].first; ret %= mod; } return ret; }//eとe'がバラバラ ll calc3(int v, int from) { ll ret = 0; vector x; for (int i = 0; i < G[v].size(); i++) { int to = G[v][i].second; if (to == from)continue; ll next = (calc3(to, v) + subnum[to] * (ll)G[v][i].first) % mod; x.push_back(next); ret += next; ret %= mod; } ll sum = 0; for (int i = 0; i < x.size(); i++) { sum += x[i]; } sum %= mod; for (int i = 0; i < x.size(); i++) { ans += x[i] * (sum - x[i] + mod); ans %= mod; } return ret; } int main() { cin >> N; //入力 for (int i = 0; i < N - 1; i++) { int A, B, C; cin >> A >> B >> C; A--; B--; G[A].push_back({C, B}); G[B].push_back({C, A}); } calc_subnum(0, -1); calc1(0, -1, 0); calc2(0, -1); //eとe'の上下を入れ替えてもう一度 calc2(0, -1); calc3(0, -1); cout << ans << endl; return 0; }