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

<p>Return <em>the number of <strong>strictly</strong> <strong>increasing</strong> paths in the grid such that you can start from <strong>any</strong> cell and end at <strong>any</strong> cell. </em>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/10/griddrawio-4.png" style="width: 181px; height: 121px;" />
<pre>
<strong>Input:</strong> grid = [[1,1],[3,4]]
<strong>Output:</strong> 8
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1],[2]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 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.
</pre>

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

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 1000</code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def countPaths(self, grid: List[List[int]]) -> int:
        rows, cols = len(grid), len(grid[0])
        memo = {}
        def dfs(i, j):
            if (i, j) in memo:
                return memo[(i, j)]
            res = 1
            for x, y in [(i-1,j), (i+1, j), (i, j-1), (i, j+1)]:
                if 0 <= x < rows and 0 <= y < cols and grid[x][y] > grid[i][j]:
                    res += dfs(x, y)
            memo[(i, j)] = res
            return res
        ans = 0
        for i in range(rows):
            for j in range(cols):
                ans += dfs(i, j)
        return ans % (10**9+7)


        # Wrong
        # dp = [[1] * cols for _ in range(rows)]
        # # dp[rows-1][cols-1] = 1
        # MOD = 10**9+7
        # ans = 1
        # for i in range(rows-1, -1, -1):
        #     for j in range(cols-1, -1, -1):
        #         if i == rows-1 and j == cols-1:
        #             continue
        #         elif i == rows-1:
        #             if grid[i][j] < grid[i][j+1]:
        #                 dp[i][j] += dp[i][j+1]
        #             # dp[i][j] = 1 if grid[i][j] >= grid[i][j+1] else 1 + dp[i][j+1]
        #         elif j == cols-1:
        #             if grid[i][j] < grid[i+1][j]:
        #                 dp[i][j] += dp[i+1][j]
        #             # dp[i][j] = 1 if grid[i][j] >= grid[i+1][j] else 1 + dp[i+1][j]
        #         else:
        #             right = 0 if grid[i][j] >= grid[i][j+1] else dp[i][j+1]
        #             down = 0 if grid[i][j] >= grid[i+1][j] else dp[i+1][j]
        #             dp[i][j] += right + down
        #             # if right == 0 and down == 1:
        #             #     dp[i][j] = 1
        #             # elif right == 1 or down == 1:
        #             #     dp[i][j] = right if down == 1 else down
        #             # else:
        #             #     dp[i][j] = right + down - 1
        #             # dp[i][j] = 1 if right == 1 and down == 1 else 1 + right + down
        #         ans += dp[i][j] % MOD
        # print(dp)
        # dp2 = [[1] * cols for _ in range(rows)]
        # # dp2[0][0] = 1
        # ans += 1
        # for i in range(rows):
        #     for j in range(cols):
        #         if i == 0 and j == 0:
        #             continue
        #         elif i == 0:
        #             if grid[i][j-1] > grid[i][j]:
        #                 dp2[i][j] += dp2[i][j-1]
        #             # dp2[i][j] = 1 if grid[i][j] >= grid[i][j-1] else 1 + dp2[i][j-1]
        #         elif j == 0:
        #             if grid[i-1][j] > grid[i][j]:
        #                 dp2[i][j] += dp2[i-1][j]
        #             # dp2[i][j] = 1 if grid[i][j] >= grid[i-1][j] else 1 + dp2[i-1][j]
        #         else:
        #             left = 0 if grid[i][j] >= grid[i][j-1] else dp2[i][j-1]
        #             up = 0 if grid[i][j] >= grid[i-1][j] else dp2[i-1][j]
        #             dp2[i][j] += left + up
        #             # dp2[i][j] = 0 if left == 0 and up == 0 else left + up
        #             # if left == 1 or up == 1:
        #             #     dp2[i][j] = left if up == 1 else up
        #             # else:
        #             #     dp2[i][j] = left + up - 1
        #         ans += dp2[i][j] % MOD
        # print(dp2)
        # return ans - rows*cols

106609