Problem: There are N blocks arranged in a row. Let us paint these blocks.  We will consider two ways to paint the blocks different if and only if there is a block painted in different colors in those two ways.  Find the number of ways to paint the blocks under the following conditions:    * For each block, use one of the M colors, Color 1 through Color M, to paint it. It is not mandatory to use all the colors.   * There may be at most K pairs of adjacent blocks that are painted in the same color.  Since the count may be enormous, print it modulo 998244353.

Submission attempts (in order):
def inverse_mod(a, mod=10**9+7):
    """ Calculate inverse of the integer a modulo mod.
    """
    return pow(a, mod-2, mod)


def combination_mod(n, r, mod=10**9+7):
    

    r = min(r, n-r)
    numerator = denominator = 1
    for i in range(r):
        numerator = numerator * (n - i) % mod
        denominator = denominator * (i + 1) % mod

    return numerator * inverse_mod(denominator, mod) % mod


def create_inverses_list(n, mod):
    """ Create list of inverses of the integers 0 to n modulo mod.
    """
    
    inv_list = [1] * (n+1)
    for x in range(n+1):
        inv_list[x] = (-(mod//x) * inv_list[mod % x]) % mod
    
    return inv_list


def main():
    n_blocks, n_colors, n_max_pairs = map(int, input().split())
    mod = 998244353

    inverses = create_inverses_list(n_max_pairs, mod)
    
    e1 = n_colors * pow(n_colors-1, n_blocks-1, mod)
    e2 = 1
    total = e1 * e2 % mod

    for k in range(1, n_max_pairs+1):
        e1 = e1 * inverses[n_colors - 1] % mod
        e2 = e2 * (n_blocks - k) % mod * inverses[k] % mod
        total += e1 * e2 % mod
        total %= mod

    print(total)


if __name__ == "__main__":
    main()


def inverse_mod(a, mod=10**9+7):
    """ Calculate inverse of the integer a modulo mod.
    """
    return pow(a, mod-2, mod)


def combination_mod(n, r, mod=10**9+7):
    

    r = min(r, n-r)
    numerator = denominator = 1
    for i in range(r):
        numerator = numerator * (n - i) % mod
        denominator = denominator * (i + 1) % mod

    return numerator * inverse_mod(denominator, mod) % mod


def create_inverses_table(n, mod=10**9+7):
    """ Create table for inverses of the integers 0 to n modulo mod.
    """

    inv_table = [1] + [1] * n
    for x in range(2, n+1):
        inv_table[x] = -(mod//x) * inv_table[mod % x] % mod

    return inv_table


def main():
    n_blocks, n_colors, n_max_pairs = map(int, input().split())
    mod = 998244353

    inverses = create_inverses_table(max(n_colors, n_max_pairs), mod)

    e1 = n_colors * pow(n_colors-1, n_blocks-1, mod) % mod if n_colors > 0 else 1
    e2 = 1
    total = e1 * e2

    for k in range(n_max_pairs+1):
        e1 = e1 * inverses[n_colors - 1] % mod
        e2 = e2 * (n_blocks - k) * inverses[k] % mod
        total += e1 * e2
        total %= mod

    print(total)


if __name__ == "__main__":
    main()


def inverse_mod(a, mod=10**9+7):
    """ Calculate inverse of the integer a modulo mod.
    """
    return pow(a, mod-2, mod)


def combination_mod(n, r, mod=10**9+7):
    

    r = min(r, n-r)
    numerator = denominator = 1
    for i in range(r):
        numerator = numerator * (n - i) % mod
        denominator = denominator * (i + 1) % mod

    return numerator * inverse_mod(denominator, mod) % mod


def create_inverses_table(n, mod=10**9+7):
    """ Create table for inverses of the integers 0 to n modulo mod.
    """

    inv_table = [0] + [1] * n
    for x in range(2, n+1):
        inv_table[x] = -(mod//x) * inv_table[mod % x] % mod

    return inv_table


def solve():
    n_blocks, n_colors, n_max_pairs = map(int, input().split())

    if n_colors == 1:
        return int(n_blocks - n_max_pairs == 1)

    mod = 998244353

    inverses = create_inverses_table(max(n_colors, n_max_pairs), mod)

    e1 = n_colors * pow(n_colors-1, n_blocks-1, mod) % mod
    e2 = 1
    total = e1 * e2

    for k in range(1, n_max_pairs+1):
        e1 = e1 * inverses[n_colors - 1] % mod
        e2 = e2 * (n_blocks - k) * inverses[k] % mod
        total += e1 * e2
        total %= mod

    return total


def main():
    print(solve())


if __name__ == "__main__":
    main()


Verdicts: Runtime Error

Wrong Answer

Accepted