You are given an m x n integer matrix grid, where you can move from a cell to any adjacent cell in all 4 directions.

Return the number of strictly increasing paths in the grid such that you can start from any cell and end at any cell. Since the answer may be very large, return it modulo 109 + 7.

Two paths are considered different if they do not have exactly the same sequence of visited cells.

&nbsp;
Example 1:

Input: grid = [[1,1],[3,4]]
Output: 8
Explanation: The strictly increasing paths are:
- Paths with length 1: [1], [1], [3], [4].
- Paths with length 2: [1 -&gt; 3], [1 -&gt; 4], [3 -&gt; 4].
- Paths with length 3: [1 -&gt; 3 -&gt; 4].
The total number of paths is 4 + 3 + 1 = 8.


Example 2:

Input: grid = [[1],[2]]
Output: 3
Explanation: The strictly increasing paths are:
- Paths with length 1: [1], [2].
- Paths with length 2: [1 -&gt; 2].
The total number of paths is 2 + 1 = 3.


&nbsp;
Constraints:


	m == grid.length
	n == grid[i].length
	1 &lt;= m, n &lt;= 1000
	1 &lt;= m * n &lt;= 105
	1 &lt;= grid[i][j] &lt;= 105


class Solution:
    def __init__(self):
        self.dp = None
        self.di = [0, 0, -1, 1]
        self.dj = [-1, 1, 0, 0]
        self.mod = 1000000007
    
    def countPaths(self, grid):
        n = len(grid)
        m = len(grid[0])
        self.dp = [[0] * m for _ in range(n)]
        ans = 0
        for i in range(n):
            for j in range(m):
                ans = (ans + self.dfs(grid, i, j, -1)) % self.mod
        return ans
    
    def dfs(self, grid, i, j, prev):
        if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] <= prev:
            return 0
        if self.dp[i][j] != 0:
            return self.dp[i][j]
        self.dp[i][j] = 1
        for k in range(4):
            self.dp[i][j] += self.dfs(grid, i + self.di[k], j + self.dj[k], grid[i][j])
            self.dp[i][j] %= self.mod
        return self.dp[i][j] % self.mod
class Solution {
    long[][] dp;
    int mod = 1_000_000_007;
    public int countPaths(int[][] grid) {
        dp = new long[grid.length][grid[0].length];
        long sum=0L;
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                sum = (sum + dfs(grid,i,j,0)) % mod;
            }
        }
        return (int)sum;
    }
    long dfs(int[][] grid,int i,int j,int pre){
        if(i<0||j<0||i>=grid.length||j>=grid[0].length) return 0;
        if(grid[i][j]<=pre) return 0;
        if(dp[i][j]>0) return dp[i][j];
        long a = dfs(grid,i+1,j,grid[i][j]);
        long b = dfs(grid,i,j+1,grid[i][j]);
        long c = dfs(grid,i-1,j,grid[i][j]);
        long d = dfs(grid,i,j-1,grid[i][j]);
        dp[i][j] = (1+a+b+c+d)%mod;
        return dp[i][j];
    }
}
class Solution {
public:
    int mod = 1000000007;
    int dx[4] = {1,0,-1,0};
    int dy[4] = {0,1,0,-1};
    int countPaths(vector<vector<int>>& grid) {
        vector <vector <int>> dp(grid.size(),vector<int>(grid[0].size(),-1));
        long long count = 0;
        for(int i = 0; i<grid.size(); i++){
            for(int j = 0; j<grid[0].size(); j++){
                count = (count%mod + dfs(i,j,grid,dp)%mod)%mod;
            }
        }
        return (int)count%mod;
    }
protected:
    bool isvalid(int x, int y, vector<vector<int>>&grid){
        if(x<0 or x>=grid.size() or y<0 or y>=grid[0].size()) return false;
        return true;
    }
    int dfs(int x, int y, vector <vector<int>>&grid,vector <vector<int>>&dp){
        if(dp[x][y]!=-1) return dp[x][y];

        int ans = 1;
        for(int i = 0; i<4; i++){
            if(isvalid(x+dx[i],y+dy[i],grid) and grid[x][y]>grid[x+dx[i]][y+dy[i]]){
                ans = (ans%mod+dfs(x+dx[i],y+dy[i],grid,dp)%mod)%mod;
            }
        }
        return dp[x][y] = ans%mod;
    }
};
var countPaths = function(grid) {
  let mod = Math.pow(10, 9) + 7;
  let result = 0;
  let rows = grid.length, columns = grid[0].length;
  let dp = Array(rows).fill(null).map(_ => Array(columns).fill(0));

  const dfs = (r, c, preVal)=> {
    if (r < 0 || r == rows || c < 0 || c == columns || grid[r][c] <= preVal) return 0
    if (dp[r][c]) return dp[r][c]
    return dp[r][c] = (1 + dfs(r + 1, c, grid[r][c]) +
                       dfs(r - 1, c, grid[r][c]) +
                       dfs(r , c + 1, grid[r][c]) +
                       dfs(r , c - 1, grid[r][c])) % mod;
  }
   for(let i = 0; i < rows; i++) {
    for(let j = 0; j < columns; j++) {
      result += dfs(i, j, -1) % mod;
    }
  }

  return result % mod;
};
Number of Increasing Paths in a Grid