Permutation Problem
Difficulty: Hard

How many n-digit numbers (without leading zeros) are there such that no digit occurs more than k times?  

As the count of such n-digit numbers can be very large, print the answer mod (10<sup>9</sup>+7)  

**Input Format**  

The first line contains an integer, _T_ , the number of test cases. This is followed by _T_ lines each containing 2 space separated integers, _n_ and _k_  


**Constraints**  

_T_ &le; 100000  
1 &le; _n_ &le; 1000  
1 &le; _k_ &le; 10<sup>9</sup>

**Sample Input**

    2
    2 3
    2 1

**Sample Output**

    90
    81

**Explanation**  
Case 1: A number can appear three times. So we have 9 (all except 0) numbers for first digit and 10 numbers for the second digit.  
Case 2: A number can appear only once. So we have 9 choices for the first digit and 9(all except the first one) for the second digit. 



Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:
#
# Complete the 'solve' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER n
#  2. INTEGER k
#

def solve(n, k):
    # Write your code here




Reference solution (C++):
###C++  
```cpp  
#include <stdio.h>
#include <stdlib.h>
#define mod 1000000007
#define min(a,b) ((a)<(b)?(a):(b))
#define LIM 1000
#define ull unsigned long long
#define _9_10 300000003 // (9/10'ths mod 10^9+7)

ull fac[LIM+1];
ull ifc[LIM+1];
ull **F[11];

int main() {
    // precalculate factorials and inverse factorials
    fac[0] = ifc[0] = fac[1] = ifc[1] = 1;
    for (int i = 2; i <= LIM; i++) { // put the inverses in ifc
        ifc[i] = (mod - mod/i) * ifc[mod%i] % mod;
    }
    for (int i = 2; i <= LIM; i++) { // precalculate fac and ifc
        fac[i] = fac[i-1] *     i  % mod;
        ifc[i] = ifc[i-1] * ifc[i] % mod;
    }

    // precalculate F[d][n][k] = f_d(n,k)
    // around 10^2/2 * 600^2/2 < 10000000 operations
    for (int d = 0; d <= 10; d++) {
        F[d] = (ull**)malloc((LIM+1)*sizeof(ull*));
        for (int n = 0; n <= LIM; n++) {
            F[d][n] = (ull*)calloc(n+1, sizeof(ull));
            for (int k = 0; k <= n; k++) {
                if (n == 0) {
                    F[d][n][k] = 1;
                } else if (k == 0 or d == 0) {
                    F[d][n][k] = 0;
                } else {
                    ull s = 0;
                    ull pw = 1;
                    int nn = n;
                    int nk = k-1;
                    for (int e = 0; e <= d; e++) {
                        if (e*k > n) break;
                        s += fac[d] * ifc[e] % mod * ifc[d-e] % mod * fac[n] % mod * ifc[nn] % mod * pw % mod * F[d-e][nn][min(nn,nk)];
                        pw = pw * ifc[k] % mod;
                        nn -= k;
                    }
                    F[d][n][k] = s % mod;
                }
            }
        }
    }



    ull **f = F[10];
    int z;
    scanf("%d", &z);
    for (int cas = 1; cas <= z; cas++) {
        int n, k;
        scanf("%d%d", &n, &k);
        ull ans = f[n][min(n, k)] * _9_10 % mod; // 1/10'ths of the numbers start with zero
        printf("%llu\n", ans);
    }
}
```