Problem:


Score : 700 points

Problem Statement

Takahashi has a tower which is divided into N layers.
Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower.
He defines the beauty of the tower as follows:

The beauty of the tower is the sum of the scores of the N layers, where the score of a layer is A if the layer is painted red, A+B if the layer is painted green, B if the layer is painted blue, and 0 if the layer is uncolored.

Here, A and B are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors.

Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly K.
How many such ways are there to paint the tower? Find the count modulo 998244353.
Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other.

Constraints

1 ≤ N ≤ 3×10^5

1 ≤ A,B ≤ 3×10^5

0 ≤ K ≤ 18×10^{10}

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N A B K

Output

Print the number of the ways to paint tiles, modulo 998244353.

Sample Input 1

4 1 2 5

Sample Output 1

40

In this case, a red layer worth 1 points, a green layer worth 3 points and the blue layer worth 2 points. The beauty of the tower is 5 when we have one of the following sets of painted layers:

1 green, 1 blue

1 red, 2 blues

2 reds, 1 green

3 reds, 1 blue

The total number of the ways to produce them is 40.

Sample Input 2

2 5 6 0

Sample Output 2

1

The beauty of the tower is 0 only when all the layers are uncolored. Thus, the answer is 1.

Sample Input 3

90081 33447 90629 6391049189

Sample Output 3

577742975



Sample input:
4 1 2 5


Sample output:
40

Submission status: Wrong Answer

Solution code:
def comb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル
N=10**6
mod=998244353
for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

n,a,b,k=map(int,input().split())
ans=0
for s in range(n+1):
  bt=k-a*s
  if bt%b==0 and 0<=bt//b<=n:
    t=bt//b
    ans+=(comb(n,s,mod)*comb(n,t,mod))%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
MOD = 998244353

class Factorial:
    def __init__(self, n, mod):
        self.f = [1]
        self.mod = mod
        for j in range(1, n + 1):
            self.f.append(self.f[-1] * j % mod)
        self.i = [pow(self.f[-1], mod - 2, mod)]
        for j in range(n, 0, -1):
            self.i.append(self.i[-1] * j % mod)
        self.i.reverse()
    def factorial(self, j):
        return self.f[j]
    def ifactorial(self, j):
        return self.i[j]
    def comb(self, n, k):
        return self.f[n] * self.i[n - k] % self.mod * self.i[k] % self.mod if n >= k else 0

def gcd(a, b):
    if a > b:
        a, b = b, a
    if b % a == 0:
        return a
    return gcd(b % a, a)

N, A, B, K = map(int, input().split())

if A < B:
    A, B = B, A

# n = 2 * min(K // A + 2, N + 1) + 3
n = min((K + A - 1) // A, N) + 1
lcm = A * B // gcd(A, B)
step = lcm // A

lst = Factorial(N + 1, MOD) #O(N)
N_factrorial = lst.factorial(N)
ilst = np.array(lst.i, dtype = np.int64)

def calc(a, b):
    if a > b:
        a, b = b, a #必ずaのほうが小さくなる
    tmp = 0
    G = min(a, b) #緑色の最大値
    # print ('a=', a, 'b=', b)
    # print ('G=', G)
    if a == b:
        if N - a - b >= 0:
            tmp = sum(((ilst[a: : -1] * ilst[b:: -1] % MOD) * ilst[0: G + 1] % MOD) * ilst[N - a - b: N - a - b + G + 1] % MOD) % MOD
            tmp *= N_factrorial
        else:
            n = N - a - b
            if n < 0:
                return 0
            # print (n)
            tmp = sum(((ilst[a - n: :-1] * ilst[b - n: : -1] % MOD) * ilst[n: G + 1] % MOD) * ilst[0: N - a - b + G + 1] % MOD) % MOD
            tmp *= N_factrorial
    elif N - a - b >= 0:
        # print ('Y')
        # print (ilst[a: : -1])
        # print (ilst[b: b - G - 1: -1])
        # print (ilst[0: G + 1])
        # print (ilst[N - a - b: N - a - b + G + 1])
        tmp = sum(((ilst[a: : -1] * ilst[b: b - G - 1: -1] % MOD) * ilst[0: G + 1] % MOD) * ilst[N - a - b: N - a - b + G + 1] % MOD) % MOD
        tmp *= N_factrorial
    else:
        # print ('N')
        n = a + b - N #少なくともn枚は緑色を作らないとだめ
        # print (n)
        if n > G:
            # print ('&')
            return 0
        # print (n)
        tmp = sum(((ilst[a - n: :-1] * ilst[b - n: b - G - 1: -1] % MOD) * ilst[n: G + 1] % MOD) * ilst[0: N - a - b + G + 1] % MOD) % MOD
        tmp *= N_factrorial
    # print ('tmp=', tmp)
    tmp %= MOD
    return tmp

def count(a, b):
    if a + b > 2 * N:
        return 0
    # print ('to calc')
    return calc(a, b)

def maike_satrt(step):
    for i in range(step):
        if (K - A * i) % B == 0:
            return i

start = maike_satrt(step)
# print (start, n)
ans = 0
for a in range(start, n, step): #Aの数
    if A * a > K:
        break
    # if (K - A * a) % B != 0:
    #     continue
    b = (K - A * a) // B
    # print ('a=', a, 'b=', b)
    ans += count(a, b)
    ans %= MOD

if K == 0:
    print (1)
    exit()

print (ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
MOD = 998244353

class Factorial:
    def __init__(self, n, mod):
        self.f = [1]
        self.mod = mod
        for j in range(1, n + 1):
            self.f.append(self.f[-1] * j % mod)
        self.i = [pow(self.f[-1], mod - 2, mod)]
        for j in range(n, 0, -1):
            self.i.append(self.i[-1] * j % mod)
        self.i.reverse()
    def factorial(self, j):
        return self.f[j]
    def ifactorial(self, j):
        return self.i[j]
    def comb(self, n, k):
        return self.f[n] * self.i[n - k] % self.mod * self.i[k] % self.mod if n >= k else 0

def gcd(a, b):
    if a > b:
        a, b = b, a
    if b % a == 0:
        return a
    return gcd(b % a, a)

N, A, B, K = map(int, input().split())

if A < B:
    A, B = B, A

# n = 2 * min(K // A + 2, N + 1) + 3
n = min((K + A - 1) // A, N) + 1
lcm = A * B // gcd(A, B)
step = lcm // A

lst = Factorial(N + 1, MOD) #O(N)
N_factrorial = lst.factorial(N)
ilst = np.array(lst.i, dtype = np.int64)

def calc(a, b):
    if a > b:
        a, b = b, a #必ずaのほうが小さくなる
    tmp = 0
    G = min(a, b) #緑色の最大値
    # print ('a=', a, 'b=', b)
    # print ('G=', G)
    if a == b:
        if N - a - b >= 0:
            tmp = sum(((ilst[a: : -1] * ilst[b:: -1] % MOD) * ilst[0: G + 1] % MOD) * ilst[N - a - b: N - a - b + G + 1] % MOD) % MOD
            tmp *= N_factrorial
        else:
            n = N - a - b
            if n < 0:
                return 0
            # print (n)
            tmp = sum(((ilst[a - n: :-1] * ilst[b - n: : -1] % MOD) * ilst[n: G + 1] % MOD) * ilst[0: N - a - b + G + 1] % MOD) % MOD
            tmp *= N_factrorial
    elif N - a - b >= 0:
        # print ('Y')
        # print (ilst[a: : -1])
        # print (ilst[b: b - G - 1: -1])
        # print (ilst[0: G + 1])
        # print (ilst[N - a - b: N - a - b + G + 1])
        tmp = sum(((ilst[a: : -1] * ilst[b: b - G - 1: -1] % MOD) * ilst[0: G + 1] % MOD) * ilst[N - a - b: N - a - b + G + 1] % MOD) % MOD
        tmp *= N_factrorial
    else:
        # print ('N')
        n = a + b - N #少なくともn枚は緑色を作らないとだめ
        # print (n)
        if n > G:
            # print ('&')
            return 0
        # print (n)
        tmp = sum(((ilst[a - n: :-1] * ilst[b - n: b - G - 1: -1] % MOD) * ilst[n: G + 1] % MOD) * ilst[0: N - a - b + G + 1] % MOD) % MOD
        tmp *= N_factrorial
    # print ('tmp=', tmp)
    tmp %= MOD
    return tmp

def count(a, b):
    if a + b > 2 * N:
        return 0
    # print ('to calc')
    return calc(a, b)

def maike_satrt(step):
    for i in range(step):
        if (K - A * i) % B == 0:
            return i

start = maike_satrt(step)
# print (start, n)
ans = 0
for a in range(start, n, step): #Aの数
    if A * a > K:
        break
    # if (K - A * a) % B != 0:
    #     continue
    b = (K - A * a) // B
    # print ('a=', a, 'b=', b)
    ans += count(a, b)
    ans %= MOD

print (ans)

Submission status: Wrong Answer

Solution code:
import sys
mod=998244353
MAX=3*10**5+100
g1=[1,1]
g2=[1,1]
for i in range(2,MAX+1):
    num_1=g1[-1]*i%mod
    g1.append(num_1)
    g2.append(pow(num_1,mod-2,mod))
def cmb(n,r):
    return g1[n]*g2[r]*g2[n-r]%mod

N,A,B,K=map(int,input().split())
if A==B:
    if K%A!=0:
        print(0)
        sys.exit()
    x=K//A
    ans=0
    if x%2==0:
        zzz=x//2-1
    else:
        zzz=x//2
    for i in range(min(N,x),zzz,-1):
        ans=(ans+cmb(N,i)*cmb(i,x-i)%mod)%mod
    print(ans)
    sys.exit()
data=[]
for x in range(N+1):
    if (K-A*x)%B!=0:
        continue
    y=(K-A*x)//B
    if 0<=x<=N and 0<=y<=N:
        data.append([x,y])
ans=0
for x,y in data:
    ans=(ans+cmb(N,x)*cmb(N,y)%mod)%mod
print(ans)

Submission status: Wrong Answer

Solution code:
MOD = 998244353
n, a, b, k = map(int, input().split())
fac = [1] * (n + 1)
for i in range(n):
    fac[i + 1] = fac[i] * (i + 1) % MOD


def comb(n, k):
    return fac[n] * pow(fac[n - k], MOD - 2, MOD) * pow(fac[k], MOD - 2, MOD)


ans = 0
for x in range(n):
    if k - x * a < 0 or (k - x * a) % b:
        continue
    y = (k - x * a) // b
    if n < y:
        continue
    ans += (comb(n, x) * comb(n, y)) % MOD
print(ans % MOD)

Submission status: Wrong Answer

Solution code:
mod = 998244353

def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

def power(x, y):
    if   y == 0     : return 1
    elif y == 1     : return x % mod
    elif y % 2 == 0 : return power(x, y//2)**2 % mod
    else            : return power(x, y//2)**2 * x % mod

def div(a, b):
    return mul(a, power(b, mod-2))


def inverse_max(a): #下2つを使用する際の下準備

    global inverse_lis
    inverse_lis = [0]
    
    inverse = 1
    for i in range(a+1):

        i += 2
        inverse_lis.append(inverse)
        inverse = mod -inverse_lis[mod % i] * (mod // i) % mod

    return

def moddiv(a,b):
    return mul(a , inverse_lis[b])
    

def mod_nCr(n,r):

    if r * 2 > n:
        r = n-r

    now = 1
    for i in range(r):
        now *= n - i
        now *= inverse_lis[r-i]
        now %= mod
    return now



N,A,B,K = map(int,input().split())
inverse_max(N)
ans = 0

#print (mod_nCr(100,2))

for i in range(N+1):

    if (K - A*i) % B == 0 :
        bnum = (K-A*i) // B
        if bnum > N:
            continue

        ans += mod_nCr(N,i) * mod_nCr(N,bnum)
        ans %= mod

print (ans)

Submission status: Wrong Answer

Solution code:
mod = 998244353

def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

def power(x, y):
    if   y == 0     : return 1
    elif y == 1     : return x % mod
    elif y % 2 == 0 : return power(x, y//2)**2 % mod
    else            : return power(x, y//2)**2 * x % mod

def div(a, b):
    return mul(a, power(b, mod-2))


def inverse_max(a): #下2つを使用する際の下準備

    global inverse_lis
    inverse_lis = [0]
    
    inverse = 1
    for i in range(a+1):

        i += 2
        inverse_lis.append(inverse)
        inverse = mod -inverse_lis[mod % i] * (mod // i) % mod

    return

def moddiv(a,b):
    return mul(a , inverse_lis[b])
    

def mod_nCr(n,r):

    if r * 2 > n:
        r = n-r

    now = 1
    for i in range(r):
        now *= n - i
        now *= inverse_lis[r-i]
        now %= mod
    return now



N,A,B,K = map(int,input().split())
inverse_max(N)
ans = 0

for i in range(N+1):

    if (K - A*i) % B == 0:
        bnum = (K-A*i) // B

        ans += mod_nCr(N,i) * mod_nCr(N,bnum)
        ans %= mod

print (ans)

Submission status: Wrong Answer

Solution code:
mod = 998244353
N, A, B, K = [int(_) for _ in input().split()]


def extgcd(a, b):
    if b:
        x, y = extgcd(b, a % b)
        x, y = y, x - (a // b) * y
    else:
        x, y = 1, 0
    return x, y


E = extgcd(A, B)
g = A * E[0] + B * E[1]
l = A * B // g
F = [E[0] * (K // g), E[1] * (K // g)]
if F[0] > F[1]:
    F = F[::-1]
    A, B = B, A
H = [F[0] + B * (F[1] // A), F[1] - A * (F[1] // A)]
L = []
for i in range(max(H[0] // (B // g), H[1] // (A // g)) + 10):
    J = [H[0] - (B // g) * i, H[1] + (A // g) * i]
    if 0 <= J[0] <= N and 0 <= J[1] <= N:
        L += [J]
f = [1] * (N + 1)
for i in range(2, N + 1):
    f[i] = f[i - 1] * i % mod
fi = [pow(f[-1], mod - 2, mod)]
for i in range(N, 0, -1):
    fi += [fi[-1] * i % mod]
fi = fi[::-1]


def comb(x, y):
    return (f[x] * fi[y] * fi[x - y]) % mod


ans = 0
for pq in L:
    p, q = pq
    ans += comb(N, p) * comb(N, q)
    ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 998244353
N, A, B, K = [int(_) for _ in input().split()]


def extgcd(a, b):
    if b:
        x, y = extgcd(b, a % b)
        x, y = y, x - (a // b) * y
    else:
        x, y = 1, 0
    return x, y


E = extgcd(A, B)
g = A * E[0] + B * E[1]
l = A * B // g
F = [E[0] * (K // g), E[1] * (K // g)]
if F[0] > F[1]:
    F = F[::-1]
    A, B = B, A
H = [F[0] + B * (F[1] // A), F[1] - A * (F[1] // A)]
L = []
for i in range(H[0] // (B // g) + 1):
    J = [H[0] - (B // g) * i, H[1] + (A // g) * i]
    if 0 <= J[0] <= N and 0 <= J[1] <= N:
        L += [J]
f = [1] * (N + 1)
for i in range(2, N + 1):
    f[i] = f[i - 1] * i % mod
fi = [pow(f[-1], mod - 2, mod)]
for i in range(N, 0, -1):
    fi += [fi[-1] * i % mod]
fi = fi[::-1]


def comb(x, y):
    return (f[x] * fi[y] * fi[x - y]) % mod


ans = 0
for pq in L:
    p, q = pq
    ans += comb(N, p) * comb(N, q)
    ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 998244353
N, A, B, K = [int(_) for _ in input().split()]


def extgcd(a, b):
    if b:
        x, y = extgcd(b, a % b)
        x, y = y, x - (a // b) * y
    else:
        x, y = 1, 0
    return x, y


E = extgcd(A, B)
g = A * E[0] + B * E[1]
F = [E[0] * (K // g), E[1] * (K // g)]
if F[0] > F[1]:
    F = F[::-1]
    A, B = B, A
H = [F[0] + B * (F[1] // A), F[1] - A * (F[1] // A)]
L = []
for i in range(H[0] // B + 1):
    J = [H[0] - B * i, H[1] + A * i]
    if 0 <= J[0] <= N and 0 <= J[1] <= N:
        L += [J]

f = [1] * (N + 1)
for i in range(2, N + 1):
    f[i] = f[i - 1] * i % mod
fi = [pow(f[-1], mod - 2, mod)]
for i in range(N, 0, -1):
    fi += [fi[-1] * i % mod]
fi = fi[::-1]


def comb(x, y):
    return (f[x] * fi[y] * fi[x - y]) % mod


ans = 0
for pq in L:
    p, q = pq
    ans += comb(N, p) * comb(N, q)
    ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 998244353
N, A, B, K = [int(_) for _ in input().split()]
A, B = sorted([A, B])


def extgcd(a, b):
    if b:
        x, y = extgcd(b, a % b)
        x, y = y, x - (a // b) * y
    else:
        x, y = 1, 0
    return x, y


E = extgcd(A, B)
g = A * E[0] + B * E[1]
F = [E[0] * (K // g), E[1] * (K // g)]
H = [F[0] + B * (F[1] // A), F[1] - A * (F[1] // A)]
L = []
for i in range(H[0] // B + 1):
    J = [H[0] - B * i, H[1] + A * i]
    if 0 <= J[0] <= N and 0 <= J[1] <= N:
        L += [J]

f = [1] * (N + 1)
for i in range(2, N + 1):
    f[i] = f[i - 1] * i % mod
fi = [pow(f[-1], mod - 2, mod)]
for i in range(N, 0, -1):
    fi += [fi[-1] * i % mod]
fi = fi[::-1]


def comb(x, y):
    return (f[x] * fi[y] * fi[x - y]) % mod


ans = 0
for pq in L:
    p, q = pq
    ans += comb(N, p) * comb(N, q)
    ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 998244353
N, A, B, K = [int(_) for _ in input().split()]
A, B = sorted([A, B])


def extgcd(a, b):
    if b:
        x, y = extgcd(b, a % b)
        x, y = y, x - (a // b) * y
    else:
        x, y = 1, 0
    return x, y


E = extgcd(A, B)
g = A * E[0] + B * E[1]
if K % g:
    print(0)
    exit()
F = [E[0] * (K // g), E[1] * (K // g)]
H = [F[0] + B * (F[1] // A), F[1] - A * (F[1] // A)]
L = []
for i in range(H[0] // B + 1):
    J = [H[0] - B * i, H[1] + A * i]
    if 0 <= J[0] <= N and 0 <= J[1] <= N:
        L += [J]

f = [1] * (N + 1)
for i in range(2, N + 1):
    f[i] = f[i - 1] * i % mod
fi = [pow(f[-1], mod - 2, mod)]
for i in range(N, 0, -1):
    fi += [fi[-1] * i % mod]
fi = fi[::-1]


def comb(x, y):
    return (f[x] * fi[y] * fi[x - y]) % mod


ans = 0
for pq in L:
    p, q = pq
    ans += comb(N, p) * comb(N, q)
    ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
#ライブラリインポート
from collections import defaultdict
con = 998244353
INF = float("inf")

def getlist():
	return list(map(int, input().split()))

N, A, B, K = getlist()

fac = [0] * (N + 1)
inv = [0] * (N + 1)
finv = [0] * (N + 1)
fac[0], fac[1] = 1, 1
inv[1] = 1
finv[0], finv[1] = 1, 1

# 前計算
for i in range(2, N + 1):
	fac[i] = fac[i - 1] * i % con
	inv[i] = inv[con % i] * (con - (con // i)) % con
	finv[i] = finv[i - 1] * inv[i] % con

#処理内容
def main():
	ans = 0
	for i in range(N):
		if (K - i * A) % B == 0:
			j = int((K - i * A) // B)
			if 0 <= j and j <= N:
				ans += (fac[N] * finv[i] * finv[N - i] * fac[N] * finv[j] * finv[N - j]) % con
				ans %= con

	#出力
	print(ans)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
N, A, B, K = map(int, input().split())
MOD = 998244353

if K == 0:
    print(1)
    exit()

class Combination:
    def __init__(self, size, mod=10**9 + 7):
        self.size = size + 2
        self.mod = mod
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % self.mod
            self.inv[i] = -self.inv[self.mod % i] * (self.mod // i) % self.mod
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % self.mod

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.mod

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.mod) % self.mod

    def factN(self, n):
        if n < 0:
            return 0
        return self.fact[n]

comb = Combination(N + 100, MOD)

ans = 0
for a in range(K // A):
    b = (K - a * A) // B

    if a * A + b * B == K:
        ans += comb.ncr(N, a) * comb.ncr(N, b)
        ans %= MOD

print(ans)

Submission status: Wrong Answer

Solution code:
n,a,b,k=map(int,input().split())
if k==0:
    print(1)
    quit()
mod=998244353
fra=[1]*(n+2)
inv=[1]*(n+2)
t1=1
t2=1
for i in range(1,n+2):
    t1*=i
    t1%=mod
    t2*=pow(i,mod-2,mod)
    t2%=mod
    fra[i]=t1
    inv[i]=t2
def comb(n,r):
    return fra[n]*inv[n-r]*inv[r]%mod
ans=0
for i in range(max(0,-(-(k-b*n)//a)),min(n+1,k//a)):
    t=k-i*a
    if t>b*n or t%b!=0:
        continue
    else:
        t//=b
        ans+=(comb(n,i)*comb(n,t))%mod
        ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
n,a,b,k=map(int,input().split())
if k==0:
    print(0)
    quit()
mod=998244353
fra=[1]*(n+2)
inv=[1]*(n+2)
t1=1
t2=1
for i in range(1,n+2):
    t1*=i
    t1%=mod
    t2*=pow(i,mod-2,mod)
    t2%=mod
    fra[i]=t1
    inv[i]=t2
def comb(n,r):
    return fra[n]*inv[n-r]*inv[r]%mod
ans=0
for i in range(n+1):
    t=k-i*a
    if t>b*n or t<0 or t%b!=0:
        continue
    else:
        t//=b
        ans+=(comb(n,i)*comb(n,t))%mod
        ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
n,a,b,k=map(int,input().split())
if k==0:
    print(0)
    quit()
mod=998244353
fra=[1]*(n+2)
inv=[1]*(n+2)
t1=1
t2=1
for i in range(1,n+2):
    t1*=i
    t1%=mod
    t2*=pow(i,mod-2,mod)
    t2%=mod
    fra[i]=t1
    inv[i]=t2
def comb(n,r):
    return fra[n]*inv[n-r]*inv[r]%mod
ans=0
for i in range(n+1):
    t=k-i*a
    if t>b*n or t<0 or t%b!=0:
        continue
    else:
        t//=b
        ans+=(comb(n,i)*comb(n,t))%mod
        ans%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


# BEGIN CUT HERE
MOD = 10**9 + 7
# MOD = 998244353  # AGC025_B


class Modint():
    '''
    自動でMODで割った余りを計算してくれる整数。MODは素数である必要があり，デフォルトは10**9+7。
    Parameters
    ----------
        n (int) : Modintの初期値
        MOD=10^9+7 (int) : 余りを計算するMOD。素数である必要がある。
    '''

    def __init__(self, n: int, mod=MOD):
        if n >= 0:
            self.n = n % mod
        else:
            self.n = (mod - (-n) % mod) % mod
        self.mod = mod

    def __eq__(self, other):
        return self.n == int(other)

    def __ne__(self, other):
        return not self == other

    def __neg__(self):
        return self.__class__(-self.n, self.mod)

    def __str__(self):
        return str(self.n)

    def __int__(self):
        return self.n

    def __iadd__(self, other):
        self.n += int(other)
        self.n %= self.mod
        return self

    def __radd__(self, other):
        self.n += int(other)
        self.n %= self.mod
        return self

    def __isub__(self, other):
        self.n -= int(other)
        self.n %= self.mod
        return self

    def __rsub__(self, other):
        self.n -= int(other)
        self.n %= self.mod
        return self

    def __imul__(self, other):
        self.n = self.n * int(other) % self.mod
        return self

    def __rmul__(self, other):
        self.n = self.n * int(other) % self.mod
        return self

    def inverse(self):
        '''
        フェルマーの小定理を考えると，MOD-2乗することでMODでの逆元を求めることができる。これは組み込み関数のpowを使うと十分に早い（O(log MOD)）。
        '''
        return pow(self.n, self.mod - 2, self.mod)

    def __ifloordiv__(self, other):
        self *= pow(int(other), self.mod - 2, self.mod)
        return self

    def __rfloordiv__(self, other):
        self *= pow(int(other), self.mod - 2, self.mod)
        return self

    def __add__(self, other):
        return self.__class__(self.n + int(other))

    def __sub__(self, other):
        return self.__class__(self.n - int(other))

    def __mul__(self, other):
        return self.__class__(self.n * int(other))

    def __floordiv__(self, other):
        return self.__class__(self.n * Modint(int(other)).inverse())

    # def comb(self, n: int, k: int):
    #     '''
    #     コンビネーション絡みの定義。comb(n,k):特に前処理をせずnCkを求める
    #     毎回O(N)かかるので死ぬ覚悟でO(N^2)を投げるくらいしか無理

    #     ちなみにですが，これでABC132_D()を出すとPyPy3で1932msくらいですれすれセーフ
    #     '''
    #     if n < k or k < 0:
    #         return Modint(0)
    #     res = Modint(1)
    #     for i in range(int(k)):
    #         res *= Modint(n - i)
    #         res //= Modint(i + 1)
    #     return res

    # def H(self, n: int, k: int):
    #     if n < 0 or k < 0:
    #         return Modint(0)
    #     elif n == 0 and k == 0:
    #         return Modint(1)
    #     else:
    #         return self.comb(n + k - 1, n)


class Combination():
    def __init__(self, sz):
        '''
        Nまでの階乗とその逆元（割り算用）を計算しておく。前処理にO(N)，P, C, Hそれぞれの計算はO(1)。N = 200000なら割と余裕で通る。
        '''
        self._fact = [Modint(0)] * (sz + 1)
        self._rfact = [Modint(0)] * (sz + 1)
        self._fact[0] = Modint(1)
        self._rfact[sz] = Modint(1)
        for i in range(1, sz + 1):
            self._fact[i] = self._fact[i - 1] * Modint(i)
        self._rfact[sz] = self._fact[sz].inverse()
        for i in range(sz - 1, -1, -1):
            self._rfact[i] = self._rfact[i + 1] * Modint(i + 1)

    def fact(self, k: int) -> int:
        return self._fact[k]

    def rfact(self, k: int) -> int:
        return self._rfact[k]

    def P(self, n: int, k: int) -> Modint:
        '''
        nPkの値を返す。nはあらかじめ前処理されたN以下でないとダメ
        Parameters
        ----------
            n (int) : nPkのn (0<=n<=N)
            k (int) : nPkのk （0<=k<=n）
        Returns
        ----------
            Modint : nPkをModint型で返す。Modintに依存。
        '''
        if k < 0 or n < k:
            return 0
        return self._fact[n] * self._rfact[n - k]

    def C(self, n: int, k: int) -> Modint:
        '''
        nCkの値を返す。nはあらかじめ前処理されたN以下でないとダメ
        Parameters
        ----------
            n (int) : nCkのn(0<=n<=N)
            k (int) : nCkのk（0<=k<=n）
        Returns
        ----------
            Modint : nCkをModint型で返す。Modintに依存。
        '''
        if k < 0 or n < k:
            return 0
        return self._fact[n] * self._rfact[k] * self._rfact[n - k]

    def H(self, n: int, k: int) -> Modint:
        '''
        nHkの値を返す。nはあらかじめ前処理されたN以下でないとダメ

        ちなみにnHkはn個の区別できないものをk個の箱に分けるパターンの数で，結局n+k-1Cnになる。
        Parameters
        ----------
            n (int) : nHkのn(0<=n<=N)
            k (int) : nHkのk（0<=k<=n）
        Returns
        ----------
            Modint : nHkをModint型で返す。Modintに依存。
        '''
        if n < 0 or k < 0:
            return 0
        elif k == 0:
            return 1
        return self.C(n + k - 1, n)

# END CUT HERE


def ABC132_D():
    N, K = mi()
    res = Combination(N)
    for i in range(1, K + 1):
        print(res.C(K - 1, i - 1) * res.H(N - K - i + 1, i + 1))
# verified on 2019/07/01
# https://atcoder.jp/contests/abc132/tasks/abc132_d
# Python3: 32ms https://atcoder.jp/contests/abc132/submissions/6200580
# PyPy3だとオーバヘッドが大きいのか240msくらいかかるけど，基本はPyPyで良いと思う。


def AGC025_B():
    N, A, B, K = mi()
    res = Combination(N)
    ans = Modint(0)
    for a in range(min(K // A + 1, N + 1)):
        b = (K - a * A) // B
        if A * a + B * b == K and a >= 0 and b >= 0:
            ans += res.C(N, a) * res.C(N, b)
    print(ans)
# verified on 2019/07/01
# https://atcoder.jp/contests/agc025/tasks/agc025_b
# PyPy3: https://atcoder.jp/contests/agc025/submissions/6200598
# Python3だとTLE


def ABC130_E():
    N, M = mi()
    S = lmi()
    T = lmi()
    a = [[Modint(0) for __ in range(M + 1)] for _ in range(N + 1)]
    for i in range(N + 1):
        for j in range(M + 1):
            if i == 0 or j == 0:
                a[i][j] = Modint(1)
            else:
                a[i][j] = a[i - 1][j] + a[i][j - 1] - \
                    (a[i - 1][j - 1] if S[i - 1] != T[j - 1] else 0)
    print(a[N][M])
# verified on 2019/07/01
# https://atcoder.jp/contests/abc130/tasks/abc130_e
# PyPy3: 1839ms https://atcoder.jp/contests/abc130/submissions/6204339
# やっぱりクソ遅い


if __name__ == '__main__':
    # ABC132_D()
    AGC025_B()
    # ABC130_E()