#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod= 1000000007 ;
vector< ll> fact( 1e5 + 1 , 1 ) ;
ll modInv( ll a, ll e) {
ll res= 1 ;
while ( e> 0 ) {
if ( e% 2 ) {
res= ( res* a) % mod;
e-- ;
}
else {
a= ( a* a) % mod;
e/ = 2 ;
}
}
return res;
}
ll nCr( ll n, ll r) {
ll invR = modInv( fact[ r] , mod- 2 ) ;
ll invNR = modInv( fact[ n- r] , mod- 2 ) ;
ll denom = ( invR * invNR) % mod;
return ( fact[ n] * denom) % mod;
}
int main( ) {
for ( int i= 1 ; i<= 1e5 ; i++ ) fact[ i] = ( fact[ i- 1 ] * i) % mod;
int t; cin >> t;
while ( t-- ) {
ll n, k; cin >> n>> k;
ll p= 0LL;
for ( int i= 1 ; i<= n; i++ ) {
int nxt= k- i;
if ( nxt>= 1 && nxt< i) p++ ;
}
ll trail= 1 , res= 0LL;
for ( ll i= 0 ; i< p; i++ ) {
ll d = ( i % 2 ) ? - 1 : 1 ;
trail = ( trail * 2 ) % mod;
ll ways = nCr( p, i + 1 ) ;
ll term = ( ways * trail) % mod;
term = ( term * fact[ n - i - 1 ] ) % mod;
term = ( d * term + mod) % mod;
res = ( res + term) % mod;
}
cout << res << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKY29uc3QgaW50IG1vZD0xMDAwMDAwMDA3Owp2ZWN0b3I8bGw+IGZhY3QoMWU1KzEsIDEpOwoKbGwgbW9kSW52KGxsIGEsIGxsIGUpewogICAgbGwgcmVzPTE7CiAgICB3aGlsZShlPjApewogICAgICAgIGlmIChlJTIpewogICAgICAgICAgICByZXM9KHJlcyphKSVtb2Q7CiAgICAgICAgICAgIGUtLTsKICAgICAgICB9CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgYT0oYSphKSVtb2Q7CiAgICAgICAgICAgIGUvPTI7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIHJlczsKfQpsbCBuQ3IobGwgbiwgbGwgcil7CiAgICBsbCBpbnZSID0gbW9kSW52KGZhY3Rbcl0sIG1vZC0yKTsgICAgICAgCiAgICBsbCBpbnZOUiA9IG1vZEludihmYWN0W24tcl0sIG1vZC0yKTsgIAogICAgbGwgZGVub20gPSAoaW52UiAqIGludk5SKSAlIG1vZDsgICAgIAogICAgcmV0dXJuIChmYWN0W25dICogZGVub20pICUgbW9kOwp9CmludCBtYWluKCkgewogICAgZm9yKGludCBpPTE7IGk8PTFlNTsgaSsrKSBmYWN0W2ldPShmYWN0W2ktMV0qaSklbW9kOwoJaW50IHQ7IGNpbj4+dDsKCXdoaWxlKHQtLSl7CgkgICAgbGwgbiwgazsgY2luPj5uPj5rOwoJICAgIGxsIHA9MExMOwoJICAgIGZvcihpbnQgaT0xOyBpPD1uOyBpKyspewoJICAgICAgICBpbnQgbnh0PWstaTsKCSAgICAgICAgaWYgKG54dD49MSAmJiBueHQ8aSkgcCsrOwoJICAgIH0KCSAgICBsbCB0cmFpbD0xLCByZXM9MExMOwoJICAgIGZvcihsbCBpPTA7IGk8cDsgaSsrKXsKCSAgICAgICAgbGwgZCA9IChpICUgMikgPyAtMSA6IDE7CiAgICAgICAgICAgIHRyYWlsID0gKHRyYWlsICogMikgJSBtb2Q7IAogICAgICAgICAgICBsbCB3YXlzID0gbkNyKHAsIGkgKyAxKTsKICAgICAgICAgICAgbGwgdGVybSA9ICh3YXlzICogdHJhaWwpICUgbW9kOwogICAgICAgICAgICB0ZXJtID0gKHRlcm0gKiBmYWN0W24gLSBpIC0gMV0pICUgbW9kOyAKICAgICAgICAgICAgdGVybSA9IChkICogdGVybSArIG1vZCkgJSBtb2Q7CiAgICAgICAgICAgIHJlcyA9IChyZXMgKyB0ZXJtKSAlIG1vZDsgCgkgICAgfQoJICAgIGNvdXQgPDwgcmVzIDw8IGVuZGw7Cgl9Cgp9Cg==