import std.bigint,std.conv,std.stdio,std.string;int M=10^^9+7;long f(int n){long z;foreach(c; ` !.G>?!<=EOI$$BIAKFV2=J$D7O)*(9B'JQE#[4K'T!N$I7?(30_+!]T"8V2"#@@[?6Y0CT8W6OWJ-22ITZ)/[58G#Z2Y#26%)WLV>JZ^@QU,4*QP3PG'2C"TBG;*[WDE=Y8--_W20C=9V-N:+(C?-?J3D6 '$;%J^%:%9^XY8%:"S/]!Z)Y6/DZ#%"IO*;]Z%Y@F6'9^?VN.@=E]C!C.\\*E'/X'U8CTE0=@/QX.OFA/6'L*4&)FD,"P4MQ;SV__*ALBK?/3MN%$YF_ ZAONM-$%@%$LT=.?O<3@SWK J.SZ,)?44^G8R[P"1?8%^SP"27W@=]#Z+WUWUFJDI;XE/UM!74H926/>P?FV&_71TTHHP*.#*(CHKB&,[)X([\;D*$8_Q?C,_'>66'O!RP;9"X\[[$?2 QM^23Z@9 )Z[=3.I%P5:90[W]-$C]BLU9/<*J,=0ID \OYO:[*)KPO,F=S66C-(^')]U3_[::@=3W*$/:(*)YB1H]G2I'3DY5AZ(!32N&D)M$SH:31[@3^;#8 H F *A?I0YQ#OML((Z/XDR'K)0_:E]W=&IYZ9V@9#C)RO]V=7J#<"VXRXCXOOLZ:VV;JA9[&9C*J?B:2FAK$)TB;#\'^51FSE)CSO;3WT"E?G'V &;]@D0\34C` [n/9^^7*5..$][0..5])z=z<<6|c-32;for(;n%9^^7;)z=z*n--%M;return z;} void main(){foreach(t;0..readln.chomp.to!int){auto s=readln.split;BigInt C=s[0],P=s[1],r=C+1-P,l=r-P;long a;if(l>=0&&l/M==r/M){a=f(r%M);for(long e=M-2,b=f(l%M);e;e/=2,b=b*b%M)if(e&1)a=a*b%M;}a.writeln;}}