Problem: 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.

Submission attempts (in order):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np
from scipy.misc import comb

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 998244353


def get_factorials(max, mod=None):
    
    ret = [1]
    n = 1
    if mod:
        for i in range(1, max + 1):
            n *= i
            n %= mod
            ret.append(n)
    else:
        for i in range(1, max + 1):
            n *= i
            ret.append(n)
    return ret


def mod_invs(max, mod):
    
    invs = [1] * (max + 1)
    for x in range(2, max + 1):
        invs[x] = (-(mod // x) * invs[mod % x]) % mod
    return invs


def factorial_invs(max, mod):
    
    ret = []
    r = 1
    for inv in mod_invs(max, mod):
        r = r * inv % mod
        ret.append(r)
    return ret


class Combination:
    def __init__(self, max, mod):
        
        self._factorials = get_factorials(max, mod)
        self._finvs = factorial_invs(max, mod)
        self._mod = mod

    def ncr(self, n, r):
        """
        :param int n:
        :param int r:
        :rtype: int
        """
        if n < r:
            return 0
        return self._factorials[n] * self._finvs[r] % self._mod * self._finvs[n - r] % self._mod


N, A, B, K = list(map(int, sys.stdin.readline().split()))
comb = Combination(max=N, mod=MOD)
ans = 0

for a in range(N + 1):
    if (K - a * A) % B != 0:
        continue
    b = int((K - a * A) // B)
    if not (0 <= a <= N and 0 <= b <= N):
        continue

    s = 0
    
    c = min(a, b)
    # A
    a -= c
    # B
    b -= c
    
    d = N - a - b - c
    
    while c >= 0 and d >= 0:
        s += comb.ncr(N, a) * comb.ncr(N - a, b) * comb.ncr(N - a - b, c)
        a += 1
        b += 1
        c -= 1
        d -= 1

    ans += s
    ans %= MOD
print(ans)


import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 998244353


def get_factorials(max, mod=None):
    
    ret = [1]
    n = 1
    if mod:
        for i in range(1, max + 1):
            n *= i
            n %= mod
            ret.append(n)
    else:
        for i in range(1, max + 1):
            n *= i
            ret.append(n)
    return ret


def mod_invs(max, mod):
    
    invs = [1] * (max + 1)
    for x in range(2, max + 1):
        invs[x] = (-(mod // x) * invs[mod % x]) % mod
    return invs


def factorial_invs(max, mod):
    
    ret = []
    r = 1
    for inv in mod_invs(max, mod):
        r = r * inv % mod
        ret.append(r)
    return ret


class Combination:
    def __init__(self, max, mod):
        
        self._factorials = get_factorials(max, mod)
        self._finvs = factorial_invs(max, mod)
        self._mod = mod

    def ncr(self, n, r):
        """
        :param int n:
        :param int r:
        :rtype: int
        """
        if n < r:
            return 0
        return self._factorials[n] * self._finvs[r] % self._mod * self._finvs[n - r] % self._mod


N, A, B, K = list(map(int, sys.stdin.readline().split()))
comb = Combination(max=N, mod=MOD)
# ans = 0

# for a in range(N + 1):

#         continue
#     b = int((K - a * A) // B)
#     if not (0 <= a <= N and 0 <= b <= N):
#         continue
#


#     c = min(a, b)
#     # A
#     a -= c
#     # B
#     b -= c

#     d = N - a - b - c

#     while c >= 0 and d >= 0:

#         a += 1
#         b += 1
#         c -= 1
#         d -= 1
#
#     ans += s
#     ans %= MOD
# print(ans)




#

# for a in range(N + 1):

#         continue
#     b = int((K - a * A) // B)
#     if not (0 <= a <= N and 0 <= b <= N):
#         continue
#     dp[0][a][b] += 1
#

#     dp[i] += dp[i - 1]
#     dp[i, :-1] += dp[i - 1, 1:]
#     dp[i, :, :-1] += dp[i - 1, :, 1:]
#     dp[i, :-1, :-1] += dp[i - 1, 1:, 1:]
# print(dp)
# print(dp[-1][0][0])



ans = 0
for a in range(N + 1):
    if (K - a * A) % B != 0:
        continue
    b = int((K - a * A) // B)
    if not (0 <= a <= N and 0 <= b <= N):
        continue
    ans += comb.ncr(N, a) * comb.ncr(N, b)
    ans %= MOD
print(ans)


Verdicts: Time Limit Exceeded

Accepted