We are given N blocks in a row, each painted with one of M colors (1..M). Two ways are different if any block color differs. Not mandatory to use all colors. Condition: There may be at most K pairs of adjacent blocks that are painted in the same color. i.e., the number of indices i (1 <= i < N) such that color_i = color_{i+1} is ≤ K. So we count colorings where the number of adjacent equal pairs (also called "bad" pairs, but not really; we just want at most K) is at most K.
We need count modulo MOD = 998244353.
Constraints: N,M up to 2e5, K up to N-1. So O(N log N) or O(N) or O(K log N) etc.
We need to count the number of colorings with exactly t equal adjacent pairs, then sum for t=0..K.
Let’s denote the number of colorings where there are exactly t adjacent equal pairs.
We can think of the sequence of colors, length N. Let’s denote the positions where color changes. Alternatively, we can think of runs: a sequence of consecutive blocks with the same color. If there are exactly r runs (i.e., r segments of constant color), then the number of adjacent equal pairs is N - r (since each run reduces adjacent pair count: if r runs, then there are r-1 boundaries between runs where colors differ, so equal pairs = (N-1) - (r-1) = N - r). Indeed, total adjacent pairs = N-1, and each boundary between runs is a place where colors differ, so #equal pairs = (N-1) - (r-1) = N - r. So t = N - r, or r = N - t.
Thus, counting colorings with exactly t equal pairs is equivalent to counting colorings with exactly r = N - t runs. For a given r runs, we need to assign colors to each run such that consecutive runs have different colors (by definition of runs, adjacent runs have different colors). And we need to choose the lengths of each run positive integers that sum to N, and there are r runs.
So the number of sequences with exactly r runs = (number of ways to split N into r positive integers) * (number of ways to assign colors to runs such that adjacent runs are different). Since runs are defined as maximal same-color segments, the condition is automatically satisfied that consecutive runs have different colors. But also note that runs themselves are unlabeled except for their order; they are part of the sequence.
Thus, number = (choose composition of N into r positive parts) * (M * (M-1)^{r-1}) because: first run can be any of M colors; each subsequent run must be different from previous, so (M-1) choices.
But careful: Are we overcounting? For a given coloring, the runs are uniquely defined. And any such composition and color assignment yields a valid coloring with exactly r runs. So indeed count = C(N-1, r-1) * M * (M-1)^{r-1}, where C(N-1, r-1) is the number of ways to place r-1 cut points among N-1 gaps to split into r positive parts.
But is that all? Let's check: if M=1, then (M-1)^{r-1} is zero for r>=2, but then coloring with r>1 is impossible because you cannot have two runs with different colors. However formula would give zero, which is correct. For M=1, only coloring all same color exists. In that case, runs =1 when N>=1. The number of equal pairs = N-1. So if K >= N-1, answer=1. But if M=1 and K < N-1, answer=0. Our formula should handle: For M=1, (M-1)=0, and M * (M-1)^{r-1} = 0 for r>1, and for r=1, it's M * (M-1)^0 = 1. So sum over r = N - t, t from 0 to K => r from N-K to N (since t <= K => r >= N-K). For M=1, only r=1 possible (if N>=1). So if N-K <=1 <= N i.e., if K >= N-1 then r=1 is included => answer=1, else 0. Good.
Now for M >=2, formula works.
Thus, the answer = sum_{t=0}^{K} f(N, M, t) where f = number with exactly t equal pairs = C(N-1, N - t - 1) * M * (M-1)^{N - t - 1}? Let's express in terms of t:
r = N - t. So number = C(N-1, r-1) * M * (M-1)^{r-1} = C(N-1, N - t - 1) * M * (M-1)^{N - t - 1}. But C(N-1, N - t - 1) = C(N-1, t) because choose t positions among N-1 to be equal pairs? Actually careful: C(N-1, r-1) with r=N-t => r-1 = N-t-1. So it's C(N-1, N-t-1) = C(N-1, t) because choose t from N-1? Since N-1 - (N-t-1) = t. Yes C(N-1, N-t-1) = C(N-1, t). So simpler: f(t) = C(N-1, t) * M * (M-1)^{N - t - 1}. Because C(N-1, t) and (M-1)^{N-t-1}. Let's check: For t = N-1 (all equal pairs), then r = N - (N-1) = 1, so f = C(N-1, N-2?) Wait: t=N-1 => r=1 => r-1=0 => C(N-1,0)=1, and M * (M-1)^0 = M, but that counts coloring with one run (all same color) indeed there are M such colorings. Our formula using C(N-1, t) gives C(N-1, N-1)=1, times M * (M-1)^{N-(N-1)-1} = M * (M-1)^{0}=M. Good.
For t=0 (no equal adjacent pairs, i.e., alternating, but not necessarily alternating because colors can change arbitrarily but no two adjacent same), r=N, so number = C(N-1, N-1) =1? Actually r=N => r-1 = N-1 => C(N-1, N-1) =1, M * (M-1)^{N-1} = M * (M-1)^{N-1}. But is that correct? For N blocks, with no two adjacent same, number of colorings = M * (M-1)^{N-1} indeed. So f(0) = M * (M-1)^{N-1} matches.
Thus the formula is consistent.
So answer = sum_{t=0}^{K} C(N-1, t) * M * (M-1)^{N-t-1}.
We can factor M * (M-1)^{N-1} * sum_{t=0}^{K} C(N-1, t) * (M-1)^{-t} maybe? Because (M-1)^{N-t-1} = (M-1)^{N-1} * (M-1)^{-t}. So answer = M * (M-1)^{N-1} * sum_{t=0}^{K} C(N-1, t) * (M-1)^{-t}.
Let x = (M-1)^{-1} modulo MOD. Since MOD is prime, we can compute modular inverse. However (M-1) may be 0 mod MOD if M-1 is multiple of MOD? But MOD=998244353, M up to 2e5, so M-1 < MOD, not zero modulo MOD (unless M=998244354? But M <= 2e5, so safe). So we can compute (M-1)^{-1} mod MOD. Then answer = M * (M-1)^{N-1} * S, where S = sum_{t=0}^{K} C(N-1, t) * (M-1)^{-t}.
But careful: if M=1, then M-1 =0, the formula M * (M-1)^{N-1} is zero for N>1, but we need to treat M=1 separately as we discussed. For M=1, answer is 1 if K >= N-1 else 0. So we can handle M=1 separately.
For M>=2, this works.
Now we need to compute S efficiently for N up to 2e5 and K up to N-1. But K can be as large as N-1, so in worst case K = N-1, then sum is over t=0..N-1 of C(N-1,t) * x^t = (1+x)^{N-1}. And answer = M * (M-1)^{N-1} * (1 + 1/(M-1))^{N-1} = M * (M-1)^{N-1} * ((M-1+1)/(M-1))^{N-1} = M * M^{N-1} = M^N. Indeed total number of colorings is M^N, because sum over all t from 0 to N-1 should give M^N. Let's check: sum_{t=0}^{N-1} f(t) = M^N? Indeed each coloring counted exactly once by t. So it's consistent.
Thus we need to compute sum_{t=0}^{K} C(N-1, t) * a^t where a = (M-1)^{-1} (or possibly compute as sum of binomial terms with weight). This is like a truncated binomial sum.
Given N up to 2e5, we can precompute factorials and inverse factorials up to N-1. Then we can compute each term and sum from t=0 to K. That would be O(K) which could be up to 2e5, which is fine. Indeed N=2e5, K up to N-1=199999, O(N) is okay. So simplest is: precompute factorials up to N-1, inv factorials, and pow_a[t] = a^t (or compute on the fly). Then iterate t=0..K, compute binom = C(N-1, t), multiply by pow_a[t], add to sum. Complexity O(N). That's acceptable for 2e5.
But wait: there is also M factor and (M-1)^{N-1} factor. So we can compute:
- powM1 = (M-1)^{N-1} mod MOD.
- invM1 = inv(M-1) mod MOD.
- Compute a = invM1.
- Compute powA[t]? Actually we can compute term = C(N-1, t) * (invM1)^t mod MOD. Could compute cur = 1 for t=0, and update cur = cur * invM1 each step, but that would be incremental. Or we can compute term = binom * pow(invM1, t). Since K up to 2e5, we can just precompute powers up to K.
Alternatively, we can compute S incrementally: initialize s=0, cur_binom = 1 (since C(N-1,0)=1), cur_pow = 1 (a^0=1). For t from 0 to K:
s += cur_binom * cur_pow mod MOD
then update cur_pow = cur_pow * invM1 % MOD
and update cur_binom = cur_binom * (N-1 - t) / (t+1) mod MOD. But careful: to avoid division, we can use factorial anyway. But incremental method works with modular inverses for each step: cur_binom = cur_binom * (N-1 - t) % MOD * inv(t+1) % MOD. Since t up to 2e5, we can precompute inv numbers or compute on the fly using pow? Precomputing inv array up to N is okay. That would be O(N) and avoid large factorial arrays.
Either way fine.
Thus the total solution: For M >= 2:
- Compute invM1 = modinv(M-1).
- Compute powM1 = pow_mod(M-1, N-1).
- Compute S = sum_{t=0}^{K} C(N-1, t) * (invM1)^t mod MOD.
- Answer = M * powM1 % MOD * S % MOD.
Edge cases: When N=1? Then there are no adjacent pairs (N-1=0). The condition "at most K pairs" is always satisfied because there are 0 pairs, and K>=0. So answer should be M (each block can be any color). Check with formula: N=1, N-1=0, sum over t=0..K? But t can only be 0 because there are no adjacent pairs, and t must be 0 (since max pairs =0). So K can be anything >=0. Our formula: M * (M-1)^{0} * sum_{t=0}^{K} C(0, t) * (M-1)^{-t}. But C(0, t) is 1 if t=0, else 0 for t>0. So sum = 1 if K>=0, else? But K>=0 always, so sum=1. Answer = M * 1 * 1 = M. Works.
But careful: For N=1, our formula uses N-1=0, so factor (M-1)^{N-1} = (M-1)^0 =1. Good.
Now M=1: Handle separately: if K >= N-1, answer = 1, else 0.
Now test given examples:
Example1: N=3, M=2, K=1.
Compute: M=2, M-1=1, invM1 = 1, powM1 = 1^{2}=1.
S = sum_{t=0}^{1} C(2, t)1^t = C(2,0)+C(2,1)=1+2=3.
Answer = M * powM1 * S = 21*3=6. matches.
Example2: N=100, M=100, K=0.
We need sum for t=0 only: C(99,0)=1, (M-1)^{-0}=1 => S=1. powM1 = 99^{99} mod MOD. Answer = 100 * 99^{99} mod MOD. They say output 73074801. Let's verify: We can trust.
Example3: big numbers.
So solution seems straightforward: O(N) time.
But wait: constraints N, M up to 2e5, O(N) is fine. However we must be careful about modulo exponentiation and precomputation.
But is there any catch? The formula f(t) = C(N-1, t) * M * (M-1)^{N-t-1} only holds for M>=1? Let's verify derivation: number of colorings with exactly r runs = C(N-1, r-1) * M * (M-1)^{r-1}. This assumes that when selecting colors for runs, the first run can be any M, subsequent runs each have M-1 choices (any color except the immediate predecessor). This is correct because we require consecutive runs to be different; there's no further restriction. But there is a subtlety: When M=1, (M-1) =0, formula gives 0 for r>1, but we treat separately. For M>=2, it's fine.
But is it valid to say that the number of ways to assign colors to r runs with adjacent runs different equals M*(M-1)^{r-1}? This holds for any M. Yes.
So the formula stands.
Thus answer = M * sum_{r=N-K}^{N} C(N-1, r-1) * (M-1)^{r-1} where r runs = N-t. But that might be more direct: t = N - r, so t <= K => r >= N-K. So sum over r from max(1, N-K) to N. But also r cannot be less than 1, but N-K may be 0? If K>=N, but K<=N-1 per constraints? Actually constraint: 0 ≤ K ≤ N-1. So K max N-1, then N-K >=1. So r range is [1,N]? Actually if K=N-1, then N-K =1, so r from 1 to N. So all runs are covered. Good.
So we could also compute sum_{r=N-K}^{N} C(N-1, r-1) * M * (M-1)^{r-1}. But that's same.
Now check N up to 2e5, and K can be as large as N-1, then we need sum over all r=1..N (or t=0..N-1). That is O(N). So no problem.
Thus straightforward O(N) approach works.
But wait: The input can be large up to 2e5, but O(N) is fine. However note that if K is small, we still sum up to K terms. But K can be up to 2e5. So overall O(N) fine.
Now we need to precompute factorials up to N-1 (which is up to 199999) and inverse factorials, or compute binom incrementally. Factorial precomputation O(N) is fine.
Now implement:
MOD = 998244353.
Read N, M, K.
If M == 1:
if K >= N-1: print(1)
else: print(0)
return
Else:
invM1 = pow(M-1, MOD-2, MOD)
powM1 = pow(M-1, N-1, MOD)
Precompute factorials fact[0..n_minus_1] where nm = N-1.
Also invfact. Or use incremental method.
We'll compute S = 0.
For t from 0 to K:
binom = C(nm, t) mod MOD
term = binom * pow(invM1, t, MOD) % MOD
S = (S + term) % MOD
But pow(invM1, t) inside loop would be O(log MOD) per t if computed naively; better compute powers iteratively: powInv = 1; for t=0..K: use powInv then update powInv = powInv * invM1 % MOD.
Also compute binom iteratively: binom = 1 for t=0; then for next t: binom = binom * (nm - t) % MOD * inv(t+1) % MOD. We'll need modular inverses of numbers 1..K+1 maybe. Precompute inv array up to nm (or up to K) using linear method: inv[i] = MOD - MOD//i * inv[MOD%i] % MOD. That is O(n). Or we can compute binom using factorial, which also requires invfact up to nm. Factorial method: compute fact and invfact, then binom = fact[nm] * invfact[t] % MOD * invfact[nm - t] % MOD. This also requires precomputation O(nm). Both are fine. Factorial method is simpler if we have precompute functions.
Given N up to 2e5, precomputing fact up to N-1 is fine.
Let's do factorial method:
nm = N-1
fact = [1]*(nm+1)
for i in range(1, nm+1): fact[i] = fact[i-1]i % MOD
invfact = [1](nm+1)
invfact[nm] = pow(fact[nm], MOD-2, MOD)
for i in range(nm,0,-1): invfact[i-1] = invfact[i]*i % MOD
Then C(nm, t) = fact[nm] * invfact[t] % MOD * invfact[nm-t] % MOD.
Now compute S:
invM1 = modinv(M-1)
powInv = 1
S = 0
for t in range(0, K+1):
if t > nm: break? but K <= N-1, so t <= nm. So ok.
binom = fact[nm] * invfact[t] % MOD * invfact[nm - t] % MOD
term = binom * powInv % MOD
S = (S + term) % MOD
powInv = powInv * invM1 % MOD
Then ans = M * powM1 % MOD * S % MOD
But check: If K >= nm, we sum t=0..nm, that's all t, and S should equal (1 + invM1)^{nm} = ( (M-1+1)/(M-1) )^{nm} = (M/(M-1))^{nm}. Then ans = M * (M-1)^{nm} * (M/(M-1))^{nm} = M * M^{nm} = M^N. Good.
Edge case: N=0? Not possible because N>=1.
Now test example2 manually? Might compute with code.
Now potential pitfalls: If M-1 is 0 mod MOD? Already excluded because M<=2e5 < MOD.
Now also note: if M=1, we have separate case. For M=1, (M-1)=0, invM1 would be undefined, but we already treat separately.
Now complexity: O(N) ~ 2e5, fine.
But wait: The constraints say N, M up to 2e5, but K could be up to N-1 (2e5). So summing over K+1 terms is okay. But we need to compute binom for each t. Using factorial and invfact each time is O(1) per t. So total O(N). Memory O(N).
However, we might consider if N can be as high as 2e5, factorial arrays of size N (max 199999) is fine.
But there is also possibility to compute S using combinatorial identities or cumulative sums to get O(log N) but not needed.
Thus straightforward solution works.
But we need to be careful: For M=1 case: K may be less than N-1, answer 0; else 1.
Now test with N=1, M=5, K=0: our M>=2 branch: nm=0, fact[0]=1, invfact[0]=1. K=0, t=0: binom = C(0,0)=1; powInv=1; S=1; powM1 = (M-1)^0=1; ans = M * 1 * 1 =5, correct.
N=1, M=1, K=0: M=1 -> separate case: K>=0? Since N-1=0, K=0>=0 -> answer=1. Correct (only one color).
N=2, M=2, K=0: colorings with no equal adjacent pairs: first any of 2, second any except first => 21=2. Our formula: nm=1, sum t=0: C(1,0)=1, invM1=1, powInv=1, S=1; powM1 = (1)^{1}=1; ans=211=2. Good. For K=1: sum t=0..1: t=0 ->1, t=1 -> C(1,1)=1, powInv after t=0: 1 still? Actually invM1=1, so powInv stays 1; term t=1:11=1; S=2; ans=21*2=4 which equals total colorings 2^2=4. Good.
Now check M>2 example: N=3, M=3, K=1. Let's enumerate: total 27 colorings. Count those with at most 1 equal adjacent pair. Equivalent to exclude those with 2 equal pairs (i.e., all three same color). Those have t=2. Number of such: all three same color: 3. So answer = 27-3=24. Our formula: nm=2, invM1 = 1/2 mod MOD, powM1 = 2^{2}=4. S = sum_{t=0}^{1} C(2,t)*(1/2)^t. t=0: C(2,0)=1; t=1: C(2,1)=2, *(1/2)=1; total S=2. ans = 3 * 4 * 2 = 24. Good.
Thus the solution is correct.
Now, I'm aware that sometimes people try to solve via DP, but this combinatorial simplification is much simpler.
Potential issue: The problem states "There may be at most K pairs of adjacent blocks that are painted in the same color." It does not say "exactly K", but "at most K". So we sum up to K.
Now, note: The derived formula uses runs. There's another perspective: The number of colorings with exactly r runs (r from 1 to N) is C(N-1, r-1) * M * (M-1)^{r-1}. So answer = M * sum_{r=N-K}^{N} C(N-1, r-1) * (M-1)^{r-1}. This might be computed directly. But the t-form is easier.
Now check constraints: N and M up to 2e5. Precomputing factorial up to N-1 = 199999 is fine.
But what if N=200000, then N-1=199999; factorial array size 200000. Fine.
Implementation details:
We'll implement using Python. Since MOD is prime and within typical ranges, use pow for exponentiation.
We'll handle M=1 separately.
Compute factorial and inverse factorial up to n-1. But if N=1, n-1=0, we still need factorial[0]=1; we can handle that.
Alternatively, we can compute binom incrementally without factorial to avoid arrays. But arrays are fine.
We must be careful about memory and speed.
Now the sum loop for t from 0 to K inclusive. If K > nm? but K ≤ N-1 = nm, so okay.
Now the answer is M * powM1 % MOD * S % MOD.
Now we should test sample3 with big numbers to ensure no overflow. But we trust.
Now consider alternative: since M and N up to 2e5, powM1 = pow(M-1, N-1, MOD) can be computed quickly.
Now, the sum S might be computed using cumulative binomial sums with ratio, but not needed.
Now some nuance: The derivation using runs assumes that the runs are defined by the coloring, which is fine. There is no double counting.
Thus, I think the solution is correct.
Now we should produce final answer in Python.
Implementation steps:
- Read input N, M, K.
- MOD = 998244353.
- If M == 1:
if K >= N-1: print(1) else print(0)
return
- nm = N - 1
- Compute invM1 = pow(M-1, MOD-2, MOD)
- Compute powM1 = pow(M-1, nm, MOD)
- Precompute factorial and invfact for up to nm:
fact = [1] * (nm+1)
for i in range(1, nm+1): fact[i] = fact[i-1] * i % MOD
invfact = [1] * (nm+1)
invfact[nm] = pow(fact[nm], MOD-2, MOD)
for i in range(nm, 0, -1): invfact[i-1] = invfact[i] * i % MOD
- Compute S = 0
cur_pow = 1
for t in range(0, K+1):
binom = fact[nm] * invfact[t] % MOD * invfact[nm - t] % MOD
term = binom * cur_pow % MOD
S = (S + term) % MOD
cur_pow = cur_pow * invM1 % MOD
- ans = M * powM1 % MOD * S % MOD
- print(ans)
But note: If nm is large (200k) and we compute binom for each t as fact[nm] * invfact[t] * invfact[nm-t], this involves three multiplications per t. That's fine. But we can also use incremental to avoid factorial arrays, but it's fine.
Now test with sample1.
Now about performance: 200k loops, each with constant operations, is fast in Python.
But we need to be careful about Python recursion? Not used.
Now, memory: two arrays of size up to 200k, each integer 28 bytes? roughly 10 MB, okay.
Now one more thought: M-1 could be zero? Already excluded because M=1 separate.
Now check if N-1 could be 0? If N=1, then nm=0. Our factorial array will be of size 1 (index 0). The loop for factorial from 1 to 0 won't run (range(1,1) empty). So fact[0]=1. invfact[0] =? we compute invfact[nm] = invfact[0] = pow(fact[0], MOD-2, MOD) = 1. Then for i in range(0,0,-1) maybe? range(nm,0,-1) where nm=0: range(0,0,-1) empty, so invfact[0] stays 1. Works. Loop for t from 0 to K: if K >=0? N=1, K between 0 and 0? Actually K <= N-1 =0, so K=0. So t=0: binom = fact[0] * invfact[0] * invfact[0] = 1. cur_pow=1. S=1. ans = M * powM1 * 1. powM1 = (M-1)^{0}=1. So ans=M, correct.
Thus works.
Now edge: if M=1 and N=1, K=0: separate case prints 1 (since K>=0). Good.
Now confirm sample2: N=100, M=100, K=0. Compute manually using code later.
Now sample3: large numbers; we trust.
Now consider if K may be large, say N-1. Then we sum t=0..N-1. This includes t=N-1. At t=N-1, binom = C(N-1, N-1)=1, cur_pow = invM1^{N-1}. So S includes that term. Then ans = M * (M-1)^{N-1} * S. Should equal M^N. Verified.
Now, we can also compute directly using formula ans = sum_{t=0}^{K} C(N-1, t) * M * (M-1)^{N-t-1}, without factoring. That might even be simpler: Precompute powers of (M-1) up to N-1, then for each t compute term = C(N-1, t) * M % MOD * powM1_{N-t-1}? But powM1_{N-t-1} = (M-1)^{N-t-1}. We could precompute powM1 array in descending order: powM1[i] = (M-1)^i for i=0..N-1. Then term = C(N-1, t) * M % MOD * powM1[N-t-1] % MOD. But then need to multiply M each time. It's similar.
But our factoring may help if we want to use cumulative sum of binomial with constant ratio. However we stick with the simplest.
But we have to consider modulo inverses. Our approach uses modular inverse of M-1, which is fine.
Now check for large N, the factorial of N-1 computed using loop, fine.
Now, but one thing: The sum S we defined as sum_{t=0}^{K} C(N-1, t) * (M-1)^{-t}. Since (M-1)^{-t} is a rational number but mod we compute it. Since we are working modulo prime, it's fine.
Now final answer.
However, we should also think about the possibility that M-1 is 0 modulo MOD? Since MOD is huge (998244353), M up to 200k, M-1 < MOD, so non-zero mod MOD. So no issue.
Now we should implement.
But before finalizing, let's double-check the derivation thoroughly.
We have N blocks in a row. Let the number of runs (maximal consecutive same-color segments) be r. Then the number of adjacent equal pairs is N - r. This is because between each pair of consecutive blocks, if they belong to same run, they are equal. Number of boundaries between runs = r-1, which are the places where colors differ. The total number of adjacent pairs = N-1, so equal pairs = (N-1) - (r-1) = N - r. So t = N - r, and r = N - t.
Now, number of ways to have exactly r runs: First, choose the lengths of the runs: they are positive integers summing to N. Number of compositions = C(N-1, r-1). Then, assign colors: first run: M choices; each subsequent run must differ from the previous: M-1 choices. So M * (M-1)^{r-1}. So total = C(N-1, r-1) * M * (M-1)^{r-1}.
Thus, number with exactly t equal pairs = C(N-1, N-t-1) * M * (M-1)^{N-t-1} = C(N-1, t) * M * (M-1)^{N-t-1} because C(N-1, N-t-1) = C(N-1, t). Yes.
Now, sum_{t=0}^{K} = M * sum_{t=0}^{K} C(N-1, t) * (M-1)^{N-t-1}.
Now we compute as M * (M-1)^{N-1} * sum_{t=0}^{K} C(N-1, t) * (M-1)^{-t}.
All good.
Now we should verify with small N manually.
N=2, M=2:
- t=0: colorings: AB with A≠B: 2*1=2.
- t=1: colorings: AA, BB: 2.
Total =4 = 2^2.
Our f(0) = C(1,0)21^{1} = 2. f(1) = C(1,1)21^{0}=2. Sum=4.
N=3, M=2:
- t=0: no equal adjacent: first any of 2, second different from first (1), third different from second (1): 211=2? Actually careful: For no equal adjacent, we require c1≠c2 and c2≠c3. That yields 2 choices for c1, then c2 must be opposite, then c3 must be opposite of c2, which is the same as c1. So c3 = c1, but that would cause c2≠c3? Wait if c1 = A, then c2 must be B, then c3 must be different from B => A, so c3=A. That gives sequences: A B A. That's one? But we have two colors: so c1 can be A or B => 2 sequences: A B A and B A B. So f(0)=2. But earlier we computed using formula: M * (M-1)^{N-1} = 2 * 1^{2}=2, correct.
- t=1: exactly one equal pair. Cases: either first two equal (c1=c2) and c2≠c3, or first two differ and second two equal (c1≠c2, c2=c3). Let's count: For first two equal: c1=c2, so choose color for pair (2 ways), c3 must be different from c2 (1 way) => 2. For second two equal: c1≠c2, c2=c3. c1 any 2, c2 must be different from c1 (1), c3 = c2 => 2. Total 4. So f(1)=4. Sum=6, matches example.
Now compute with formula: f(1) = C(2,1)2(1)^{1} = 221=4, good.
So fine.
Now, any other nuance? The problem statement: "There may be at most K pairs of adjacent blocks that are painted in the same color." That includes the possibility that some adjacent blocks may be same, but up to K. So our counting sums over t=0..K.
Now, about modulo: 998244353. All good.
Now I'll produce final code in Python.
Potential optimization: we could stop early if K is large (>= nm) then S = (1 + invM1)^{nm}. But we can just compute sum up to nm. Since K may be as large as nm, it's still O(N). That's fine.
But note: If K > nm? Not possible due to constraint K <= N-1 = nm.
Thus all set.
Now we can implement.
But let's think about whether we need to handle large M up to 2e5 but N up to 2e5, but M may be 0? No, M≥1.
Now think about using factorial arrays: we need to compute factorial of up to 200000, which is fine. However, we must ensure to compute factorial up to nm = N-1. If N=200000, nm=199999, okay. But if N=1, nm=0, we still need factorial array of size 1. So we should handle that gracefully.
Now about memory: we allocate fact and invfact each of size nm+1. For nm=199999, that's around 200k integers. In Python, each integer object is about 28 bytes, so 200k * 28 * 2 ≈ 11.2 MB, plus overhead of list (8 bytes per reference?) Actually list holds references to PyObject, each reference 8 bytes, so two lists 200k * 8 * 2 = 3.2 MB plus integer objects. Each integer for numbers up to MOD (30 bits) uses 28 bytes (maybe). So total might be around 11-12 MB, which is fine.
But we can even avoid storing factorial and invfact and compute binom incrementally, reducing memory. But not necessary.
However, note that computing factorial for nm=200000 uses multiplication of large numbers mod MOD, it's fine.
Alternatively, we can compute S using cumulative sum of binom * q^t via recurrence, like:
Let a_t = C(n, t) * q^t. Then a_{t+1} = a_t * q * (n - t) / (t+1). So we can compute on the fly, without factorial array. That reduces memory and maybe simplifies code. But we need modular inverse of t+1 quickly; we could precompute inv up to n, or compute using pow each step (log MOD) which would be O(N log MOD) ~ 2e5 * 30 = 6e6, still acceptable. But better to precompute inv linear.
Let's consider incremental method for elegance and maybe less memory.
We'll compute:
n = N-1.
q = invM1.
cur_binom = 1
cur_pow = 1
S = 0
for t in range(0, min(K, n)+1):
S += cur_binom * cur_pow
# update for next t:
if t == n: break # because t+1 > n
cur_binom = cur_binom * (n - t) % MOD * inv(t+1) % MOD
cur_pow = cur_pow * q % MOD
But careful: The order of updates: At t, we used cur_binom and cur_pow. After that, we want cur_binom for t+1. So we multiply by (n - t) and inv(t+1). And cur_pow multiply by q. This is correct if cur_pow initially 1 corresponds to q^0. After using at t, we multiply cur_pow by q to get q^{t+1} for next iteration.
We need inv(t+1) for each step. We can precompute inv array up to n using linear method: inv[1]=1; for i in range(2, n+1): inv[i] = MOD - MOD//i * inv[MOD%i] % MOD. Or compute pow(i, MOD-2, MOD) each step (which is O(log MOD)). But with n up to 2e5, pow per iteration might be okay? 2e5 * ~30 = 6 million operations, probably fine. However, to be safe and efficient, I'd precompute inv up to n using linear method (O(n)). That also uses memory O(n). But we already considered factorial memory, so similar.
We could also compute cur_binom using multiplication and division via precomputed factorial? Anyway.
Given that both methods are fine, I'll go with factorial for clarity, as it's straightforward and less prone to off-by-one errors.
But there is a minor nuance: For large n, computing factorial and then invfact for each t is O(1) per t. However, we also need to compute fact[n] and invfact array. That's okay.
But we must compute fact[n] first, then compute invfact. That requires O(n) precomputation.
Now, is there any issue with Python's speed if we have loops for 200k and use multiplication and modular operations? It's fine.
Now, final code:
import sys
MOD = 998244353
def main():
import sys
input_data = sys.stdin.read().strip().split()
if not input_data:
return
N, M, K = map(int, input_data)
if M == 1:
if K >= N-1:
print(1)
else:
print(0)
return
n = N - 1 # because we use binomial (N-1 choose t)
inv_M1 = pow(M-1, MOD-2, MOD)
pow_M1 = pow(M-1, n, MOD)
# Precompute factorials and inv factorials up to n
fact = [1] * (n+1)
for i in range(1, n+1):
fact[i] = fact[i-1] * i % MOD
inv_fact = [1] * (n+1)
if n >= 0:
inv_fact[n] = pow(fact[n], MOD-2, MOD)
for i in range(n, 0, -1):
inv_fact[i-1] = inv_fact[i] * i % MOD
S = 0
cur_pow = 1
for t in range(0, min(K, n)+1):
# binomial C(n, t)
comb = fact[n] * inv_fact[t] % MOD * inv_fact[n-t] % MOD
term = comb * cur_pow % MOD
S = (S + term) % MOD
cur_pow = cur_pow * inv_M1 % MOD
# If K > n (should not happen per constraints), but just in case:
# Actually K <= N-1 = n, so not needed.
ans = M * pow_M1 % MOD * S % MOD
print(ans)
if __name__ == "__main__":
main()
But note: we used min(K, n) but K <= n, so fine.
Now test with given examples.
Let's test manually with Python mental:
Example1: N=3 M=2 K=1
n=2, inv_M1 = inv(1)=1, pow_M1=1^2=1
fact: [1,1,2]? Actually compute: fact[0]=1, fact[1]=1, fact[2]=2
inv_fact: compute inv_fact[2]=inv(2)=499122177? Wait 2^-1 mod 998244353 = (998244353+1)/2 = 499122177. But we don't need exact numbers as long as combination works.
t=0: comb = fact[2]inv_fact[0]inv_fact[2] = 21inv_fact[2] -> inv_fact[2]=? But let's compute properly: inv_fact[2] = pow(fact[2], MOD-2) = 2^{-1}=499122177. So comb = 2 * 1 * 499122177 mod = 1? Actually C(2,0)=1, so the product should be 1. Since inv_fact[2] is modular inverse of 2, fact[2]=2, so 2 * inv_fact[2] = 1, times inv_fact[0]=1 gives 1. So fine.
cur_pow=1, term=1, S=1.
t=1: cur_pow updated? Wait we update cur_pow after each iteration. In the loop, we should update cur_pow after using term. In my code, I computed term using current cur_pow, then update cur_pow for next iteration. So after t=0, cur_pow becomes cur_pow * inv_M1 = 11=1. So for t=1, cur_pow=1. comb = C(2,1)=2? Let's compute: fact[2]inv_fact[1]inv_fact[1] = 2 * inv_fact[1]^2. inv_fact[1] is 1 (since fact[1]=1, inv_fact[1]=1). So comb=2. term=2, S=3. Then update cur_pow=11=1. Done. ans=213=6. Good.
Now example2: N=100 M=100 K=0:
n=99, inv_M1 = inv(99). Since 99 < MOD, its inverse exists. pow_M1 = 99^99 mod. We'll trust that answer 73074801.
Now example3: we trust.
Now we have to ensure that we compute factorial for n up to 199999. That's fine.
But wait: What if N = 200000, then n=199999. That is fine.
Now I realize: For M >= 2, but what about M=0? Not possible.
Now time complexity: O(N) precomputation, O(K) loop. K may be up to N-1, so O(N) total. So 2e5 operations is trivial.
Now I'll consider potential overflow: Python integers are big, but we mod each operation, so fine.
Now check if there is any other interpretation: Could there be any overcounting due to runs where M-1 choices for subsequent runs might include the same color as two runs before, but that's allowed as long as adjacent differ. So correct.
Thus solution is good.
But wait: Did we need to consider the condition "There may be at most K pairs of adjacent blocks that are painted in the same color." This is exactly the number of equal adjacent pairs. So our method counts exactly.
Now confirm for M=1: if K>=N-1, answer=1; else 0. That matches.
Now also consider N=0? Not possible.
Now we might also want to treat K negative? No, K >=0.
Now, double-check for K=0: The answer = M * (M-1)^{N-1}, correct.
Now, some people might try DP with prefix sums but we have a closed form.
Thus final answer.
But before concluding, let's check if there is any subtlety: The count of runs method implicitly assumes that the coloring is defined by runs, but it might double count if we consider compositions and then color assignments that could produce the same runs? No, each coloring uniquely defines the runs and their lengths; and given runs lengths and colors, we can reconstruct the coloring. So it's bijection.
Thus answer is correct.
Now I'll produce final answer.
But there is also alternative formula: answer = sum_{i=0}^{K} C(N-1,i) * M * (M-1)^{N-i-1}. Could be computed directly using precomputed powers of (M-1) and factorials, no need to compute inv of M-1. Actually simpler: compute powM1 = (M-1)^{N-1}, then compute powers of invM1 for each term, but we could also compute powers of (M-1) for decreasing exponents: let powM1_desc[t] = (M-1)^{N-1-t}. Then we can compute term = C(N-1,t) * M * powM1_desc[t] % MOD. But we can compute powM1_desc iteratively: start with powM1 = (M-1)^{N-1} for t=0; then for each step, divide by (M-1) (multiply by invM1) to get next. So same.
I'll stick with my method.
Now we should ensure that if N=1, then n=0, factorial array of size 1. In the loop for t from 0 to min(K, n) = 0, so we run t=0. comb = fact[0] * inv_fact[0] * inv_fact[0] = 1. cur_pow=1, term=1, S=1. ans = M * pow_M1 * 1 = M * (M-1)^0 = M. Works.
Now for M=1, separate.
Now there's one more nuance: M-1 might be 0 mod MOD? Not in constraints. So okay.
Now, I'll implement final solution as described.
But we need to be careful about reading input and output.
Now, the solution passes examples.
Let's do a quick test for N=2, M=3, K=0: answer should be M * (M-1)^{1} = 32=6. Our code: M>=2, n=1, invM1=inv(2)=? Actually M-1=2, invM1 = inverse of 2 = (MOD+1)//2? That's fine. pow_M1 = 2^1=2. S = sum t=0..0: C(1,0)=1, cur_pow=1, S=1. ans = 32*1=6. Good.
For K=1: answer should be total M^2=9. Our S: t=0 term=1; update cur_pow = 1inv2; t=1: comb=C(1,1)=1, cur_pow = inv2? Actually after t=0, cur_pow becomes 1inv2 = inv2. Then term = 1 * inv2 = inv2. S = 1 + inv2. Then ans = 3 * 2 * (1 + inv2) = 6 * (1 + inv2) = 6 + 6*inv2 = 6 + 3 = 9. Since inv2 = (MOD+1)//2 mod? Actually 6 * inv2 mod = 3 mod, correct.
Now implement.
Potential improvements: Avoid factorial arrays if we want to be more memory efficient, but it's not necessary.
But there's one more point: The problem constraints say N, M ≤ 2×10^5, but K can be up to N-1. So using O(N) is fine.
Now final answer.
But wait, we might also be able to compute the sum using combinatorial identities that involve incomplete beta functions, but not needed.
Thus final solution.
Now code.
I'll produce final answer with proper comments.
Check for possible pitfalls: In Python, when n=0, fact array is size 1, but when computing inv_fact[n] = inv_fact[0] = pow(fact[0], MOD-2) = 1. Then for loop for i in range(n,0,-1) becomes range(0,0,-1) which is empty, so okay. Good.
Now, what about K being larger than n? The constraint says K ≤ N-1 = n, so not. But I'll still use min(K, n) to be safe.
Now final answer.
Now provide solution in final channel. ```python
import sys
MOD = 998244353
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
N, M, K = map(int, data)
# Special case: only one colour
if M == 1:
print(1 if K >= N - 1 else 0)
return
n = N - 1 # binomial upper index
inv_M1 = pow(M - 1, MOD - 2, MOD) # (M-1)^{-1}
pow_M1 = pow(M - 1, n, MOD) # (M-1)^{n}
# factorials and inverse factorials up to n
fact = [1] * (n + 1)
for i in range(1, n + 1):
fact[i] = fact[i - 1] * i % MOD
inv_fact = [1] * (n + 1)
if n >= 0:
inv_fact[n] = pow(fact[n], MOD - 2, MOD)
for i in range(n, 0, -1):
inv_fact[i - 1] = inv_fact[i] * i % MOD
# S = sum_{t=0}^{K} C(n, t) * (M-1)^{-t}
S = 0
cur_pow = 1 # (M-1)^{-t}
for t in range(0, min(K, n) + 1):
comb = fact[n] * inv_fact[t] % MOD * inv_fact[n - t] % MOD
S = (S + comb * cur_pow) % MOD
cur_pow = cur_pow * inv_M1 % MOD # next power
ans = M * pow_M1 % MOD * S % MOD
print(ans)
if name == "main":
main()