We need to compute number of colorings of n bricks with m colors, such that exactly k bricks have a color different from the brick on its left. The first brick is not counted, so we have n-1 adjacent pairs; exactly k of them have a change (i.e., the colors are different). This is essentially sequences of length n over an alphabet of size m, with exactly k positions i (2 ≤ i ≤ n) where a_i != a_{i-1}. This is a classic combinatorial counting problem.
We can think: choose the positions where a change occurs. Since there are n-1 transitions, we need exactly k changes. So we have a sequence of runs: the first brick chooses any of m colors. Then each time we have a change, we switch to a different color (any of the m-1 other colors). However, careful: when we have a change, the color chosen must be different from the previous color, but could be same as earlier colors. The count: number of sequences = m * (m-1)^k * (number of ways to choose which of the n-1 transitions are changes?) Wait: If we simply choose exactly k transitions to be changes and the remaining n-1-k transitions to be non-changes (i.e., same color as previous), then the sequence is determined by: first brick color (m choices). Then for each transition that is a change, we choose a new color: but it must be different from the immediate previous color. However, if we have multiple consecutive changes, the color after each change can be any of the m-1 colors different from the previous. But we also have to ensure that the transitions designated as non-changes indeed have same color as previous. So given a pattern of changes (which transitions are changes), the number of colorings consistent with that pattern is: m * (m-1)^{#changes} (since each change we pick a color from the m-1 other than the previous). Is that correct? Let's verify: Suppose we have pattern: positions (i) where a_i != a_{i-1} indicated. For the first brick, color = c1. For i from 2 to n: if transition i is a non-change, then a_i = a_{i-1}. If it is a change, then a_i can be any color except a_{i-1}. However, note that when we have a change, the choice of a_i may affect subsequent changes because the "previous color" changes. But the counting approach of m * (m-1)^{#changes} is valid if we treat each change independently: given the previous color, there are exactly m-1 choices for the new color regardless of history. However, does the possibility of picking the same color as earlier (maybe equal to some color before previous) matter? No, because we only restrict that it is not equal to the immediate previous color. So yes, for each change we have m-1 choices, independent of previous choices (given the current color). Since each change's choices are independent conditioned on the previous color, the total number is m * (m-1)^k. This does not depend on the positions of changes; any pattern with exactly k changes yields that many colorings. However, is there any restriction that when we have a change, the color must be different from the previous, but could it accidentally cause a non-change later to be forced? No, because non-change means we set a_i = a_{i-1} exactly, so it's consistent regardless of what color we chose at the change. So indeed, for any fixed set of change positions, the number of colorings is m * (m-1)^k.
Now, the total number of ways to select which of the n-1 transitions are changes is C(n-1, k). So total number = C(n-1, k) * m * (m-1)^k.
But wait, check with examples: Example 1: n=3,m=3,k=0 => C(2,0)=1 * 3 * (2)^0 = 3. matches.
Example 2: n=3,m=2,k=1 => C(2,1)=2 * 2 * (1)^1 = 4. matches.
So answer = C(n-1, k) * m % MOD * (m-1)^k % MOD.
But is that all? Constraints: n,m up to 2000, k up to n-1. So computing C(n-1,k) modulo MOD (998244353) is easy with precomputed factorials up to 2000. However, is there any nuance? The problem statement says "m paint buckets of different colors at hand, so he painted each brick in one of those m colors." So each color is distinct; there's no other restrictions. So the straightforward formula seems correct.
But wait: Could there be any other interpretation? For instance, the first brick is not counted, so exactly k bricks have a color different from the left neighbor. That's exactly the count of changes. So yes.
But note that we might be missing something: The colorings may be considered up to something else? No.
Double-check: Suppose m=1. Then we have only one color. Then if k>0, it's impossible because you cannot have changes. If k=0, then only 1 coloring. According to formula: C(n-1,k) * 1 * (0)^k. For k>0, (m-1)^k = 0^k = 0. So total 0, correct. For k=0, C(n-1,0)11=1. Good.
So answer is straightforward.
But wait, the constraints say n,m up to 2000, which is small, but we can compute factorials easily.
However, sometimes problems require DP or something else, but here it's simple.
But is there any nuance with modulo exponent? We'll compute pow(m-1, k, MOD).
Edge Cases: m=0? No, m ≥ 1.
Thus solution: read n,m,k; compute ans = C(n-1,k) * m % MOD * pow(m-1, k, MOD) % MOD.
But wait: Is it exactly C(n-1, k)? Actually, careful: The positions of changes are among the n-1 transitions. But what if we have two consecutive changes? That's allowed. So number of patterns = number of subsets of size k from n-1 transitions = C(n-1, k). Yes.
Thus answer.
But maybe there's a subtlety: When we have a change, we can choose any of the m-1 colors, but is it possible that two different choices could lead to the same overall coloring if some later change or non-change? No, because each coloring uniquely determines which transitions are changes, and for a given coloring, the number of changes is exactly k. So our counting by pattern and then coloring within pattern should be correct and each coloring is counted exactly once for the pattern it induces. So no overcounting.
Thus solution is simple.
But let's consider larger constraints? Not needed.
Now, I suspect this problem might be from Codeforces (perhaps "Chouti and the bricks"? Actually, problem "Chouti and the bricks" maybe? Could be a problem from Codeforces Round... I recall a problem about painting bricks with exactly k changes, and the solution was indeed combinatorial. So it's fine.
Nevertheless, let me think: Could there be any requirement about using all colors? No.
So final solution: compute factorial up to n (max 2000). Precompute modular inverses. Compute comb(n-1,k) using factorials.
But note: n could be 2000, so factorial up to 2000 is fine.
We can also compute comb by direct multiplication since n is small, but better use factorials.
Implementation:
MOD = 998244353
def binom(n, r):
if r<0 or r>n: return 0
return fact[n] * invfact[r] % MOD * invfact[n-r] % MOD
We need to compute fact and invfact.
Compute pow for (m-1)^k with exponent maybe 0.
Edge: m=1 and k>0 => (0)^k = 0, okay.
So final code.
But hold on: n can be up to 2000 only, but m can be up to 2000 as well. So pow is fine.
Thus solution appears correct.
However, I'll double-check with some random small cases using brute force to ensure the formula holds.
Let’s brute for n=4,m=2,k=1.
All sequences of length 4 over {0,1} with exactly 1 change (i.e., only one index i where a_i != a_{i-1}, for i=2,3,4). Count manually:
List all 16 sequences? Actually m=2 gives 16 sequences, but let's count changes.
We want exactly 1 change.
Case: start with 0.
-
No change at position2 => a2=0. Then need exactly 1 change among positions3,4? That would give either change at 3 only or change at 4 only? But also note that if a2=0 and a3=0 (no change at3), then a4 must be something: if no change at4 then a4=0 => total changes 0. If change at4 => a4=1 => change count=1 (since a3=0, a4=1). So that yields one coloring: 0 0 0 1? Actually a = [0,0,0,1] has change at position4 only. That's exactly 1 change. But wait: also could a3=1 (change at3) and then a4? If a3=1, then we have a change at3. Then if a4=1 (no change at4) => changes=1 (at3). If a4=0 (change at4) => changes=2. So we get [0,0,1,1] changes at3 only; [0,0,1,0] changes at3 and at4 (2). So from start 0 with a2=0, we have valid sequences: [0,0,0,1] and [0,0,1,1]. Two sequences.
-
Now a2=1 (change at2). Then changes count includes position2. Need total changes=1 => so no more changes at positions3,4. Thus a3 must equal a2=1, and a4 must equal a3=1. So sequence: [0,1,1,1]. That's one.
Similarly starting with 1 gives symmetric: [1,1,1,0], [1,1,0,0], [1,0,0,0]. So total 6? Let's count: from start 0: three? Actually we found [0,0,0,1], [0,0,1,1], [0,1,1,1] -> 3. From start 1: [1,1,1,0], [1,1,0,0], [1,0,0,0] -> 3. Total 6.
Now compute using formula: n-1=3, k=1 => C(3,1)=3, m=2, (m-1)^1=1, so total = 321=6. Yes.
Another test: n=4,m=3,k=2.
Manual: Let's compute with formula: C(3,2)=3, m=3, (2)^2=4 => total 334=36.
But let's try brute with small numbers maybe? Not needed.
Thus formula is correct.
Therefore final solution is straightforward.
But wait: there is a nuance: The problem might be tricky because constraints up to 2000 suggest a DP or something else, but the combinatorial solution is O(n) or O(n log MOD) after precomputations, which is fine.
But is there any hidden twist? Maybe the first brick is not counted, so we have exactly k bricks with a color different from the left. That's same as exactly k changes. So it's fine.
Thus answer.
I'll now craft final response.
Implementation details:
- Read n,m,k.
- Precompute factorial and inv factorial up to max_n = max(n-1, maybe n) as n-1 can be 0? If n=1, then k must be 0 because k ≤ n-1=0. So we need factorial up to 0, which is fine. But we should handle n=1 separately? The formula works: n-1=0, C(0,0)=1, m * (m-1)^0 = m * 1 = m. That would mean for a single brick, there are m ways regardless of k (only k=0). But the problem states k ≤ n-1, so if n=1, k must be 0. So answer = m. That's correct.
But careful: C(0,0)=1 is standard.
Thus we need factorials up to 2000 at most, but for n=1, we need factorial up to 0, so we can set array size n+5 or just 2005.
We'll compute fact[0]=1, for i in 1..N: fact[i]=fact[i-1]*i%MOD.
Then invfact[N]=pow(fact[N], MOD-2, MOD). Then downwards.
Then compute comb.
Then compute ans = comb(n-1, k) * m % MOD * pow(m-1, k, MOD) % MOD.
Return ans.
Edge case: if m==1 and k>0: then (m-1)^k = 0, ans=0, correct.
Thus solution.
I'll also mention alternative DP solution, but not needed.
Now answer. ```python
import sys
MOD = 998244353
def main():
data = sys.stdin.read().strip().split()
if not data:
return
n, m, k = map(int, data)
# Precompute factorials and inverse factorials up to n (at most 2000)
N = max(n, 2000) # safe size, but we only need up to n-1 actually
fact = [1] * (N + 1)
for i in range(1, N + 1):
fact[i] = fact[i-1] * i % MOD
inv_fact = [1] * (N + 1)
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
def binom(nn, r):
if r < 0 or r > nn:
return 0
return fact[nn] * inv_fact[r] % MOD * inv_fact[nn - r] % MOD
comb = binom(n-1, k)
ans = comb * m % MOD * pow(m-1, k, MOD) % MOD
print(ans)
if name == "main":
main()