#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; using ull = unsigned long long; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } //template T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; } //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353; struct mint { long long x; mint(long long x = 0) :x((x% mod + mod) % mod) {} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int n; cin >> n; vector>> g(n); for (int i = 0; i < n - 1; i++) { int u, v; ll w; cin >> u >> v >> w; u--; v--; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } vector sz(n); vector len(n); vector dp(n); auto dfs1 = [&](auto&& self, int cur, int pre)->void { sz[cur] += 1; for (auto [nxt, w] : g[cur]) { if (nxt == pre) continue; self(self, nxt, cur); sz[cur] += sz[nxt]; len[cur] += len[nxt] + mint(w) * sz[nxt]; dp[cur] += mint(w * w) * sz[nxt] + mint(2) * w * len[nxt] + dp[nxt]; } }; mint res = 0; dfs1(dfs1, 0, -1); auto dfs2 = [&](auto&& self, int cur, int pre)->void { sz[cur] = 1; len[cur] = 0; dp[cur] = 0; for (auto [nxt, w] : g[cur]) { sz[cur] += sz[nxt]; len[cur] += len[nxt] + mint(w) * sz[nxt]; dp[cur] += mint(w * w) * sz[nxt] + mint(2) * w * len[nxt] + dp[nxt]; } res += dp[cur]; for (auto [nxt, w] : g[cur]) { if (nxt == pre) continue; mint psz = sz[cur]; mint l = len[cur]; mint d = dp[cur]; sz[cur] -= sz[nxt]; len[cur] -= len[nxt] + mint(w) * sz[nxt]; dp[cur] -= mint(w * w) * sz[nxt] + mint(2) * w * len[nxt] + dp[nxt]; self(self, nxt, cur); sz[cur] = psz; len[cur] = l; dp[cur] = d; } }; dfs2(dfs2, 0, -1); res /= 2; cout << res.x << "\n"; }