#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define debugg(x) cout<<#x<<'='<<x<<endl;
#define debug1(x,y,z) cout<<#x<<' '<<x<<' '<<#y<<' '<<y<<' '<<#z<<' '<<z<<endl;
#define debug cout<<endl<<"********"<<endl;
#define ll long long
#define ull unsigned long long
#define ding cerr<<endl<<"Time: "<<(clock() * 1000. / CLOCKS_PER_SEC)<<" ms"<<endl;
#define ld long double 
#define itn int
#define pi (int)(3.1415926536)
#define pii pair<int,int>
#define rep(I, A, B) for (int I = (A); I <= (B); ++I)
#define dwn(I, A, B) for (int I = (A); I >= (B); --I)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define mod (ll)(1e9+7)
#define mod1 (ll)(1e9)
#define int ll
void fre(){
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
}
void fc(){fclose(stdin);fclose(stdout);}
inline ll ccin(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
inline void cou(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) cou(x/10);
    putchar(x%10+'0');
}



const int maxn=2e5+10;
const ll inf=1e16;
const int maxm=5e5+10;

int n;
struct node{
    int v;int w;
};
vector<node> yuan[maxn];
int zhi[maxn],cnt0[70],cnt1[70];
void dfs(int now,int fa,int val){
    zhi[now]=val;
    for(int i=0;i<yuan[now].size();i++){
        int v=yuan[now][i].v;
        if(v==fa) continue;
        dfs(v,now,val^yuan[now][i].w);
    }
}

signed main(){
    ios;
    cin>>n;
    for(int i=1;i<n;i++){
        int a1,a2,a3;cin>>a1>>a2>>a3;
        yuan[a1].push_back((node){a2,a3});
        yuan[a2].push_back((node){a1,a3});
    }
    dfs(1,0,0);
    for(int i=0;i<=61;i++){
        int now=(1LL<<i);
        for(int j=1;j<=n;j++){
            if(now&zhi[j]) cnt1[i]++;
            else cnt0[i]++;
        }
    }
    ll ans=0;
    for(int i=0;i<=61;i++){
        ll now=(1LL<<i)%mod;
        ll cnt=cnt1[i]*cnt0[i]%mod;
        ans=(ans+now*cnt)%mod;
    }
    cout<<ans<<endl;
    ding;return 0;
}
#include <iostream>
#include <cstring>
#include <cstdio>
#define LL long long
#define mod 1000000007

using namespace std;

int T;
LL n,a,b;

inline LL cal(LL n,LL a)
{
    return (n-a+1)*(n-a+1)%mod;
}

LL pow_mod(LL a,LL b)
{
    LL ret=1;
    while (b)
    {
        if (b&1) ret=ret*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return ret;
}

LL inv2;

int main()
{
    inv2=pow_mod(2,mod-2);
    scanf("%d",&T);
    while (T--)
    {
        scanf("%lld%lld%lld",&n,&a,&b);
        if (a<b) swap(a,b);
        LL ans=cal(n,a)*cal(n,b)%mod;
        if (b==1)
        {
            LL tmp=cal(n,a)*cal(a,b)%mod;
            ans-=tmp;
            if (ans<0) ans+=mod;
        }
        else
        {
            ans-=(cal(n,a)*cal(a,b)%mod);
            if (ans<0) ans+=mod;
            LL h=min(a+b-1,n);
            LL p1=(a-b+1)*(n-a+1)%mod;
            LL p2=(n+1)*(h-a)%mod;
            LL p3=(a+1+h)%mod*(h-a)%mod*inv2%mod;
            p1=4LL*p1%mod*(p2-p3+mod)%mod;
            ans-=p1;
            if (ans<0) ans+=mod;
            p1=(p2-p3)%mod;
            p1=p1*p1%mod*4%mod;
            ans-=p1;
            if (ans<0) ans+=mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
