#include <cmath>
#include <cstdint>
#include <iostream>
#include <istream>
#include <tuple>

using ll = int64_t;

constexpr ll maxn = 1e6,p=1e9+7;
ll fact[maxn+5],invfact[maxn+5],der[maxn+5];

ll qpow(ll b,ll e){
    ll res{1};
    while(e){
        if(e&1)res=res*b%p;
        b = b*b%p;
        e/=2;
    }
    return res;
}

int main(){
    std::iostream::sync_with_stdio(false),std::cin.tie(nullptr),std::cout.tie(nullptr);

    fact[1]=1;
    for(ll i{2};i<=maxn;i++){
        fact[i]=fact[i-1]*i%p;
    }
    invfact[maxn] = qpow(fact[maxn], p-2);
    for(ll i{maxn-1};i>=0;i--){
        invfact[i]=invfact[i+1]*(i+1)%p;
    }
    der[0]=1;
    der[1]=0;
    for(ll i{2};i<=maxn;i++){
        der[i] = (i-1)*((der[i-1]+der[i-2])%p)%p;
    }

    ll T;
    std::cin>>T;

    while(T--){
        const auto [n,m] = []()->std::tuple<ll,ll>{
            ll n,m;
            std::cin>>n>>m;
            return std::make_tuple(n,m);
        }();
        const ll C {fact[n]*invfact[m]%p*invfact[n-m]%p};
        std::cout<<(der[n-m]*C%p)<<'\n';
    }
}