<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 MOD 1000000007
#define INF 0x3f3f3f3f

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

void _print(ll e) {cout << e;}
void _print(int e) {cout << e;}
void _print(double e) {cout << e;}
void _print(unsigned int e) {cout << e;}
void _print(bool e) {cout << e;}
void _print(float e) {cout << e;}
void _print(const string& e) { cout << e; }

template<class T> void _print(const vector<T>& v) { cout << "[ "; for (const auto& e : v) { _print(e); cout << " "; } cout << "]"; }
template<class T> void _print(const set<T>& s) { cout << "{ "; for (const auto& e : s) { _print(e); cout << " "; } cout << "}"; }
template<class T> void _print(const unordered_set<T>& us) { cout << "{ "; for (const auto& e : us) { _print(e); cout << " "; } cout << "}"; }
template<class K, class V> void _print(const map<K, V>& m) { cout << "{ "; for (const auto& kv : m) { cout << "("; _print(kv.first); cout << ": "; _print(kv.second); cout << ") "; } cout << "}"; }
template<class K, class V> void _print(const unordered_map<K, V>& um) { cout << "{ "; for (const auto& kv : um) { cout << "("; _print(kv.first); cout << ": "; _print(kv.second); cout << ") "; } cout << "}"; }

#define DEBUG(x) std::cout << #x << " "; _print(x); cout << std::endl;


/*
Hint: get both min max product for each grid.
dp[i][j][0]: be the min product at grid[i][j].
dp[i][j][1]: be the max product at grid[i][j].
*/

class Solution {
public:
    int maxProductPath(vector<vector<int>>& grid) {
        int m = grid.size(), n = grid[0].size();
        vector<vector<vector<ll>>> dp(m, vector<vector<ll>>(n, vector<ll>(2, 0)));
        dp[0][0][0] = grid[0][0];
        dp[0][0][1] = grid[0][0];
        for (int i = 1; i < m; i++) {
            dp[i][0][0] = dp[i - 1][0][0] * grid[i][0];
            dp[i][0][1] = dp[i - 1][0][1] * grid[i][0];
        }
        for (int i = 1; i < n; i++) {
            dp[0][i][0] = dp[0][i - 1][0] * grid[0][i];
            dp[0][i][1] = dp[0][i - 1][1] * grid[0][i];
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                auto curr_val = grid[i][j];
                auto from_up_min = dp[i - 1][j][0] * curr_val;
                auto from_up_max = dp[i - 1][j][1] * curr_val;
                auto from_left_min = dp[i][j - 1][0] * curr_val;
                auto from_left_max = dp[i][j - 1][1] * curr_val;

                dp[i][j][0] = min(from_up_min, min(from_up_max, min(from_left_min, from_left_max)));
                dp[i][j][1] = max(from_up_min, max(from_up_max, max(from_left_min, from_left_max)));
            }
        }
        DEBUG(dp[m-1][n-1][1] % MOD);
        return max(-1, (int)(dp[m - 1][n - 1][1] % MOD));

    }
};
14000