#include<bits/stdc++.h>
using namespace std;
#define int long long
const int NN=1e9+7;
int n,c;
int p[20000100],ni[20000100];
int cc(int x,int y){
if(x<0||y<0||x<y)return 0;
return p[x]*ni[ y]%NN*ni[x-y]%NN;
}
int mi(int x,int y){
int sum=1;
for (;y;y>>=1,x=x*x%NN)
if(y&1)sum=sum*x%NN;
return sum;
}
void solve(){
cin>>n>>c;//n*=2;
p[0]=1;
for (int i=1;i<=2*n;i++)
p[i]=p[i-1]*i%NN;
ni[2*n]=mi(p[2*n],NN-2);
for (int i=2*n;i;i--)
ni[i-1]=ni[i]*i%NN;
int ans=0;
int pp=1;
for (int i=0;i<n;i++){
ans=(ans+cc(n*2,i)*pp[i]%NN*c%NN*(n-i))%NN;
pp=pp*(c-1)%NN;
}
cout<<ans*mi(n,NN-2)%NN<<"\n";
}
signed main(){
int T;cin>>T;
while(T--)solve();
}