Problem p03332 (python)

Fixed submission:
n, a, b, k = map(int, input().split())

mod = 998244353
fact = [1] + [0] * n
inv_fact = [0] * (n+1)
for i in range(n):
    fact[i+1] = (fact[i] * (i+1)) % mod

for i in range(n+1):
    inv_fact[i] = pow(fact[i],mod-2,mod)
    
def cb(n, k, mod):
    return (fact[n] * inv_fact[k] * inv_fact[n-k]) % mod

ans = 0
for x in range(n+1):
    if (k-x*a) % b == 0:
        y = (k-x*a)//b
        if y >= 0:
            if x > n or y > n:
                pass
            else:
                ans += cb(n, x, mod) * cb(n, y, mod)
                ans %= mod
    
print(ans)

Buggy submission (verdict: Runtime Error):
n, a, b, k = map(int, input().split())

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)
    
def mod_inv(a, mod):
    return (pow(a,mod-2,mod))

def mod_combination(n, k, mod):
    return (fact[n]*mod_inv(fact[k], mod)*mod_inv(fact[n-k], mod)) % mod

mod = 998244353
fact = [1] + [0] * n
for i in range(n):
    fact[i+1] = (fact[i] * (i+1)) % mod

ans = 0
for x in range(n+1):
    if (k-x*a) % b == 0:
        y = (k-x*a)//b
        if y >= 0:
            ans += mod_combination(n, x, mod) * mod_combination(n, y, mod)
            ans %= mod

    
print(ans)

Buggy submission (verdict: Runtime Error):
n, a, b, k = map(int, input().split())

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)
    
def mod_inv(a, mod):
    return (pow(a,mod-2,mod))

def mod_combination(n, k, mod):
    return (fact[n]*mod_inv(fact[k], mod)*mod_inv(fact[n-k], mod)) % mod

g = gcd(k, gcd(a, b))
a //= g
b //= g
k //= g

mod = 998244353
fact = [1] + [0] * n
for i in range(n):
    fact[i+1] = (fact[i] * (i+1)) % mod
if a < b:
    tmp = a
    a = b
    b = tmp

ans = 0
for x in range(max(-int(-(k-b*n)//a),0), k//a+1):
    if (k-x*a) % b == 0:
        y = (k-x*a)//b
        ans += mod_combination(n, x, mod) * mod_combination(n, y, mod)
        ans %= mod
    
print(ans)

Buggy submission (verdict: Runtime Error):
n, a, b, k = map(int, input().split())

if False:
    n = 4
    a = 1
    b = 2
    k = 5

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)
    
def mod_inv(a, mod):
    return (pow(a,mod-2,mod))

def mod_combination(n, k, mod):
    return fact[n]*mod_inv(fact[k], mod)*mod_inv(fact[n-k], mod) % mod

g = gcd(k, gcd(a, b))
a //= g
b //= g
k //= g

mod = 998244353
fact = [1] + [0] * n
for i in range(n):
    fact[i+1] = (fact[i] * (i+1)) % mod
# print(fact)
if a < b:
    tmp = a
    a = b
    b = tmp

ans = 0
# print("n, a, b =", n, a, b)
# print((k-b*n)//a, k//a+1)
for x in range(max(-int(-(k-b*n)//a),0), k//a+1):
    if (k-x*a) % b == 0:
        y = (k-x*a)//b
        
        # print("n, x, y, mod", n, x, y, mod)
        # print(mod_combination(n, x, mod))
        # print(mod_combination(n, y, mod))
        
        ans += mod_combination(n, x, mod) * mod_combination(n, y, mod)
        ans %= mod
    
print(ans)

Buggy submission (verdict: Runtime Error):
n, a, b, k = map(int, input().split())

if False:
    n = 4
    a = 1
    b = 2
    k = 5

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)
    
def mod_combinationaaaaaaaaa(n, k, mod):
    # nCk (mod m)
    def mod_permutationaaa(n, k, mod):
        if n<=k:
            return 1
        else:
            return (n * mod_permutation(n-1,k,mod))%mod

    def mod_inv_permutationaaa(k, mod):
        k, mod = int(k), int(mod)
        if k<=1:
            return 1
        else:
            return (pow(k,mod-2,mod) * mod_inv_permutation(k-1, mod))%mod

    return (mod_permutation(n,n-k,mod) * mod_inv_permutation(k, mod))%mod

def mod_combination(N, K, M):
    def mod_permutation(n, k, mod):
        ret = 1
        for i in range(n, n-k, -1):
            ret *= i
            ret %= mod
        print("P =", ret)
        return ret

    def mod_inv(k, mod):
        return (pow(k,mod-2,mod))
    
    def mod_inv_permutation(n, k, mod):
        return mod_inv(mod_permutation(n, k, mod), mod)

    return (mod_permutation(N,K,M) * mod_inv_permutation(K, K, M))%M

g = gcd(k, gcd(a, b))
a //= g
b //= g
k //= g

if a < b:
    tmp = a
    a = b
    b = tmp

mod = 998244353
ans = 0
print("n, a, b =", n, a, b)
print((k-b*n)//a, k//a+1)
for x in range(max(-int(-(k-b*n)//a),0), k//a+1):
    if (k-x*a) % b == 0:
        print("n, x, y, mod", n, x, y, mod)
        print(mod_combination(n, x, mod))
        print(mod_combination(n, y, mod))
        
        y = (k-x*a)//b
        ans += mod_combination(n, x, mod) * mod_combination(n, y, mod)
        ans %= mod
    
print(ans)