<p>Given the <code>root</code> of a binary tree, split the binary tree into two subtrees by removing one edge such that the product of the sums of the subtrees is maximized.</p>

<p>Return <em>the maximum product of the sums of the two subtrees</em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p><strong>Note</strong> that you need to maximize the answer before taking the mod and not after taking it.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/sample_1_1699.png" style="width: 500px; height: 167px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6]
<strong>Output:</strong> 110
<strong>Explanation:</strong> Remove the red edge and get 2 binary trees with sum 11 and 10. Their product is 110 (11*10)
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/01/21/sample_2_1699.png" style="width: 500px; height: 211px;" />
<pre>
<strong>Input:</strong> root = [1,null,2,3,4,null,null,5,6]
<strong>Output:</strong> 90
<strong>Explanation:</strong> Remove the red edge and get 2 binary trees with sum 15 and 6.Their product is 90 (15*6)
</pre>

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

<ul>
	<li>The number of nodes in the tree is in the range <code>[2, 5 * 10<sup>4</sup>]</code>.</li>
	<li><code>1 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
</ul>

class newTree {
public:
    long long val;
    newTree* left;
    newTree* right;
    newTree(long long value) {
        val = value;
        left = NULL, right = NULL;
    }
};

class Solution {
private:
    int MOD = 1e9 + 7;
    newTree* generateSum(TreeNode* root) {
        if(root == NULL) {
            return NULL;
        }
        newTree* left = generateSum(root->left);
        newTree* right = generateSum(root->right);
        long long temp = left? left->val: 0; temp += right? right->val: 0; temp += root->val;
        newTree* newnode = new newTree(temp);
        newnode->left = left, newnode->right = right;
        return newnode;
    }
    long long support(newTree* root, long long& sum, newTree* &head) {
        if(root == NULL) {
            return 0;
        }
        if(root->left == NULL && root->right == NULL) {
            long long temp = sum-root->val;
            temp = temp*root->val;
            return root->val = temp;
        }
        long long left = support(root->left, sum, head);
        long long right = support(root->right, sum, head);
        if(head == root) {
            return root->val = max(left, right);
        }
        long long temp = sum-root->val;
        temp = temp*root->val;
        return root->val = (max({temp, left, right}));
    }
public:
    int maxProduct(TreeNode* root) {
        newTree* newRoot;
        newRoot = generateSum(root);
        long long sum = newRoot->val;
        long long ans = support(newRoot, sum, newRoot);
        return ans%MOD;
    }
};

auto init = []() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    return 'c';
}();
96638