#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MOD = 1e9 + 7;

void solve() {
    int t;
    if (!(cin >> t)) return;
    
    vector<pair<int, int>> queries(t);
    int max_n = 0;
    
    for (int i = 0; i < t; i++) {
        cin >> queries[i].first >> queries[i].second;
        max_n = max(max_n, queries[i].first);
    }
    
    // DP table: dp[i][j] is the number of permutations of length i+1 
    // (using 0 to i) with exactly j distinct MEX values.
    vector<vector<long long>> dp(max_n + 1, vector<long long>(max_n + 3, 0));
    
    if (max_n >= 2) {
        dp[1][3] = 2; // Base case: For N=2, the permutations {0,1} and {1,0} both have 3 distinct MEXes.
        
        for (int i = 2; i < max_n; i++) {
            for (int j = 3; j <= i + 2; j++) {
                dp[i][j] = (dp[i-1][j] * (i - 1) % MOD + 2 * dp[i-1][j-1] % MOD) % MOD;
            }
        }
    }
    
    for (int i = 0; i < t; i++) {
        int n = queries[i].first;
        int k = queries[i].second;
        
        if (n == 1) {
            // For N=1, the only permutation is {0}, which has exactly 1 distinct MEX value (1).
            if (k == 1) cout << 1 << "\n";
            else cout << 0 << "\n";
        } else {
            if (k > n + 1) cout << 0 << "\n";
            else cout << dp[n-1][k] << "\n";
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    return 0;
}