There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. 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 at most maxMove moves to the ball.

Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 109 + 7.

&nbsp;
Example 1:

Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
Output: 6


Example 2:

Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
Output: 12


&nbsp;
Constraints:


	1 &lt;= m, n &lt;= 50
	0 &lt;= maxMove &lt;= 50
	0 &lt;= startRow &lt; m
	0 &lt;= startColumn &lt; n


class Solution:
    def helper(self, m, n, maxMove, startRow, startColumn, mat,dp) -> int:
        if startRow < 0 or startRow >=m or startColumn < 0 or startColumn >=n:
            return 1
        
        if dp[maxMove][startRow][startColumn]!=-1:
            return dp[maxMove][startRow][startColumn]
        
        if mat[startRow][startColumn]==1:
            return 0
        
        if maxMove <= 0:
            return 0
        
        # mat[startRow][startColumn] = 1
        a = self.helper(m, n, maxMove-1, startRow+1, startColumn,mat,dp)
        b = self.helper(m, n, maxMove-1, startRow-1, startColumn,mat,dp)
        c = self.helper(m, n, maxMove-1, startRow, startColumn+1,mat,dp)
        d = self.helper(m, n, maxMove-1, startRow, startColumn-1,mat,dp)
        dp[maxMove][startRow][startColumn] = a+b+c+d
        return dp[maxMove][startRow][startColumn]
        
        
    def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int:
        mat = [[0 for i in range(n)] for j in range(m)]
        dp = [[[-1 for i in range(n+1)] for j in range(m+1)] for k in range(maxMove+1)]
        return self.helper(m, n, maxMove, startRow, startColumn, mat,dp)%(10**9  + 7) 
    

            
class Solution {
    int[][][] dp;
    int mod = 1000000007;
    public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
        dp = new int[m][n][maxMove + 1];
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                for (int k = 0; k <= maxMove; k++)
                    dp[i][j][k] = -1;
        return count(m, n, maxMove, startRow, startColumn) % mod;
    }
    public int count(int m, int n, int move, int r, int c) {
        if (r < 0 || c < 0 || r >= m || c >= n)
            return 1;
        if (move <= 0)
            return 0;
        if (dp[r][c][move] != -1)
            return dp[r][c][move] % mod;
        dp[r][c][move] = ((count(m, n, move - 1, r + 1, c) % mod + count (m, n, move - 1, r - 1, c) % mod) % mod + (count (m, n, move - 1, r, c + 1) % mod + count(m, n, move - 1, r, c - 1) % mod) % mod ) % mod;
        return dp[r][c][move] % mod;
    }
}
vector<vector<vector<int>>> dp;

int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int mod = 1e9+7;

int fun(int i,int j,int n,int m,int k){
    
    if(i < 0 || j < 0 || i == n || j == m)return 1;
    else if(k == 0)return 0;
    
    if(dp[i][j][k] != -1)return dp[i][j][k];
    
    int ans = 0;
    for(int c = 0; c < 4; c++){
        int ni = i+dx[c] , nj = j+dy[c];
        ans = (ans + fun(ni,nj,n,m,k-1)) % mod;
    }
    
    return dp[i][j][k] = ans;
}

int findPaths(int m, int n, int maxMove, int startRow, int startCol) {
    
    dp = vector<vector<vector<int>>>(m, vector<vector<int>>(n, vector<int>(maxMove+1, -1)));
	
    return fun(startRow, startCol,m,n,maxMove);
}
vector<vector<vector<int>>> dp;

int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

int mod = 1e9+7;

int fun(int i,int j,int n,int m,int k){
    
    if(i < 0 || j < 0 || i == n || j == m)return 1;
    else if(k == 0)return 0;
    
    if(dp[i][j][k] != -1)return dp[i][j][k];
    
    int ans = 0;
    for(int c = 0; c < 4; c++){
        int ni = i+dx[c] , nj = j+dy[c];
        ans = (ans + fun(ni,nj,n,m,k-1)) % mod;
    }
    
    return dp[i][j][k] = ans;
}

int findPaths(int m, int n, int maxMove, int startRow, int startCol) {
    
    dp = vector<vector<vector<int>>>(m, vector<vector<int>>(n, vector<int>(maxMove+1, -1)));
	
    return fun(startRow, startCol,m,n,maxMove);
}
Out of Boundary Paths