#include <bits/stdc++.h>

const int N = 200010;
const long long INF = 1ll<<60;
const int inf = 0x3f3f3f3f;
typedef std::pair<int, int> PairInt;
typedef std::pair<long long, int> Pair;
const long long mod = 1e9 + 7;

int n;
std::vector<std::pair<int, long long>> g[N];
int dp[N][60][2];
int mi[100];
int ans;

void Dfs(int u, int f) {
  for (int i = 0; i < 60; ++i) dp[u][i][0] = 1;
  for (size_t i = 0; i < g[u].size(); ++i) {
    int v = g[u][i].first;
    long long w = g[u][i].second;
    if (v == f) continue;
    Dfs(v, u);
    for (int j = 0; j < 60; ++j) {
      if ((w >> j) & 1) {
        ans = (ans + 1ll * dp[u][j][0] * dp[v][j][0] % mod * mi[j] % mod) % mod;
        ans = (ans + 1ll * dp[u][j][1] * dp[v][j][1] % mod * mi[j] % mod) % mod;
      } else {
        ans = (ans + 1ll * dp[u][j][0] * dp[v][j][1] % mod * mi[j] % mod) % mod;
        ans = (ans + 1ll * dp[u][j][1] * dp[v][j][0] % mod * mi[j] % mod) % mod;
      }
    }
    for (int j = 0; j < 60; ++j) {
      if ((w >> j) & 1) {
        dp[u][j][0] = (dp[u][j][0] + dp[v][j][1]) % mod;
        dp[u][j][1] = (dp[u][j][1] + dp[v][j][0]) % mod;
      } else {
        dp[u][j][0] = (dp[u][j][0] + dp[v][j][0]) % mod;
        dp[u][j][1] = (dp[u][j][1] + dp[v][j][1]) % mod;
      }
    }
  }
}

int main() {
  scanf("%d", &n);
  mi[0] = 1;
  for (int i = 1; i < 60; ++i) mi[i] = 2ll * mi[i - 1] % mod;
  for (int i = 1; i < n; ++i) {
    int u, v;
    long long w;
    scanf("%d%d%lld", &u, &v, &w);
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  Dfs(1, 0);
  printf("%d\n", ans);

  return 0;
}
#include <bits/stdc++.h>
using namespace std;

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,s,e) for(int i=s ; i < e ; i++)
#define rrep(i,s,e) for(int i=s ; i > e ; i--)
#define srep(i,s,e,j) for(int i=s ; i < e ; i+=j)
#define tr(i,x) for(auto i : x)
#define inp(a) for(int i=0 ; i<a.size() ; i++)cin>>a[i]
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vb vector<bool>
#define vpi vector<pii>
#define maxpqi priority_queue<int>
#define minpqi priority_queue <int, vector<int>, greater<int> >
#define pii pair<int,int> 
#define F first
#define S second
#define mk make_pair
#define pb push_back
#define pf push_front
#define endl '\n'
#define br printf("\n")
#define inf 1000000
#define imap map<int,int>   
#define uimap unordered_map<int,int>
#define gcd(a,b) __gcd(a,b)
#define clr(x) memset(x,0,sizeof(x))
#define fill(x,y) memset(x,y,sizeof(x))
#define lb lower_bound
#define ub upper_bound
#define npos string::npos
#define all(x) x.begin(),x.end()
#define descen greater<int>()

const int mod = 1e9 + 7;

int SumOfXorOfAllPairs(int a[],int n,int numbits){
    int sum = 0;
    int cnt[2];
    rep(i,0,numbits){
        clr(cnt);
        rep(j,0,n)cnt[(a[j]>>i)&1]++;
        sum += (1LL<<i)%mod*cnt[0]%mod*cnt[1];
        sum = sum%mod;
    }
    return sum;
}

void solve(){
    int n;cin>>n;
    vpi adj[n];
    int u,v,w;
    rep(i,0,n-1){
        cin>>u>>v>>w;
        u--;v--;
        adj[u].pb({v,w});
        adj[v].pb({u,w});
    }
    bool vis[n];fill(vis,false);
    int dist[n];clr(dist);
    queue<int>q;
    q.push(0);
    int curr;
    while(!q.empty()){
        curr = q.front();q.pop();
        tr(i,adj[curr]){
            if(!vis[i.F]){
                dist[i.F] = dist[curr]^i.S;
                vis[i.F] = true;
                q.push(i.F);
            }
        }
    }
    cout << SumOfXorOfAllPairs(dist,n,60LL) << endl;
}

int32_t main()
{
    fastio
    // ios::sync_with_stdio(false);
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    solve();
    return 0;
}

/*
// Sort the array in descending order 
    sort(arr, arr + n, greater<int>()); 
*/