<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 {
public:
    void updateMax(long long int currProduct, long long int &maxPositive, long long int &maxNegative){
        if(currProduct >= 0){
            if(currProduct > maxPositive){
                maxPositive = currProduct;
            }
        } else {
            if(currProduct < maxNegative){
                maxNegative = currProduct;
            }
        }
    }

    int maxProductPath(vector<vector<int>>& grid) {
        // recursion: goto (i, j) from (i-1, j) or (i, j-1)
        // DP[i][j] = maxProduct to (i, j)
        // DP[0][0] = grid[0][0]
        // DP[i][j] = maxABS(grid[i][j] * DP[i-1][j], grid[i][j] * DP[i][j-1])
        // iterate thru the grid row by row
        int numRows = grid.size();
        int numCols = grid[0].size();

        vector<vector<long long int>> positive(numRows, vector<long long int>(numCols, -1));
        vector<vector<long long int>> negative(numRows, vector<long long int>(numCols, 1));
        vector<vector<bool>> zero(numRows, vector<bool>(numCols, false));
        if(grid[0][0] > 0){
            positive[0][0] = grid[0][0];
        } else if(grid[0][0] < 0) {
            negative[0][0] = grid[0][0];
        } else {
            zero[0][0] = true;
        }
        vector<vector<int>> directions = {{-1,0}, {0, -1}};

        for(int row = 0; row < numRows; row++){
            for(int col = 0; col < numCols; col++){
                if(row == 0 && col == 0){
                    continue;
                }
                // There are four choices
                // positive from left, negative from left, positive up, negative up
                // need to pick the largest
                long long int maxPositive = -1;
                long long int maxNegative = 1;
                long long int currProduct;
                zero[row][col] = (grid[row][col] == 0);
                for(int dir = 0; dir < 2; dir++){
                    int prevRow = row + directions[dir][0];
                    int prevCol = col + directions[dir][1];
                    if(prevRow < 0 || prevCol < 0){
                        continue;
                    }
                    long long int prevPositive = positive[prevRow][prevCol];
                    if(prevPositive != -1){
                        currProduct = prevPositive * (long long int)grid[row][col];
                        updateMax(currProduct, maxPositive, maxNegative);
                    }
                    long long int prevNegative = negative[prevRow][prevCol];
                    if(prevNegative != 1){
                        currProduct = prevNegative * (long long int)grid[row][col];
                        updateMax(currProduct, maxPositive, maxNegative);
                    }
                    zero[row][col] = zero[row][col] || zero[prevRow][prevCol];
                }
                positive[row][col] = maxPositive;
                negative[row][col] = maxNegative;
                
                // std::cout << "row: " << to_string(row) << ", col: " << to_string(col) << 
                //     ", maxPos: " << to_string(maxPositive) << 
                //     ", maxNeg: " << to_string(maxNegative) <<
                //     ", zero: " << to_string(zero[row][col]) <<  std::endl;
            }
        }
        if(positive[numRows - 1][numCols - 1] == -1){
            if(zero[numRows - 1][numCols - 1]){
                return 0;
            } else {
                return -1;
            }
        }
        return (int)(positive[numRows - 1][numCols - 1] % (7 + 1000000000));
    }
};
13600