""" A*pow(10,C) // B % 10 """ import sys from sys import stdin def uf_find(n,p): ufl = [] while p[n] != n: ufl.append(n) n = p[n] for i in ufl: p[i] = n return n def uf_union(a,b,p,rank): ap = uf_find(a,p) bp = uf_find(b,p) if ap == bp: return True else: if rank[ap] > rank[bp]: p[bp] = ap rank[ap] += rank[bp] else: p[ap] = bp rank[bp] += rank[bp] N = int(stdin.readline()) mod = 10**9+7 ABC = [] for i in range(N-1): A,B,C = map(int,stdin.readline().split()) A -= 1 B -= 1 ABC.append( (A,B,C) ) ans = 0 for bit in range(30): nb = 2**bit p = [i for i in range(N)] rank = [1] * N for A,B,C in ABC: if C & nb > 0: uf_union(A,B,p,rank) now = 0 for v in range(N): if uf_find(v,p) == v: now += rank[v] * (rank[v]-1) // 2 ans += now * nb ans %= mod print (ans)