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 bi2 = 1 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) for i in range(n): x = U.size(i) if x > 1: ans += bi2 * (x - 1) * x // 2 ans %= mod bi2 *= 2 bi2 %= mod print(ans)