import sys
input = sys.stdin.readline

mod=10**9+7
n=int(input())

# UnionFind

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

E=[tuple(map(int,input().split())) for i in range(n-1)]

LANS=0

for i in range(32):
    Group = [i for i in range(n+1)] # グループ分け
    Nodes = [1]*(n+1) # 各グループのノードの数

    for x,y,c in E:
        x-=1
        y-=1

        if c & (1<<i) != 0:
            Union(x,y)

    ANS=0

    for j in range(n):
        if find(j)==j:
            x=Nodes[j]

            ANS+=x*(x-1)//2

    LANS+=ANS*(1<<i)

print(LANS%mod)