import sys sys.setrecursionlimit(10**7) input=sys.stdin.readline def main1(n,uvw): ki=[[] for _ in range(n)] for u,v,w in uvw: u,v=u-1,v-1 ki[u].append([v,w]) ki[v].append([u,w]) mod=10**9+7 memo={} def dfs1(p,v):# if (p,v) in memo:return memo[(p,v)] numv,sumv=1,0 for nv,nw in ki[v]: if nv==p:continue num_,sum_=dfs1(v,nv) numv+=num_ sumv+=sum_+num_*nw sumv%=mod memo[(p,v)]=numv,sumv return numv,sumv dfs1(-1,0) ans=[0] def dfs2(p,v): snumv,ssumv=1,0 for nv,nw in ki[v]: num_,sum_=memo[(v,nv)] snumv+=num_ ssumv+=sum_+num_*nw ssumv%=mod for nv,nw in ki[v]: if nv==p:continue num0,sum0=memo[(v,nv)] num1,sum1=snumv-num0,ssumv-sum0-nw*num0 num1,sum1=num1%mod,sum1%mod tmp=num0*sum1%mod+num1*sum0%mod+nw*num0*num1%mod tmp%=mod tmp*=nw tmp%=mod ans[0]+=tmp ans[0]%=mod memo[(nv,v)]=num1%mod,sum1%mod dfs2(v,nv) dfs2(-1,0) #print(memo) return ans[0] if __name__=='__main__': n=int(input()) uvw=[list(map(int,input().split())) for _ in range(n-1)] ret1=main1(n,uvw) print(ret1)