/*
问题的关键是在于求在x1+x2+..+xk=n的解中
t出现了多少次,t出现的次数是k*C(n-k-1,k-1).
然后快速幂即可
*/
#include <cstdio>
#include <cstring>
#define LL long long
#define MAX 2200
const int MOD = 1e9 + 7;
int C[2020][2020];
void init()
{
C[0][0] = 1;
for(int i = 1;i < 2020;i++)
{
C[i][0] = C[i][i] = 1;
for(int j = 1;j < i;j++)
{
C[i][j] = C[i-1][j] + C[i-1][j-1];
if(C[i][j] >= MOD-1)
C[i][j] -= MOD-1;
}
}
}
LL pow_mod(LL a,LL n,LL mod)
{
LL ret = 1;
LL tmp = a%mod;
while(n)
{
if(n&1)
{
ret = ret*tmp%mod;
}
tmp = tmp*tmp%mod;
n >>= 1;
}
return ret;
}
int main()
{
int T;
scanf("%d",&T);
init();
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
if(n==1)
{
printf("%d\n",m%MOD);
continue;
}
LL ans=1;
for(int i=2;i<=m;i++)
{
ans=(ans*pow_mod(i,((LL)n*C[m-i+n-2][n-2])%(MOD-1),MOD))%MOD;
}
printf("%lld\n",ans);
}
return 0;
}