<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>

#define LL long long
#define PQ priority_queue
#define VI vector<int>
#define VL vector<long>
#define VUL vector<unsigned long>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VVL vector<vector<long>>
#define VVUL vector<vector<unsigned long>>
#define VVB vector<vector<bool>>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VS vector<string>
#define VSEI vector<set<int>>
#define VVS vector<vector<string>>
#define PII pair<int, int>
#define PLL pair<long, long>
#define PLI pair<long, int>
#define VPII vector<pair<int, int>>
#define VPLL vector<pair<long, long>>
#define VVPII vector<vector<pair<int, int>>>
#define VVPLL vector<vector<pair<long, long>>>
#define VVPLI vector<vector<pair<long, int>>>
#define UMII unordered_map<int, int>
#define UMSS unordered_map<string, string>
#define UMCS unordered_map<char, string>
#define UM unordered_map
#define USI unordered_set<int>
#define USS unordered_set<string>
#define QPII queue<pair<int, int>>

#define DSZ(g, M, N) const int M = g.size(), N = g[0].size()
#define D4(dx, dy)             \
    int dx[4] = {-1, 1, 0, 0}; \
    int dy[4] = {0, 0, -1, 1}
#define D8(dx, dy)                           \
    int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; \
    int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1}
#define OB(x, y, M, N) ((x) >= (M) || (y) >= (N) || (x) < 0 || (y) < 0)
using AI3 = std::array<int, 3>;
using VAI3 = std::vector<std::array<int, 3>>;
using AI4 = std::array<int, 4>;
using VAI4 = std::vector<std::array<int, 4>>;

#define G_DSZ(g, M, N) const int M = g.size(), N = g[0].size()
#define G_D4D(d) vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}
#define G_D8D(d) vector<pair<int, int>> d = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}
#define G_SZ(g, M, N) M = g.size(), N = g[0].size()
#define G_OB(x, y, M, N) ((x) >= (M) || (y) >= (N) || (x) < 0 || (y) < 0)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)

class Solution {
public:
    int maxProductPath(vector<vector<int>>& g) {
        DSZ(g, m, n);
        VVL dpmax(m, VL(n, INT_MIN));
        VVL dpmin(m, VL(n, INT_MAX));
        dpmax[0][0] = g[0][0];
        dpmin[0][0] = g[0][0];
        FOR(j, 1, n - 1) {
            dpmax[0][j] = dpmax[0][j - 1] * g[0][j];
            dpmin[0][j] = dpmax[0][j];
        }
        FOR(i, 1, m - 1) {
            dpmax[i][0] = dpmax[i - 1][0] * g[i][0];
            dpmin[i][0] = dpmax[i][0];
        }

        FOR(i, 1, m - 1) {
            FOR(j, 1, n - 1) {
                int cur = g[i][j];
                if (cur == 0) {
                    dpmax[i][j] = 0;
                    dpmin[i][j] = 0;
                } else if (cur > 0) {
                    dpmax[i][j] = max(dpmax[i - 1][j], dpmax[i][j - 1]) * cur;
                    dpmin[i][j] = min(dpmin[i - 1][j], dpmin[i][j - 1]) * cur;
                } else {
                    // cur < 0
                    dpmin[i][j] = max(dpmax[i - 1][j], dpmax[i][j - 1]) * cur;
                    dpmax[i][j] = min(dpmin[i - 1][j], dpmin[i][j - 1]) * cur;
                }
            }
        }
        const int MOD = 1e9 + 7;
        return dpmax[m - 1][n - 1] >= 0 ? dpmax[m - 1][n - 1] % MOD : -1;
    }
};

13300