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

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

use io::*;
use modular::*;
use std::cmp::{max, min};

fn main() {
    let con = read_string();
    let mut sc = Scanner::new(&con);

    let n: i64 = sc.next();
    let a: i64 = sc.next();
    let b: i64 = sc.next();
    let k: i64 = sc.next();

    let mut ans = Modular::new(0);

    let mut facts = vec![Modular::new(1); n as usize + 1];
    let mut ifacts = vec![Modular::new(1); n as usize + 1];

    for i in 1..n as usize + 1 {
        facts[i] = facts[i - 1] * Modular::new(i as _);
        ifacts[i] = facts[i].recip();
    }

    for red in 0..n {
        if !(k - a * red >= 0 && (k - a * red) % b == 0) {
            continue;
        }

        let blue = (k - a * red) / b;

        let x = min(red, blue);
        let y = max(red, blue);

        // println!("{}, {}", x, y);

        for i in 0..min(x, n - y) + 1 {
            // let nr = i;
            // let ng = x - i;
            // let nb = y - x + i;
            // ans += comb(n, nr) * comb(n - nr, ng) * comb(n - nr - ng, nb);

            // ans += comb(n, i) * comb(n - i, x - i) * comb(n - x, y - x + i);

            // ans += fact(n) / fact(i) / fact(n - i) * fact(n - i) / fact(x - i) / fact(n - x)
            //     * fact(n - x) / fact(y - x + i) / fact(n - y - i);

            // ans += fact(n) / fact(i) / fact(x - i) / fact(y - x + i) / fact(n - y - i);

            ans += facts[n as usize] * ifacts[i as usize] * ifacts[(x - i) as usize]
                * ifacts[(y - x + i) as usize] * ifacts[(n - y - i) as usize];
        }
    }

    println!("{}", ans);
}

// fn comb(a: i64, b: i64) -> Modular {
//     if b == 0 {
//         1.into()
//     } else {
//         comb(a - 1, b - 1) * a / Modular::new(b)
//     }
// }

fn comb(n: i64, k: i64) -> Modular {
    fact(n) / fact(k) / fact(n - k)
}

fn fact(n: i64) -> Modular {
    let mut ret = 1.into();
    for i in 1..n + 1 {
        ret *= Modular::new(i)
    }
    ret
}

//-----

pub mod io {
    use std;
    use std::str::FromStr;

    pub struct Scanner<'a> {
        iter: std::str::SplitWhitespace<'a>,
    }

    impl<'a> Scanner<'a> {
        pub fn new(s: &'a str) -> Scanner<'a> {
            Scanner {
                iter: s.split_whitespace(),
            }
        }

        pub fn next<T: FromStr>(&mut self) -> T {
            let s = self.iter.next().unwrap();
            if let Ok(v) = s.parse::<T>() {
                v
            } else {
                panic!("Parse error")
            }
        }

        pub fn next_vec_len<T: FromStr>(&mut self) -> Vec<T> {
            let n: usize = self.next();
            self.next_vec(n)
        }

        pub fn next_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.next()).collect()
        }
    }

    pub fn read_string() -> String {
        use std::io::Read;

        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    }

    pub fn read_line() -> String {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).unwrap();
        s.trim_right().to_owned()
    }
}

pub mod modular {
    const M: i64 = 998244353;

    #[derive(Debug, Clone, Copy, Default, PartialOrd, Ord, PartialEq, Eq)]
    pub struct Modular(i64);

    impl ::std::fmt::Display for Modular {
        fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl Modular {
        pub fn new(v: i64) -> Modular {
            Modular(v % M)
        }

        pub fn pow(self, mut r: i64) -> Modular {
            let mut k = self;
            let mut ret = 1.into();

            while r > 0 {
                if r % 2 != 0 {
                    ret = ret * k;
                }
                r /= 2;
                k = k * k;
            }

            ret
        }

        // This requires M is prime
        pub fn recip(self) -> Modular {
            self.pow(M - 2)
        }
    }

    use std::ops::*;

    impl<T: Into<Modular>> Add<T> for Modular {
        type Output = Modular;
        fn add(self, rhs: T) -> Self::Output {
            Modular::new(self.0 + rhs.into().0)
        }
    }
    impl<T: Into<Modular>> AddAssign<T> for Modular {
        fn add_assign(&mut self, rhs: T) {
            *self = *self + rhs;
        }
    }

    impl<T: Into<Modular>> Sub<T> for Modular {
        type Output = Modular;
        fn sub(self, rhs: T) -> Self::Output {
            Modular::new(self.0 - rhs.into().0 + M)
        }
    }
    impl<T: Into<Modular>> SubAssign<T> for Modular {
        fn sub_assign(&mut self, rhs: T) {
            *self = *self - rhs;
        }
    }

    impl<T: Into<Modular>> Mul<T> for Modular {
        type Output = Modular;
        fn mul(self, rhs: T) -> Self::Output {
            Modular::new(self.0 * rhs.into().0)
        }
    }
    impl<T: Into<Modular>> MulAssign<T> for Modular {
        fn mul_assign(&mut self, rhs: T) {
            *self = *self * rhs;
        }
    }

    impl<T: Into<Modular>> Div<T> for Modular {
        type Output = Modular;
        fn div(self, rhs: T) -> Self::Output {
            self * rhs.into().recip()
        }
    }
    impl<T: Into<Modular>> DivAssign<T> for Modular {
        fn div_assign(&mut self, rhs: T) {
            *self = *self / rhs;
        }
    }

    impl Neg for Modular {
        type Output = Modular;
        fn neg(self) -> Self::Output {
            Modular(0) - self
        }
    }

    impl<T: ::std::convert::Into<i64>> ::std::convert::From<T> for Modular {
        fn from(v: T) -> Self {
            Modular::new(v.into())
        }
    }
}