import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(2*10**5+10) write = lambda x: sys.stdout.write(x+"\n") _print = lambda *x: print(*x, file=sys.stderr) # グラフの読み込み n = int(input()) ns = [[] for _ in range(n)] for _ in range(n-1): u,v,c = map(int, input().split()) u -= 1 v -= 1 ns[u].append((c,v)) ns[v].append((c,u)) # 深さ優先探索 (巻き戻しあり) dfs q = [(0, -1)] d = [0]*n s = [0]*n k = [0]*n v1 = 0 v2 = 0 while q: u,prv = q.pop() if u<0: # 返るときの処理 u = ~u ss = 0 ss2 = 0 kk = 0 kk2 = 0 for c,v in ns[u]: if v==prv: continue ss += s[v] ss2 += s[v]*s[v] kk += k[v] kk2 += k[v]**2 v1 += ((ss**2 - ss2)//2 + ss) * d[u]**2 v2 += ((kk**2 - kk2)//2 + kk+ss*d[u]) * d[u] v1 %= M v2 %= M s[u] = ss + 1 k[u] = kk + d[u] else: q.append((~u,prv)) for c,v in ns[u]: # 進むときの処理 if v==prv: continue q.append((v,u)) d[v] = d[u] + c S = 0 S2 = 0 for v in d: S += v S2 += v*v S%=M S2%=M ans = n*S2 + S*S - 2*S2 + 4*v1 - 4*v2 print(ans%M)