<p>You are given a <code>m x n</code> matrix <code>grid</code>. Initially, you are located at the top-left corner <code>(0, 0)</code>, and in each step, you can only <strong>move right or down</strong> in the matrix.</p>

<p>Among all possible paths starting from the top-left corner <code>(0, 0)</code> and ending in the bottom-right corner <code>(m - 1, n - 1)</code>, find the path with the <strong>maximum non-negative product</strong>. The product of a path is the product of all integers in the grid cells visited along the path.</p>

<p>Return the <em>maximum non-negative product <strong>modulo</strong> </em><code>10<sup>9</sup> + 7</code>. <em>If the maximum product is <strong>negative</strong>, return </em><code>-1</code>.</p>

<p>Notice that the modulo is performed after getting the maximum product.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/product1.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>Input:</strong> grid = [[-1,-2,-3],[-2,-3,-3],[-3,-3,-2]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> It is not possible to get non-negative product in the path from (0, 0) to (2, 2), so return -1.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/product2.jpg" style="width: 244px; height: 245px;" />
<pre>
<strong>Input:</strong> grid = [[1,-2,1],[1,-2,1],[3,-4,1]]
<strong>Output:</strong> 8
<strong>Explanation:</strong> Maximum non-negative product is shown (1 * 1 * -2 * -4 * 1 = 8).
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/product3.jpg" style="width: 164px; height: 165px;" />
<pre>
<strong>Input:</strong> grid = [[1,3],[0,-4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Maximum non-negative product is shown (1 * 0 * -4 = 0).
</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;= 15</code></li>
	<li><code>-4 &lt;= grid[i][j] &lt;= 4</code></li>
</ul>

class Solution:
    def maxProductPath(self, grid: List[List[int]]) -> int:
        mod = 1000000007
        m = len(grid)
        n = len(grid[0])
        res = []
        for i in range(m):
            t = []
            for j in range(n):
                t.append([0, 0, 0, 0])
            res.append(t)

        if grid[0][0] < 0:
            res[0][0][0] = 1
            res[0][0][1] = grid[0][0]
        else:
            res[0][0][3] = 1
            res[0][0][2] = grid[0][0]

        for i in range(1, m):
            if grid[i][0] < 0:
                res[i][0][0] = 1 if res[i-1][0][3] == 1 else 0
                res[i][0][1] = (res[i-1][0][2] * grid[i][0])
                res[i][0][2] = (res[i-1][0][1] * grid[i][0])
                res[i][0][3] = 1 if res[i-1][0][0] == 1 else 0
            else:
                res[i][0][0] = 1 if res[i-1][0][0] == 1 else 0
                res[i][0][1] = (res[i-1][0][1] * grid[i][0])
                res[i][0][2] = (res[i-1][0][2] * grid[i][0])
                res[i][0][3] = 1 if res[i-1][0][3] == 1 else 0

        for i in range(1, n):
            if grid[0][i] < 0:
                res[0][i][0] = 1 if res[0][i-1][3] == 1 else 0
                res[0][i][1] = (res[0][i-1][2] * grid[0][i])
                res[0][i][2] = (res[0][i-1][1] * grid[0][i])
                res[0][i][3] = 1 if res[0][i-1][0] == 1 else 0
            else:
                res[0][i][0] = 1 if res[0][i-1][0] == 1 else 0
                res[0][i][1] = (res[0][i-1][1] * grid[0][i])
                res[0][i][2] = (res[0][i-1][2] * grid[0][i])
                res[0][i][3] = 1 if res[0][i-1][3] == 1 else 0

        for i in range(1, m):
            for j in range(1, n):
                if grid[i][j] < 0:
                    res[i][j][0] = 1 if res[i-1][j][3] == 1 or res[i][j-1][3] == 1 else 0
                    res[i][j][1] = max(res[i-1][j][2], res[i][j-1][2]) * grid[i][j]
                    res[i][j][2] = min(res[i-1][j][1], res[i][j-1][1]) * grid[i][j]
                    res[i][j][3] = 1 if res[i-1][j][0] == 1 or res[i][j-1][0] == 1 else 0
                else:
                    res[i][j][0] = 1 if res[i-1][j][0] == 1 or res[i][j-1][0] == 1 else 0
                    res[i][j][1] = min(res[i-1][j][1], res[i][j-1][1]) * grid[i][j]
                    res[i][j][2] = max(res[i-1][j][2], res[i][j-1][2]) * grid[i][j]
                    res[i][j][3] = 1 if res[i-1][j][3] == 1 or res[i][j-1][3] == 1 else 0

        zero_encountered = 0
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 0:
                    zero_encountered = 1

        if res[m-1][n-1][3] == 0:
            if zero_encountered == 1:
                return 0
            else:
                return -1


        return res[m-1][n-1][2] % mod
16900