<p>There is an <code>m x n</code> grid with a ball. The ball is initially at the position <code>[startRow, startColumn]</code>. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply <strong>at most</strong> <code>maxMove</code> moves to the ball.</p>

<p>Given the five integers <code>m</code>, <code>n</code>, <code>maxMove</code>, <code>startRow</code>, <code>startColumn</code>, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png" style="width: 500px; height: 296px;" />
<pre>
<strong>Input:</strong> m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
<strong>Output:</strong> 6
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png" style="width: 500px; height: 293px;" />
<pre>
<strong>Input:</strong> m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
<strong>Output:</strong> 12
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= m, n &lt;= 50</code></li>
	<li><code>0 &lt;= maxMove &lt;= 50</code></li>
	<li><code>0 &lt;= startRow &lt; m</code></li>
	<li><code>0 &lt;= startColumn &lt; n</code></li>
</ul>

#include <bits/stdc++.h>
using namespace std;
#define siddhantnema ios_base::sync_with_stdio(0); cin.tie(0);
#define ll long long
#define inparr(arr, N) for (ll i = 0; i < N; i++) { cin >> arr[i]; }
#define forn(_i, N) for (ll _i = 0; _i < N; _i++)
#define trc(x) cout << #x << " -> " << x << '\n';
#define len(x) (ll)x.size()
const ll RMOD = 1e9 + 7;

int DP[69][69][69];
int addm(int x, int y, ll RMOD)
{
    ll x1 = x, y1 = y;
    return (int)((x1 + y1) % RMOD);
}

class Solution
{
public:
    int rec(int m, int n, int x, int y, int moves)
    {
        if ((x >= m or y >= n) or (x < 0 or y < 0))
            return 1;

        if (DP[x][y][moves] != -1)
            return DP[x][y][moves];

        if (moves == 0)
            return DP[x][y][moves] = 0;

        int res = 0;
        auto dir = {-1, 1, 0};
        for (int i : dir)
            for (int j : dir)
                if ((abs(i) + abs(j)) == 1)
                    res = addm(res, rec(m, n, x + i, y + j, moves - 1), RMOD);

        return DP[x][y][moves] = res;
    }
    int findPaths(int m, int n, int maxMove, int startRow, int startColumn)
    {
        forn(i, m + 1)
            forn(j, n + 1)
                forn(k, maxMove + 1)
                    DP[i][j][k] = -1;

        return rec(m, n, startRow, startColumn, maxMove);
    }
};
9538