#include <bits/stdc++.h>
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=4e5+10;
const int INF=0x3f3f3f3f;
int a[N],n;
pii pa[N];
int mx[N<<2],lazy[N<<2];
int main() {
    auto push_down=[&](int rt){
        if(lazy[rt]){
            lazy[ls]+=lazy[rt];
            lazy[rs]+=lazy[rt];
            mx[ls]-=lazy[rt];
            mx[rs]-=lazy[rt];
            lazy[rt]=0;
        }
    };
    function<void(int,int,int)>build=[&](int l,int r,int rt){
        if(l==r){mx[rt]=l;return ;}
        int mid=l+r>>1;
        build(l,mid,ls);
        build(mid+1,r,rs);
    };
    function<int(int,int,int,int)>query=[&](int l,int r,int x,int rt){
        if(l==r)return mx[rt];
        push_down(rt);int mid=l+r>>1;
        if(x<=mid)return query(l,mid,x,ls);else return query(mid+1,r,x,rs);
    };
    function<void(int,int,int,int,int,int)>update=[&](int l,int r,int L,int R,int val,int rt){
        if(L<=l&&r<=R){mx[rt]-=val;lazy[rt]+=val;return ;}
        push_down(rt);int mid=l+r>>1;
        if(L<=mid)update(l,mid,L,R,val,ls);
        if(mid<R)update(mid+1,r,L,R,val,rs);
        mx[rt]=max(mx[ls],mx[rs]);
    };
    cin>>n;
    vector<pair<int,int>>pa;
    for(int i=1;i<=2*n;i++){
		cin>>a[i],pa.emplace_back(a[i],i>n?i-n:n-i+1);
	}
	sort(pa.rbegin(),pa.rend());
    ll ans=0;
    build(1,n,1);
    for(auto& i:pa){
        int x=i.second;
        int val=query(1,n,x,1);
        if(val==0)continue;
        ans+=i.first;
        int l=1,r=n,res=-1;
        while (l<=r){
            int mid=l+r>>1;
            if(query(1,n,mid,1)>=val){
                res=mid;
                r=mid-1;
            }else l=mid+1;
        }
        update(1,n,res,n,1,1);
    }
    cout<<ans;
    return 0;
}

#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<queue>
#include<stack>
#include<utility>
#include<cstdlib>
#include<cstdio>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<list>
#include<tuple>
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
 
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
 
#define FOR(i, a, b) for(ll i=(a); i<(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define NREP(i, n) FOR(i, 1, n+1)
 
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
 
struct IOSetup{
	IOSetup(){
		cin.tie(0);
		ios::sync_with_stdio(0);
		cout<<fixed<<setprecision(12);
	}
} iosetup;
 
struct Edge{
    ll to, cost;
    Edge(ll t, ll c) : to(t), cost(c) { }
};
using Graph = vector<vector<Edge>>;
 
ll MODpow(ll x, ll n, ll mod){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n >>= 1;
	}
	return res;
}
 
const ll MOD=1e9+7;
const ll nmax=200000+10;

ll N;
Graph G(nmax);
 
ll solve(){
	vvl color(nmax,vl(60,-1));
	vl cnt1(60,0);
	vl seen(nmax,0);
    queue<ll> q;
    q.push(0);
	REP(i,60) color[0][i]=0;
    
    while(!q.empty()){
        ll v=q.front();
        q.pop();
        seen[v]=1;
        
        for(auto e:G[v]){
            if(seen[e.to]) continue;
            
			ll et=e.to, ec=e.cost;
			q.push(e.to);
			REP(i,60){
				bool eci=ec&1;
				ec>>=1;
				color[et][i]=color[v][i]^eci;
				if(color[et][i]) cnt1[i]++;
			}
        }
    }
	
	ll res=0,rec=1;
	REP(i,60){
		ll mid1=rec;
		rec=rec*2%MOD;
		ll mid2=(N-cnt1[i])*cnt1[i]%MOD;
		ll mid=mid1*mid2%MOD;
		res+=mid;
		res%=MOD;
	}
    
    return res;
}
 
int main(void){
	scanf(" %ld",&N);
    REP(i,N-1){
        ll u,v,w; scanf(" %ld%ld%ld",&u,&v,&w);
        u--,v--;
        G[u].push_back(Edge{v,w});
        G[v].push_back(Edge{u,w});
    }
    
    ll res=solve();
    printf("%ld\n",res);
    
    return 0;
}