qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]
print(next)
# print(link)

link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0
# if len(link2) == 1 :
#     print('a')
#     rep = len(link)+1-len(link2)
# else:
print('b')
rep = len(link)- len(link2)

if(k > len(link2)):
    # if len(link2) == 1:
    #     print('c')
    #     p = (k-len(link2)+2)%rep
    #     print(p)

    print('d')
    p = (k-len(link2)+1)%rep
    # print(p)
    #
    # print(next)
    for i in range(p+rep-1):
        next = a[next-1]
    print('e')
else:
    print('f')
    next = 0
    for i in range(k):
        next = a[next-1]

ans = next
print(link)
print(link2)
print(ans)


Fixed code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
import numpy as np
link = set()
next = a[0]
link.add(1)
while next not in link:
    link.add(next)
    next = a[next-1]

# print(next)
link2 = set()
next2 = a[0]
link2.add(1)
while next not in link2:
    link2.add(next2)
    next2 = a[next2-1]

link2.remove(next)
ans = 0

rep = len(link) - len(link2)
# print(rep)
if (len(link2) == 0):
    p = k%rep
    # print('e')
    for i in range(p+rep):
        next = a[next-1]
elif(k > len(link2)):
    # print('f')
    p = (k-len(link2))%rep
    # print(p)
    # print(next)
    for i in range(p+rep):
        next = a[next-1]
else:
    # print('d')
    next = a[0]
    for i in range(k-1):
        next = a[next-1]
# print(next)
ans = next
# print(link,link2)
print(ans)
Buggy code:
n,k= list(map(int, input().split()))
li= list(map(int, input().split()))
d = [0 for i in range(n)]
c=[1]
nex=1
first=0
flag=0
for i in range(k):
    nex=li[nex-1]
    if d[nex]:
        first=c.index(nex)
        flag=1
        break
    d[nex-1]=1
    c.append(nex)
if flag:
    k-=first
    k%=len(c)-first
    print(c[first+k])
else:
    print(c[-1])

Fixed code:
n,k= list(map(int, input().split()))
li= list(map(int, input().split()))
d = [0 for i in range(n)]
c=[1]
nex=1
first=0
flag=0
for i in range(k):
    nex=li[nex-1]
    if d[nex-1]:
        first=c.index(nex)
        flag=1
        break
    d[nex-1]=1
    c.append(nex)
if flag:
    k-=first
    k%=len(c)-first
    print(c[first+k])
else:
    print(c[-1])
Buggy code:
from collections import defaultdict

n,k = map(int, input().split())
a = list(map(int, input().split()))

s = 1
w = [1]
y = defaultdict(lambda: 0)
y[1] = 1

flag = 0
zen = 0
for i in range(k):
	print(s,a[s-1])
	if(y[a[s-1]] == 1):
		print("w",w,a[s-1])
		flag = 1
		if(a[s-1] == 1):
			break
		else:
			for j in range(len(w)):
				print("aa",w[j],a[s-1])
				if(w[j] == a[s-1]):
					zen = j
					w = w[j:]
					
					break
		break
	s = a[s-1]
	y[s] = 1
	w.append(s)

print(s,w)

if(flag==0):
	print(s)
else:
	print(w[((k - zen) % (len(w)))])
	print(w,zen,((k - zen) % (len(w))))

Fixed code:
from collections import defaultdict

n,k = map(int, input().split())
a = list(map(int, input().split()))

s = 1
w = [1]
y = defaultdict(lambda: 0)
y[1] = 1

flag = 0
zen = 0
for i in range(k):
#	print(s,a[s-1])
	if(y[a[s-1]] == 1):
#		print("w",w,a[s-1])
		flag = 1
		if(a[s-1] == 1):
			break
		else:
			for j in range(len(w)):
#				print("aa",w[j],a[s-1])
				if(w[j] == a[s-1]):
					zen = j
					w = w[j:]
					
					break
		break
	s = a[s-1]
	y[s] = 1
	w.append(s)

#print(s,w)

if(flag==0):
	print(s)
else:
	print(w[((k - zen) % (len(w)))])
#	print(w,zen,((k - zen) % (len(w))))
Buggy code:
n, k =[int(i) for i in input().split()]
a = [int(i) - 1 for i in input().split()]

go = dict()
res = []

go[0] = True
res = [0]

now = a[0]
while True:
    if now in go:
        break
    res.append(now)
    go[now] = True
    now = a[now]

# 1
# [0, 1, 2, 3]
# (5 - 1) % (xunhuan)
amari = res.index(now)
xunhuan = res[amari:]
print(res, xunhuan, amari)
if k <= amari:
    print(res[k])
else:
    print(xunhuan[ (k - amari) % len(xunhuan)] + 1)

Fixed code:
n, k =[int(i) for i in input().split()]
a = [int(i) - 1 for i in input().split()]

go = dict()
res = []

go[0] = True
res = [0]

now = a[0]
while True:
    if now in go:
        break
    res.append(now)
    go[now] = True
    now = a[now]

# 1
# [0, 1, 2, 3]
# (5 - 1) % (xunhuan)
amari = res.index(now)
xunhuan = res[amari:]
#print(res, xunhuan, amari)

if k <= amari:
    print(res[k] + 1)
else:
    print(xunhuan[ (k - amari) % len(xunhuan)] + 1)
Buggy code:
import sys
from collections import deque
import copy
def main():
    N, K = map(int, input().split())
    A = [(int(i) - 1) for i in input().split()]
    d = []
    for i in range(N):
        d.append(-1)
    d[0] = 0
    now = 0
    cycle = 0
    ans = -1
    a = 0
    flag = True
    while a < K:
        if flag:
            if d[A[now]] == -1:
                d[A[now]] = d[now] + 1
            else:
                cycle = (d[now] + 1) - d[A[now]]
                K = (K - (d[now] + 1)) % cycle
                a = 0
                flag = False
        a += 1
        now = A[now]
    print(now + 1)


if __name__ == '__main__':
    main()


Fixed code:
import sys
from collections import deque
import copy
def main():
    N, K = map(int, input().split())
    A = [(int(i) - 1) for i in input().split()]
    d = []
    for i in range(N):
        d.append(-1)
    d[0] = 0
    now = 0
    cycle = 0
    ans = -1
    a = 0
    flag = True
    while K > 0:
        if flag:
            if d[A[now]] == -1:
                d[A[now]] = d[now] + 1
            else:
                cycle = (d[now] + 1) - d[A[now]]
                K %= cycle
                flag = False
                if K == 0:
                    break
        K -= 1
        now = A[now]
    print(now + 1)


if __name__ == '__main__':
    main()
Buggy code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
path = []

now = a[0]
path.append(now)

for i in range(1, k+1):
    now = a[now-1]
    if now in path:
        now = a[a.index(now) + (k-i)%(len(path) - a.index(now) - 1)]
        break
    path.append(now)

print(now)


Fixed code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
alr = [0 for i in range(n)]
now = 1
path = [now]

for i in range(k):
    now = a[now-1]
    # if now in path:
    #     now = path[path.index(now) + (k-i-1)%(len(path) - path.index(now))]
    #     break
    if alr[now-1] != 0:
        now = path[path.index(now) + (k-i-1)%(len(path) - path.index(now))]
        break
    path.append(now)
    alr[now-1] += 1

print(now)
Buggy code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

r = [0]*N

now = 0
r[now]=1
for i in range(N):
  now = A[now]-1
  if r[now] == 0:
    r[now] = i+2
  else:
    x1 = r[now]
    x2 = i+1
    break
print(x1, x2)
print(r)
roop_num = x2 - x1 + 1
x = (K-x1+1) % roop_num
print(x)
if K < x1:
  print(r.index(K+1)+1)
else:
  print(r.index(x1 + x)+1)


Fixed code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

r = [0]*N

now = 0
r[now]=1
for i in range(N):
  now = A[now]-1
  if r[now] == 0:
    r[now] = i+2
  else:
    x1 = r[now]
    x2 = i+1
    break

roop_num = x2 - x1 + 1
x = (K-x1+1) % roop_num

if K < x1:
  print(r.index(K+1)+1)
else:
  print(r.index(x1 + x)+1)
Buggy code:
a = list(map(int,input().split()))
z = 0
b = [1]
c = [0]*n
for i in range(n*3):
  j = [a[b[i]-1]]
  b.extend(j)
  c[a[b[i]-1]-1] = c[a[b[i]-1]-1]+1
  if c[a[b[i]-1]-1] == 2:
    z = z + 1

if k <= n:
  y = b[k]
else:
  l = (k-n)%z
  y = b[n+l]
print(y)

Fixed code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
z = 0
b = [1]
c = [0]*n
for i in range(n*3):
  j = [a[b[i]-1]]
  b.extend(j)
  c[a[b[i]-1]-1] = c[a[b[i]-1]-1]+1
  if c[a[b[i]-1]-1] == 2:
    z = z + 1
if k <= n:
  y = b[k]
else:
  l = (k-n)%z
  y = b[n+l]
print(y)
Buggy code:
def main():
    n, k = map(int, input().split())
    lst = list(map(int, input().split()))
    ma = [l-1 for l in lst]
    
    visited = [False for _ in range(n)]
    history = []
    now = 0
    while True:
        if visited[now]:
            history.append(now)
            break
        
        visited[now] = True
        history.append(now)
        
        now = ma[now]
    if k <= len(history)-1:
        print(history[k])
        return
    
    last = history[-1]
    for i in range(n):
        if history[i] == last:
            roopstart = i
            break
    syokilst = history[:roopstart]
    rooplst = history[roopstart:]
    syokilen = len(syokilst)
    rooplen = len(rooplst)
    resi = k - (syokilen-1)
    mod = resi%rooplen
    print(rooplst[mod])
        
main()

Fixed code:
def main():
    n, k = map(int, input().split())
    lst = list(map(int, input().split()))
    ma = [l-1 for l in lst]
    
    visited = [False for _ in range(n)]
    history = []
    now = 0
    while True:
        if visited[now]:
            history.append(now)
            break
        
        visited[now] = True
        history.append(now)
        
        now = ma[now]
    if k <= len(history)-1:
        print(history[k] + 1)
        return
    
    last = history[-1]
    for i in range(n):
        if history[i] == last:
            roopstart = i
            break
    syokilst = history[:roopstart]
    rooplst = history[roopstart:-1]
    syokilen = len(syokilst)
    rooplen = len(rooplst)
    resi = k - syokilen
    mod = resi%(rooplen)
    print(rooplst[mod] + 1)
main()
Buggy code:
N, M, K = map(int, input().split())
out = 0
mod = 998244353

c = 1

for k in range(K+1):

  out += M * pow(M-1, N-k-1, mod) * c
  c *= N - k
  c *= k + 1
  out %= mod
print(out)

Fixed code:
N, M, K = map(int, input().split())
out = 0
mod = 998244353

c = 1

for k in range(K+1):
  out += M * pow(M-1, N-k-1, mod) * c
  out %= mod
  c *= (N-k-1) * pow(k+1, mod-2, mod)
  c %= mod
print(out)
Buggy code:
n, m, k = map(int, input().split())
z = 998244353
if n <= k+1:
    a = 1
    for i in range(n):
        a = a*m%z
    print(a)
else:
    c = m
    for i in range(n-1):
        c = c*(m-1)%z
    a = [c]
    b = []
    d = [1, 1]
    e = [1, 1]
    f = [0, 1]
    for i in range(2, n):
        d.append(d[-1]*i%z)
        f.append((-e[z%i]*(z//i))%z)
        e.append(e[-1]*f[-1]%z)
    for i in range(k+1):
        b.append(d[n-1]*e[i]*e[n-i-1]%z)
    x = 0
    for i in range(1, k+1):
        a.append(int(a[-1]*b[i]/b[i-1]/(m-1))%z)
    for i in range(k+1):
        x = (x+a[i])%z
    print(x)

Fixed code:
n, m, k = map(int, input().split())
z = 998244353
if n <= k+1:
    a = 1
    for i in range(n):
        a = a*m%z
    print(a)
else:
    a = []
    b = []
    d = [1, 1]
    e = [1, 1]
    f = [0, 1]
    for i in range(2, n):
        d.append(d[-1]*i%z)
        f.append((-f[z%i]*(z//i))%z)
        e.append(e[-1]*f[-1]%z)
    for i in range(k+1):
        b.append(d[n-1]*e[i]*e[n-i-1]%z)
    x = 0
    g = [1]
    for i in range(n-1):
        g.append(g[i]*(m-1)%z)
    for i in range(k+1):
        a.append(b[i]*m*g[n-i-1]%z)
    for i in range(k+1):
        x = (x+a[i])%z
    print(x)
Buggy code:
#import numpy as np
import math
#from decimal import *
#from numba import njit
#@njit
def main():
    (N, M, K) = map(int, input().split())
    MOD = 998244353
    
    fact = [1]*(N+1)
    factinv = [1]*(N+1)
    for i in range(1,K+1):
        fact[i] = fact[i-1]*i % MOD
        factinv[i] = pow(fact[i], MOD-2, MOD)
    def comb(n, k):
        return fact[n] * factinv[k] * factinv[n-k] % MOD

    ans = 0
    for k in range(K+1):
        ans += (comb(N-1,k)*M*pow(M-1, N-k-1, MOD))%MOD

    print(ans%MOD)

main()


Fixed code:
#import numpy as np
import math
#from decimal import *
#from numba import njit
#@njit
def main():
    (N, M, K) = map(int, input().split())
    MOD = 998244353
    
    fact = [1]*(N+1)
    factinv = [1]*(N+1)
    for i in range(1,N+1):
        fact[i] = fact[i-1]*i % MOD
        factinv[i] = pow(fact[i], MOD-2, MOD)
    def comb(n, k):
        return fact[n] * factinv[k] * factinv[n-k] % MOD

    ans = 0
    for k in range(K+1):
        ans += (comb(N-1,k)*M*pow(M-1, N-k-1, MOD))%MOD

    print(ans%MOD)

main()
Buggy code:
N,M,K=map(int,input().split())

mod=998244353

fact=[1 for i in range(N+1)]
for i in range(1,N):
    fact[i+1]=(fact[i]*(i+1))%mod

def nCk(n,k):
    return fact[n]//fact[n-k]//fact[k]

result=0
for k in range(K+1):
    result+=nCk(N-1,k)*M*pow(M-1,N-k-1,mod)
    result=int(result)%mod
print(result)

Fixed code:
N,M,K=map(int,input().split())

mod=998244353

fact=[1 for i in range(N+1)]
for i in range(1,N):
    fact[i+1]=(fact[i]*(i+1))%mod

def nCk(n,k):
    return fact[n]*pow(fact[n-k]*fact[k],mod-2,mod)

result=0
for k in range(K+1):
    result+=nCk(N-1,k)*M*pow(M-1,N-k-1,mod)
    result=int(result)%mod
print(result)
Buggy code:
n,m,k=map(int,input().split())
ans=0
mod=998244353
c=1
for i in range(k+1):
    ans+=c*m*pow(m-1,n-i-1,mod)
    ans%=mod
    c*=max(1,n-i-1)//(i+1)
print(ans)

Fixed code:
n,m,k=map(int,input().split())
ans=0
mod=998244353
c=1
for i in range(k+1):
    ans+=c*m*pow(m-1,n-i-1,mod)
    ans%=mod
    c*=(n-i-1)*pow(i+1,mod-2,mod)
    c%=mod
print(ans)
Buggy code:
N,M,K = map(int,input().split())

mod = 10 ** 9 + 7
MAX = N+1
fac = [0] * MAX
finv = [0] * MAX
inv = [0] * MAX
def cominit():
    fac[0] = 1
    fac[1] = 1
    finv[0] = 1
    finv[1] = 1
    inv[1] = 1
    for i in range(2,MAX):
        fac[i] = fac[i-1] * i % mod
        inv[i] = mod - inv[mod%i] * (mod//i) % mod
        finv[i] = finv[i-1] * inv[i] % mod
def cmb(n,r):
    if n < 0 or r < 0 or r > n:return 0
    if r > n/2: r = n-r        
    return fac[n] * (finv[r] * finv[n-r] % mod) % mod
    
cominit()

MOD =  998244353
ans = pow(M,N,MOD)
minus = 0
for k  in range(K+1,N):
    minus += M * pow(M-1,N-k-1,MOD) * cmb(N-1,k)
    minus %= MOD

ans -= minus 
ans = (ans+MOD)%MOD
print(ans)

Fixed code:
N,M,K = map(int,input().split())

MOD =  998244353
mod = MOD
MAX = N+1
fac = [0] * MAX
finv = [0] * MAX
inv = [0] * MAX
def cominit():
    fac[0] = 1
    fac[1] = 1
    finv[0] = 1
    finv[1] = 1
    inv[1] = 1
    for i in range(2,MAX):
        fac[i] = fac[i-1] * i % mod
        inv[i] = mod - inv[mod%i] * (mod//i) % mod
        finv[i] = finv[i-1] * inv[i] % mod
def cmb(n,r):
    if n < 0 or r < 0 or r > n:return 0
    if r > n/2: r = n-r        
    return fac[n] * (finv[r] * finv[n-r] % mod) % mod
    
cominit()

ans = pow(M,N,MOD)
minus = 0
for k  in range(K+1,N):
    minus += M * pow(M-1,N-k-1,MOD) * cmb(N-1,k)
    minus %= MOD

ans -= minus 
ans = (ans+MOD)%MOD
print(ans)
Buggy code:
 = 998244353
n, m, k = map(int, input().split())
 
fact = [0]*(n+1)
fact[0] = 1
for i in range(1, n+1):
    fact[i] = fact[i-1]*i % MOD
 
invfact= [0]*(n+1)
for i in range(n+1):
    invfact[i] = pow(fact[i], MOD-2, MOD)
 
 
def nCk(n, k):
    return fact[n]*invfact[k]*invfact[n-k]
 
 
ans = 0
for i in range(k+1):
    ans += m*pow(m-1, n-1-i, MOD)*nCk(n-1, i)
    ans %= MOD
print(ans)

Fixed code:
MOD = 998244353
n, m, k = map(int, input().split())
 
fact = [0]*(n+1)
fact[0] = 1
for i in range(1, n+1):
    fact[i] = fact[i-1]*i % MOD
 
invfact = [0]*(n+1)
for i in range(n+1):
    invfact[i] = pow(fact[i], MOD-2, MOD)
 
 
def nCk(n, k):
    return fact[n]*invfact[k]*invfact[n-k]
 
 
ans = 0
for i in range(k+1):
    ans += m*pow(m-1, n-1-i, MOD)*nCk(n-1, i)
    ans %= MOD
print(ans)
Buggy code:
N, M, K = map(int, input().split())
MOD = 998244353
MAX_N = 10**6
fac = [0]*(MAX_N)
fac_inv = [0]*(MAX_N)
fac[0] = 1
for i in range(MAX_N-1):
    fac[i+1] = fac[i]*(i+1) % MOD
fac_inv[-1] = pow(fac[-1], MOD-2, MOD)
for i in range(MAX_N-2, -1, -1):
    fac_inv[i] = fac_inv[i+1]*(i+1) % MOD


def nCk(n, k):
    return fac[n] * fac_inv[k] * fac_inv[n-k]


ans = 0
for k in range(K+1):
    temp = M*nCk(N-1, k)*pow(M-1, N-K-1, MOD) % MOD
    ans += temp
    ans %= MOD
print(ans)


Fixed code:
N, M, K = map(int, input().split())
MOD = 998244353
MAX_N = 10**6
fac = [0]*(MAX_N)
fac_inv = [0]*(MAX_N)
fac[0] = 1
for i in range(MAX_N-1):
    fac[i+1] = fac[i]*(i+1) % MOD
fac_inv[-1] = pow(fac[-1], MOD-2, MOD)
for i in range(MAX_N-2, -1, -1):
    fac_inv[i] = fac_inv[i+1]*(i+1) % MOD


def nCk(n, k):
    return fac[n] * fac_inv[k] % MOD * fac_inv[n-k] % MOD


ans = 0
for k in range(K+1):
    # temp = M*nCk(N-1, k)*pow(M-1, N-K-1, MOD) % MOD
    temp = (M*nCk(N-1, k)) % MOD*pow(M-1, N-k-1, MOD) % MOD
    ans += temp
    ans %= MOD
print(ans)
Buggy code:
n,m,k=map(int,input().split())
mod=998244353
ans=(m*pow(m-1,n-1,mod))%mod
cntb=1
for i in range(1,k+1):
    cntb*=(n-i)//i
    ans+=m*pow(m-1,n-1-i,mod)*cntb
    ans%=mod
print(ans)

Fixed code:
n,m,k=map(int,input().split())
mod=998244353
cntb=1
ans=0
for i in range(k+1):
    ans=ans+(((m*pow(m-1,n-1-i,mod))%mod)*cntb)%mod
    ans=ans%mod
    cntb=(cntb*(n-1-i)*pow(i+1,mod-2,mod))%mod
print(ans%mod)
Buggy code:
n, m, k = [int(t) for t in open(0).read().split()]
mod = 998244353
mem = [0] * max(k + 3, n + 1)
for i in range(k + 2):
    mem[i] = pow(m, i, mod)
mem[k + 2] = (pow(m, k + 2, mod) + mod - m) % mod
for i in range(k + 3, n + 1):
    mem[i] = (m * mem[i - 1] - (m - 1) * mem[i - k - 2]) % mod
print(mem[n])


Fixed code:
n, m, k = [int(t) for t in open(0).read().split()]
mod = 998244353
mem = [0] * max(k + 3, n + 1)
for i in range(k + 2):
    mem[i] = pow(m, i, mod)
c = 1
for i in range(k + 2, n + 1):
    mem[i] = (m * mem[i - 1] - m * c * pow(m - 1, i - k - 2, mod)) % mod
    c = (c * (i - 1) * pow(i - k - 1, - 1, mod)) % mod
print(mem[n])
Buggy code:
n,m,k=[int(x) for x in input().split()]
#TODO Mutiply wih m
sumi=0
mod=998244353
prodi=pow(m-1,n-1,mod)
invi=pow(m-1,mod-2,mod)
for j in range(n-1,n-1-k-1,-1):
  sumi+=prodi
  sumi%=mod
  prodi*=n-j
  prodi%=mod
  prodi*=pow(j+1,mod-2,mod)
  prodi%=mod
  prodi*=invi
  prodi%=mod
sumi*=m
sumi%=mod
print(sumi)
  

Fixed code:
n,m,k=[int(x) for x in input().split()]
#TODO Mutiply wih m
if m==1:
  if k==n-1: print(1)
  else: print(0)
  exit()
sumi=0
mod=998244353
prodi=pow(m-1,n-1,mod)
invi=pow(m-1,mod-2,mod)
for j in range(n-1,n-1-k-1,-1):
  sumi+=prodi
  sumi%=mod
  prodi*=j
  prodi%=mod
  prodi*=pow(n-j,mod-2,mod)
  prodi%=mod
  prodi*=invi
  prodi%=mod 
sumi*=m
sumi%=mod
print(sumi)
Buggy code:
import sys
l = sys.stdin.readline().strip()
N, M, K = map(int, l.split())
comb_quick = {}
mod = 998244353
C = [1]
for i in range(1, (N-1) // 2 + 2):
    next = C[-1] * (N-i) // i
    C.append(next)

res = 0

exp_quick = [1]
for i in range(N):
    exp_quick.append(exp_quick[-1] * (M-1) % mod)
print("quick finished")
for x in range(0,K+1):
    crt =  exp_quick[N-x-1] * M
    crt %= mod
    #print("start")
    idx = min(x, N-1-x)
    crt *= C[idx]
    #print("end")
    crt %= mod
    res += crt
    res %= mod

print(res)

Fixed code:
import sys
l = sys.stdin.readline().strip()
N, M, K = map(int, l.split())
comb_quick = {}
mod = 998244353
C = [1]
for i in range(1, (N-1) // 2 + 2):
    next = C[-1] * (N-i) * pow(i, mod-2, mod)
    C.append(next % mod)

res = 0

for x in range(K+1):
    crt =  (pow(M-1, N-x-1, mod) * M) % mod 
    crt %= mod
    #print("start")
    idx = min(x, N-1-x)
    crt *= C[idx]
    #print("end")
    crt %= mod
    res += crt
    res %= mod

print(res)
Buggy code:
n,m,k = map(int, input().split())
#ncr
mod = 998244353
class Combination:
    def __init__(self, N):
        self.fac = [1] * (N + 1)
        for i in range(1, N + 1):
            self.fac[i] = (self.fac[i - 1] * i) % mod
        self.invmod = [1] * (N + 1)
        self.invmod[N] = pow(self.fac[N], mod - 2, mod)
        for i in range(N, 0, -1):
            self.invmod[i - 1] = (self.invmod[i] * i) % mod 
    def ncr(self, n, k):  # nCk
        return self.fac[n] * self.invmod[k] % mod * self.invmod[n - k] % mod 
cmb = Combination(n)
ans = 0
for i in range(k+1):
  a = m*(m-1)**(n-i-1)%p
  a *= cmb.ncr(n,i)
  ans += a%p
print(ans%p)

Fixed code:
n,m,k = map(int, input().split())
#ncr
mod = 998244353
class Combination:
    def __init__(self, N):
        self.fac = [1] * (N + 1)
        for i in range(1, N + 1):
            self.fac[i] = (self.fac[i - 1] * i) % mod
        self.invmod = [1] * (N + 1)
        self.invmod[N] = pow(self.fac[N], mod - 2, mod)
        for i in range(N, 0, -1):
            self.invmod[i - 1] = (self.invmod[i] * i) % mod 
    def ncr(self, n, k):  # nCk
        return self.fac[n] * self.invmod[k] % mod * self.invmod[n - k] % mod 
cmb = Combination(n)
ans = 0
for i in range(k+1):
  a = m*pow(m-1,n-i-1,mod)%mod
  a *= cmb.ncr(n-1,i)
  ans += a%mod
print(ans%mod)
Buggy code:
n,m,k=map(int,input().split())
mod=998244353
if m==1:print(0 if k!=n-1 else 1);exit()
mod=10**9+7
fact=[1]*(n-1+1)
inv=[1]*(n-1+1)
for i in range(2,n-1+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n-1,1,-1):
    inv[i-1]=inv[i]*i%mod
def comb(x,y):return fact[x]*inv[y]%mod*inv[x-y]%mod
ans=0
po=pow(m-1,n-1,mod)
ue=fact[n-1]
iii=pow(m-1,mod-2,mod)
for i in range(k+1):
    ans+=comb(n-1,i)*m*po%mod
    po*=iii
    po%=mod
print(ans%mod)

Fixed code:
n,m,k=map(int,input().split())
mod=998244353
if m==1:print(0 if k!=n-1 else 1);exit()
fact=[1]*(n-1+1)
inv=[1]*(n-1+1)
for i in range(2,n-1+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n-1,1,-1):
    inv[i-1]=inv[i]*i%mod
def comb(x,y):return fact[x]*inv[y]%mod*inv[x-y]%mod
ans=0
po=pow(m-1,n-1,mod)*m%mod
ue=fact[n-1]
iii=pow(m-1,mod-2,mod)%mod
for i in range(k+1):
    ans+=comb(n-1,i)*po%mod
    po*=iii
    po%=mod
print(ans%mod)
Buggy code:
n,m,k=map(int,input().split())
mod=998244353
if m==1:print(0 if k!=n-1 else 1);exit()
fact=[1]*(n-1+1)
inv=[1]*(n-1+1)
for i in range(2,n-1+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n-1,1,-1):
    inv[i-1]=inv[i]*i%mod
ans=0
po=pow(m-1,n-1,mod)
ue=fact[n-1]
iii=m*pow(m-1,mod-2,mod)%mod
for i in range(k+1):
    ans+=ue*inv[n-1-i]%mod*inv[i]%mod*po%mod
    po*=iii
print(ans%mod)


Fixed code:
n,m,k=map(int,input().split())
mod=998244353
if m==1:print(0 if k!=n-1 else 1);exit()
fact=[1]*(n-1+1)
inv=[1]*(n-1+1)
for i in range(2,n-1+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n-1,1,-1):
    inv[i-1]=inv[i]*i%mod
ans=0
po=pow(m-1,n-1,mod)*m%mod
ue=fact[n-1]
iii=pow(m-1,mod-2,mod)%mod
for i in range(k+1):
    ans+=ue*inv[n-1-i]%mod*inv[i]%mod*po%mod
    po*=iii
    po%=mod
print(ans%mod)
Buggy code:
def inverse(a, p):
    a_, p_ = a, p
    x, y = 1, 0
    while p_:
        t = a_ // p_
        a_ -= t * p_
        a_, p_ = p_, a_
        x -= t * y
        x, y = y, x
    x %= p
    return x


def solve():
    N, M, K = map(int, input().split())
    mod = 998244353

    nCi = [1]
    for i in range(1, N):
        nCi.append(nCi[i - 1] * (N - i) * inverse(i, mod) % mod)
        # nCi.append(nCi[i - 1] * (N - i + 1) // i)
    print(nCi)
    ans = 0
    for k in range(K + 1):
        n = N - k
        ans += M * int(pow((M - 1), n - 1, mod)) * nCi[k]
        ans %= mod
    print(ans)


if __name__ == '__main__':
    solve()


Fixed code:
def inverse(a, p):
    a_, p_ = a, p
    x, y = 1, 0
    while p_:
        t = a_ // p_
        a_ -= t * p_
        a_, p_ = p_, a_
        x -= t * y
        x, y = y, x
    x %= p
    return x


def solve():
    N, M, K = map(int, input().split())
    mod = 998244353

    nCi = [1]
    for i in range(1, N):
        nCi.append(nCi[i - 1] * (N - i) * inverse(i, mod) % mod)
    ans = 0
    for k in range(K + 1):
        n = N - k
        ans += M * int(pow((M - 1), n - 1, mod)) * nCi[k]
        ans %= mod
    print(ans)


if __name__ == '__main__':
    solve()
Buggy code:
R=range
M=998244353
n,m,k=map(int,input().split())
f=1
for i in R(n):f*=i+1
r=[pow(f,-1,M)]
for i in R(n):r+=r[i]*(n-i)%M,
print(sum(f//n*m*r[i-n]*r[~i]*pow(m-1,~i+n,M)for i in R(k+1))%M)

Fixed code:
R=range
M=998244353
n,m,k=map(int,input().split())
f=1
for i in R(n-1):f=f*-~i%M
r=[pow(f*n,-1,M)]
for i in R(n):r+=r[i]*(n-i)%M,
print(sum(m*f*r[i-n]*r[~i]*pow(m-1,~i+n,M)for i in R(k+1))%M)