When we remove a contiguous subarray of length , the array shrinks, but the relative positions of the remaining elements follow a strict mathematical rule.
When a subarray of length is removed starting at some index , any element originally at index (where j > i+k) shifts left by exactly units.
Because the shift is exactly , the property j \pmod k remains unchanged.
This means the element that ends up at the 1^{st} position of the final array must have originally been at an index i_1 \equiv 1 \pmod k.
Generally, the element at the j^{th} position of the final array must have originally been at an index i_j \equiv j \pmod k.
Let be the initial length and be the removal block size.
The final length will be r = n \pmod k.
Let q = \lfloor n/k \rfloor be the total number of blocks we can remove.
We need to choose indices (i_1, i_2, \dots, i_r) from the original such that:
1 \le i_1 < i_2 < \dots < i_r \le n (Relative order is preserved).
i_j \equiv j \pmod k for all .
To satisfy the modulo condition, we can rewrite each original index as:
Where a_j is the number of -sized blocks skipped or deleted to the left of the j^{th} element.
Substituting this into our ordering constraint (i_j < i_{j+1}):
And for the boundary (i_r \le n):
This reduces the problem to choosing a non-decreasing sequence of integers where each a_j \in [0, q].
This is a classic "Stars and Bars" problem (choosing items from q+1 possible values with replacement). The number of ways is:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 998244353 ;
const ll N = 1e6 + 5;
vector <ll> factorial(N);
vector <ll> invfact(N);
ll binpow(ll a , ll b) {
ll ans = 1;
ll index = a;
while (b) {
if (b & 1) {
ans = (ans * index) % mod;
}
index = (index * 1LL * index) % mod;
b >>= 1;
}
return ans % mod;
}
void fillfact() {
factorial[0] = 1;
for (int i = 1; i < N; i++) {
factorial[i] = (factorial[i - 1] * i) % mod;
}
}
void fillinvfact() {
invfact[N - 1] = (binpow(factorial[N - 1] , mod - 2)) % mod;
for (int i = N - 2; i >= 0; i--) {
invfact[i] = ((i + 1) * 1LL * invfact[i + 1]) % mod;
}
}
ll ncr(int n , int r) {
if (n < r) return 0;
if (n < 0 || r < 0) return 0;
ll ans = (factorial[n] * 1LL * invfact[n - r]) % mod;
ans = (ans * invfact[r]) % mod;
return ans % mod;
}
void solve() {
int n , k ;
cin >> n >> k ;
int sum = (n % k) ;
int f = ((n + k - 1) / k) ;
cout << ncr(sum + f - 1 , sum) << endl ;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("output.txt", "w", stdout);
// freopen("input.txt", "r", stdin);
int t;
cin >> t ;
fillfact() ;
fillinvfact() ;
while (t--) {
solve();
}
return 0;
}