import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines MOD = 1_000_000_007 def bfs_tree(N, G, root=1): q = [root] parent = [0] * (N + 1) dist = [0] * (N + 1) for v in q: for w, cost in G[v]: if w == parent[v]: continue dist[w] = dist[v] + cost parent[w] = v q.append(w) return parent, q, dist def main(N, G): parent, order, dist = bfs_tree(N, G) ans = 0 # k 乗和 dp_0 = [0] * (N + 1) dp_1 = [0] * (N + 1) dp_2 = [0] * (N + 1) dp = [0] * (N + 1) # lca ごとの答 for v in order[::-1]: dp_0[v] += 1 dp[v] += dp_2[v] * dp_0[v] * 2 dp[v] += 2 * dp_1[v]**2 dp_0[v] %= MOD dp_1[v] %= MOD dp_2[v] %= MOD dp[v] %= MOD p = parent[v] if p == 0: break d = dist[v] - dist[p] dp[p] -= dp_0[v] * dp_2[v] * 2 + 4 * d * d * dp_0[v]**2 dp[p] -= 2 * dp_1[v] * dp_1[v] dp[p] -= 8 * d * dp_0[v] * dp_1[v] dp_0[p] += dp_0[v] dp_1[p] += dp_1[v] + d * dp_0[v] dp_2[p] += dp_2[v] + 2 * d * dp_1[v] + d * d * dp_0[v] ans = sum(dp) % MOD if ans & 1: ans += MOD ans //= 2 return ans N = int(readline()) G = [[] for _ in range(N + 1)] for line in readlines(): a, b, c = map(int, line.split()) G[a].append((b, c)) G[b].append((a, c)) print(main(N, G))