We need to understand the problem. We have K distinct characters (alphabet size K). The password length is N. The password is composed only of the K characters belonging to the special language. However, each character can be used at most once. So the password is a string of length N, with characters chosen from the K distinct characters, without repetition. That is essentially: number of injective functions from N positions to K characters, i.e., number of permutations of K taken N at a time, but also order matters; that is, we need to count all sequences of length N with distinct characters from a set of size K. That number is P(K, N) = K! / (K-N)!. However, careful: The password is a string, order matters. Yes, that's exactly the number of permutations of K taken N at a time. So answer = P(K,N) mod 1e9+7.
But wait: The note says: "You need to print the value of each element and not their weight." That note may be from some other problem? Possibly irrelevant.
Sample: N=3, K=3 -> P(3,3)=3!=6 matches.
Constraints: 1 ≤ N ≤ K ≤ 1e5, T up to 10. So K can be as large as 100k. We need compute K! / (K-N)! mod MOD = product_{i=K-N+1}^{K} i mod MOD. Since MOD=1e9+7 is prime, we can compute directly as product of a range. But K up to 1e5, product of up to 1e5 numbers mod mod is fine. However, T up to 10, and we may need to handle multiple queries efficiently. But K and N may vary. Since each product range can be computed individually in O(N) time, but N can be up to 1e5, and T up to 10, worst-case O(1e6) which is fine. But perhaps we can precompute factorials up to max K across test cases (max K ≤ 1e5) and then answer = fact[K] * inv_fact[K-N] % MOD. That would be O(maxK) preprocessing, then O(1) per query. That is efficient. However, note we need modular inverses. Since MOD prime, we can precompute factorials and inverse factorials using Fermat's little theorem. That is straightforward.
Thus answer = factorial[K] * inv_factorial[K-N] % MOD.
But check: N=0? Constraint says N ≥ 1, so no zero-length password. Good.
Edge Cases: N=K => product = K!; N=1 => product = K.
Implementation: For each test case, read N and K. Compute result = fact[K] * inv_fact[K-N] % MOD.
But we must ensure we precompute factorial up to maxK among all test cases. Since K <= 1e5, we can precompute up to 100000 (or maybe a bit higher). The problem constraints: K ≤ 10^5 exactly. So we can set MAX = 100000. But we can read all test cases first, find maxK, then precompute up to that.
Alternatively, compute on the fly: but factorial precomputation is easy.
So algorithm:
- MOD = 10^9+7
- Read T.
- For each test case, read N, K. Store them, also track maxK.
- Precompute factorial[0..maxK] and inv_factorial[0..maxK].
- factorial[0]=1
- for i=1..maxK: factorial[i]=factorial[i-1]*i % MOD
- inv_factorial[maxK] = powmod(factorial[maxK], MOD-2, MOD)
- for i=maxK-1 downto 0: inv_factorial[i]=inv_factorial[i+1]*(i+1)%MOD
- For each test case: answer = factorial[K] * inv_factorial[K-N] % MOD
- Print answer.
We must consider if there is any twist: "Each character belonging to the special language has been used at max once in the secret code." That means characters can be unused, but cannot be repeated. So exactly an N-length permutation of K characters. So it's as we thought.
Thus code is simple.
But let me check sample: T=1, N=3, K=3 => factorial[3]=6, inv_fact[0]=1 => 6 => prints 6.
Double-check large numbers: MOD is prime 1000000007.
One nuance: K can be up to 1e5, but N can be up to K. So K-N could be 0. We have inv_fact[0] defined as 1. Good.
Thus we can implement.
I will now produce the final answer in Python.
Since T up to 10 and K up to 1e5, Python should be fine.
We must ensure we use fast exponentiation for modular inverse, but the method using factorial and inv factorial is O(maxK) precomputation and O(1) per query.
Implementation in Python:
def solve():
import sys
MOD = 10**9+7
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
T = int(next(it))
cases = []
maxK = 0
for _ in range(T):
N = int(next(it)); K = int(next(it))
cases.append((N,K))
if K > maxK:
maxK = K
# precompute factorial and inv factorial up to maxK
fact = [1]*(maxK+1)
for i in range(1, maxK+1):
fact[i] = fact[i-1]*i % MOD
inv_fact = [1]*(maxK+1)
# compute inv factorial using Fermat: inv_fact[maxK] = pow(fact[maxK], MOD-2, MOD)
inv_fact[maxK] = pow(fact[maxK], MOD-2, MOD)
for i in range(maxK, 0, -1):
inv_fact[i-1] = inv_fact[i]*i % MOD
out = []
for N, K in cases:
ans = fact[K] * inv_fact[K-N] % MOD
out.append(str(ans))
sys.stdout.write("\n".join(out))
But careful: In the loop to compute inv_fact, we start from maxK down to 1: inv_fact[i-1] = inv_fact[i] * i % MOD. Yes.
Alternatively, we could compute product directly. But precomputing is fine.
Given constraints, this works.
Now I'd like to test with sample:
Input:
1
3 3
maxK=3, fact=[1,1,2,6]; inv_fact[3]=pow(6,MOD-2)=166666668? Actually 6^-1 mod 1e9+7 = 166666668. Then inv_fact[2] = inv_fact[3]3 % MOD = 1666666683=500000004 mod? Actually 166666668*3=500000004, which is 2 inverse? But anyway. inv_fact[0]=1. ans = 6 * 1 =6. prints 6.
Ok.
Edge: When K is large, but we precompute up to 100k, okay.
Now check if there is any misinterpretation: The password must be composed only of the K characters belonging to the special language. And each character used at most once. That means we cannot use a character more than once. But it doesn't require that we use all K characters; we use exactly N distinct characters from the K. So permutations of length N from K. So correct.
Thus answer.
We could also directly compute product_{i=K-N+1}^{K} i mod MOD. That might be simpler, but precomputation is fine.
But note: The note "You need to print the value of each element and not their weight." is likely a stray from another problem. So ignore.
Hence final answer.
But the problem statement originally might be from a contest; some may think it's about combinations but careful: order matters. So permutations.
Now I will produce final solution.
But ensure we handle the case when N > K? Constraint says N ≤ K, so fine.
We'll provide explanation as well.
Now I'll write the final answer. ```python
import sys
MOD = 10**9 + 7
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
T = int(next(it))
cases = []
maxK = 0
for _ in range(T):
N = int(next(it))
K = int(next(it))
cases.append((N, K))
maxK = max(maxK, K)
# precompute factorials up to maxK
fact = [1] * (maxK + 1)
for i in range(1, maxK + 1):
fact[i] = fact[i-1] * i % MOD
# precompute inverse factorials
inv_fact = [1] * (maxK + 1)
inv_fact[maxK] = pow(fact[maxK], MOD - 2, MOD) # Fermat
for i in range(maxK, 0, -1):
inv_fact[i-1] = inv_fact[i] * i % MOD
out_lines = []
for N, K in cases:
ans = fact[K] * inv_fact[K - N] % MOD
out_lines.append(str(ans))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()