class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a0: result2.append(result[i]) return result2 n = int(input()) mod = 10 ** 9 + 7 abc = [list(map(int,input().split())) for _ in range(n - 1)] ans = 0 for bit in range(32): U = dsu(n + 1) for i in range(n - 1): a, b, c = abc[i] a -= 1; b -= 1 if 1 & (c >> bit): U.merge(a, b) s = set() for i in range(n): if U.leader(i) in s: continue else: s.add(U.leader(i)) c = U.size(i) ans += pow(2, bit, mod) * c * (c - 1) // 2 ans %= mod print(ans)