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 C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include "bits/stdc++.h"

using namespace std;

const long long MOD = 998244353;

void Main() {
    long long A, B, C, D;
    cin >> A >> B >> C >> D;

    vector<long long> nrow(C - A + 1, 0LL);
    vector<long long> ncol(D - B + 1, 0LL);
    for (long long i = 0; i < nrow.size(); ++i) {
        nrow[i] = A + i;
    }
    for (long long j = 0; j < ncol.size(); ++j) {
        ncol[j] = B + j;
    }

    vector<long long> temp(3, 0LL);
    vector<vector<long long> > dp(nrow.size(), vector<long long>(ncol.size(), -1LL));
    for (long long i = 0; i < nrow.size(); ++i) {
        for (long long j = 0; j < ncol.size(); ++j) {
            long long add = 0LL;
            if (i == 0) {
                if (j == 0) {
                    add = 0LL;
                }
                else {
                    if (nrow[i] > 1) {
                        add = nrow[i] * ncol[j - 1];
                    }
                    add += dp[i][j - 1];
                }
            }
            else {
                long long fromim1 = nrow[i - 1] * ncol[j];
                if (j == 0) {
                    if (ncol[j] > 1) {
                        add = fromim1;
                    }
                    add += dp[i - 1][j];
                }
                else {
                    long long fromjm1 = nrow[i] * ncol[j - 1];
                    long long fromdiag = nrow[i - 1] * ncol[j - 1] * (nrow[i - 1] * nrow[j - 1] + nrow[i - 1] + ncol[j - 1]);
                    temp[0] = fromim1;
                    temp[1] = fromjm1;
                    temp[2] = fromdiag;
                    auto maxIter = max_element(temp.begin(), temp.end());
                    add = *maxIter;
                    if (0 == distance(temp.begin(), maxIter)) {
                        add += dp[i - 1][j];
                    }
                    else if (1 == distance(temp.begin(), maxIter)) {
                        add += dp[i][j - 1];
                    }
                    else {
                        add += dp[i - 1][j - 1];
                    }
                }
            }
            dp[i][j] = add;
            dp[i][j] %= MOD;
        }
    }

    long long ans = dp.back().back();
    cout << ans << endl;
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}