import sys readline = sys.stdin.readline def parorder(Edge, p): N = len(Edge) par = [0]*N dist = [0]*N par[p] = -1 stack = [p] order = [] visited = set([p]) ast = stack.append apo = order.append while stack: vn = stack.pop() apo(vn) for w, vf in Edge[vn]: if vf in visited: continue visited.add(vf) par[vf] = vn dist[vf] = w ast(vf) return par, order, dist def getcld(p): res = [[] for _ in range(len(p))] for i, v in enumerate(p[1:], 1): res[v].append(i) return res MOD = 10**9+7 N = int(readline()) Edge = [[] for _ in range(N)] for _ in range(N-1): a, b, w = map(int, readline().split()) a -= 1 b -= 1 Edge[a].append((w, b)) Edge[b].append((w, a)) P, L, dist = parorder(Edge, 0) dsum = [0]*N dsum2 = [0]*N size = [1]*N ans = 0 for l in L[:0:-1]: p = P[l] w = dist[l] dsum2[l] = (dsum2[l] + w*w%MOD*size[l] + 2*w*dsum[l])%MOD dsum[l] = (dsum[l] + w*size[l])%MOD ans = (ans + (dsum[l] + dsum[p])**2 - dsum[l]*dsum[l] - dsum[p]*dsum[p] + size[p]*dsum2[l] + size[l]*dsum2[p])%MOD dsum[p] = (dsum[p] + dsum[l])%MOD dsum2[p] = (dsum2[p] + dsum2[l])%MOD size[p] += size[l] print(ans)