Problem:
Score : 600 points

Problem Statement

We have a grid with A horizontal rows and B vertical columns, with the squares painted white. On this grid, we will repeatedly apply the following operation:

Assume that the grid currently has a horizontal rows and b vertical columns. Choose "vertical" or "horizontal".

If we choose "vertical", insert one row at the top of the grid, resulting in an (a+1) \times b grid.

If we choose "horizontal", insert one column at the right end of the grid, resulting in an a \times (b+1) grid.

Then, paint one of the added squares black, and the other squares white.

Assume the grid eventually has C horizontal rows and D vertical columns. Find the number of ways in which the squares can be painted in the end, modulo 998244353.

Constraints

1 \leq A \leq C \leq 3000

1 \leq B \leq D \leq 3000

A, B, C, and D are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of ways in which the squares can be painted in the end, modulo 998244353.

Sample Input 1

1 1 2 2

Sample Output 1

3

Any two of the three squares other than the bottom-left square can be painted black.

Sample Input 2

2 1 3 4

Sample Output 2

65

Sample Input 3

31 41 59 265

Sample Output 3

387222020

Solution in Rust (1.42.0) (judge verdict: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use competitive::prelude::*;

type GF = competitive::gf::GF<promote!(998244353)>;

fn main() {
    input! {
        a: usize,
        b: usize,
        c: usize,
        d: usize,
    }

    // dbg!(naive(2, 1, 2, 3));
    // dbg!(naive(2, 1, 2, 4));
    // dbg!(naive(2, 1, 3, 3));
    // dbg!(naive(2, 1, 3, 4));

    let ans = solve(a, b, c, d);
    echo!(ans);
}

fn print_bd(bd: &Vec<Vec<i32>>) {
    for row in bd.iter() {
        for cell in row.iter() {
            eprint!("{}", cell);
        }
        eprintln!();
    }
    eprintln!();
}

fn naive(a: usize, b: usize, c: usize, d: usize) -> usize {
    let bd = vec![vec![0; b]; a];

    let mut mm = BTreeSet::new();
    mm.insert(bd.clone());

    let mut q = VecDeque::new();
    q.push_back(bd);

    let mut ans = BTreeSet::new();

    while let Some(bd) = q.pop_front() {
        if bd.len() == c && bd[0].len() == d {
            ans.insert(bd);
            continue;
        }

        // print_bd(&bd);

        if c > bd.len() {
            let y = bd.len() - 1;
            for x in 0..bd[0].len() {
                let mut nbd = bd.clone();
                let mut v = vec![0; bd[0].len()];
                v[x] = 1;
                nbd.insert(y + 1, v);

                // print_bd(&nbd);

                if !mm.contains(&nbd) {
                    mm.insert(nbd.clone());
                    q.push_back(nbd);
                }
            }
        }

        if d > bd[0].len() {
            let x = bd[0].len() - 1;
            for y in 0..bd.len() {
                let mut nbd = bd.clone();

                for yy in 0..bd.len() {
                    nbd[yy].insert(x + 1, if y == yy { 1 } else { 0 });
                }

                // print_bd(&nbd);

                if !mm.contains(&nbd) {
                    mm.insert(nbd.clone());
                    q.push_back(nbd);
                }
            }
        }
    }

    ans.len()
}

#[memoise(c, d)]
fn solve(a: usize, b: usize, c: usize, d: usize) -> GF {
    if (a, b) == (c, d) {
        return 1.into();
    }

    if c > a && b == d {
        return solve(a, b, c - 1, d) * d;
    }

    if d > b && a == c {
        return solve(a, b, c, d - 1) * c;
    }

    solve(a, b, c - 1, d) * d + solve(a, b, c, d - 1) * c
        - solve(a, b, c - 1, d - 1) * (c - 1) * (d - 1)
}
*/

fn main() {
    let exe = "/tmp/bin5C652467";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";