#include<bits/stdc++.h>
using namespace std;
long long dp1[200010],dp2[200010],tr[200010],n,m,t,ans=0,vis[200010];
vector<long long>f[200010];
void change(long long x){
    for(long long i=x;i<=m;i+=i&-i)
    tr[i]++;
}
long long ask(long long x){
    long long s=0;
    for(long long i=x;i;i-=i&-i)
    s=s+tr[i];
    return s;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m>>t;
    long long s1=n+1,s2=m+1;
    for(long long i=1;i<=n;i++)
    dp1[i]=m+1;
    for(long long i=1;i<=m;i++)
    dp2[i]=n+1;
    for(long long i=1;i<=t;i++){
        long long x,y;
        cin>>x>>y;
        dp1[x]=min(dp1[x],y);
        dp2[y]=min(dp2[y],x);
        f[x].push_back(y);
    }
    for(long long i=1;i<dp2[1];i++)
    ans=ans+dp1[i]-1;
    for(long long j=1;j<dp1[1];j++)
    ans=ans+dp2[j]-1;
    for(long long i=1;i<dp2[1];i++){
        for(long long j=0;j<f[i].size();j++){
            long long tt=f[i][j];
            if(!vis[tt]){
                vis[tt]=true;
                change(tt);
            }
        }
        long long tt=min(dp1[i],dp1[1]-1);
        ans=ans-(tt-ask(tt));
    }
    cout<<ans<<"\n";
    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 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){
    cin>>N;
    REP(i,N-1){
        ll u,v,w; cin>>u>>v>>w;
        u--,v--;
        G[u].push_back(Edge{v,w});
        G[v].push_back(Edge{u,w});
    }
    
    ll res=solve();
    cout<<res<<endl;
    
    return 0;
}
