#include using namespace std; #define rep(i,n) for(ll i=0;i=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue,greater> #define all(x) (x).begin(),(x).end() #define CST(x) cout<; using vvl=vector>; using pl=pair; using vpl=vector; using vvpl=vector; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[8]={-1,0,1,0,1,1,-1,-1}; const ll dx[8]={0,-1,0,1,1,-1,1,-1}; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } const int mod = 1000000007; const int max_n = 200005; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} 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 { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } bool operator==(const mint &p) const { return x == p.x; } bool operator!=(const mint &p) const { return x != p.x; } // 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 { return mint(*this) /= a;} }; istream& operator>>(istream& is, mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} struct Edge{ ll to,weight; }; template class Rerooting{ public: int n; T def; function merge; function addroot; vector> g; vector> dp; public: vector ans; Rerooting(int _n,T _def,function _merge,function _addroot) :n(_n),def(_def),merge(_merge),addroot(_addroot) { g=vector>(n); dp=vector>(n); ans=vector(n,def); } void add_edge(int a,Edge e){ g[a].pb(e); } T dfs(int v=0,int par=-1){ T ret=def; dp[v].resize(g[v].size()); rep(i,g[v].size()){ if(g[v][i].to==par){ continue; } dp[v][i]=addroot(dfs(g[v][i].to,v),g[v][i].weight); ret=merge(ret,dp[v][i]); } return ret; } void bfs(int v,T val,int par=-1){ int deg=g[v].size(); rep(i,deg){ if(g[v][i].to==par)dp[v][i]=val; } vector dpl(deg+1,def),dpr(deg+1,def); rep(i,deg)dpl[i+1]=merge(dpl[i],dp[v][i]); per(i,deg)dpr[i]=merge(dpr[i+1],dp[v][i]); ans[v]=dpl[deg]; rep(i,deg){ int nv=g[v][i].to; if(nv==par)continue; bfs(nv,addroot(merge(dpl[i],dpr[i+1]),g[v][i].weight),v); } } }; struct val{ mint sq,sum,len; }; int main(){ ll n;cin >> n; auto mg=[](val a,val b){ a.sq+=b.sq;a.sum+=b.sum;a.len+=b.len; return a; }; auto addroot=[](val a,ll b){ a.sq+=a.sum*2*b+(a.len+1)*b*b; a.sum+=(a.len+1)*b; a.len+=1; return a; }; val def={0,0,0}; Rerooting g(n,def,mg,addroot); rep(i,n-1){ ll a,b,w;cin >> a >> b >> w;a--;b--; g.add_edge(a,{b,w}); g.add_edge(b,{a,w}); } g.dfs(0); g.bfs(0,def); mint ans=0; rep(i,n)ans+=g.ans[i].sq; cout << ans/2 <