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

// /**
//  * Definition for a binary tree node.
//  * struct TreeNode {
//  *     int val;
//  *     TreeNode *left;
//  *     TreeNode *right;
//  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
//  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
//  * };
//  */
// class Solution {
// public:
//     long long sum(TreeNode* root){
//         if(root==NULL){
//             return 0;
//         }
//         long long currsum=root->val;
//         currsum+=sum(root->left);
//         currsum+=sum(root->right);
//         return currsum;
//     }

//     void dfs(TreeNode* root,long long &total,long long &maxpro,long long &mod){
//         if(root==NULL){
//             return;
//         }
//         long long curr_sum=sum(root);
//         long long curr_pro=(total-curr_sum)*curr_sum*1ll;
//         maxpro=max(maxpro,curr_pro);
//         dfs(root->left,total,maxpro,mod);
//         dfs(root->right,total,maxpro,mod);
//     }



//     int maxProduct(TreeNode* root) {
//        long long maxpro=1;
//        long long total=sum(root);
//        long long mod=1e9+7;
//        dfs(root,total,maxpro,mod);
//        maxpro%=mod;
//        return maxpro;
//     }
// };

class Solution {
public:
    long long mod = 1e9+7;
    
    
    long long calculateSubtreeSums(TreeNode* root, unordered_map<TreeNode*, long long>& subtreeSums) {
        if (root == NULL) {
            return 0;
        }
        long long currSum = root->val;
        currSum += calculateSubtreeSums(root->left, subtreeSums);
        currSum += calculateSubtreeSums(root->right, subtreeSums);
        subtreeSums[root] = currSum;
        return currSum;
    }

    
    void dfs(TreeNode* root, long long total, long long& maxpro, unordered_map<TreeNode*, long long>& subtreeSums) {
        if (root == NULL) {
            return;
        }
        long long currSum = subtreeSums[root];
        long long currPro = (total - currSum) * currSum;
        maxpro = max(maxpro, currPro);
        dfs(root->left, total, maxpro, subtreeSums);
        dfs(root->right, total, maxpro, subtreeSums);
    }

    int maxProduct(TreeNode* root) {
        unordered_map<TreeNode*, long long> subtreeSums;
        long long total = calculateSubtreeSums(root, subtreeSums); 
        long long maxpro = 0;
        dfs(root, total, maxpro, subtreeSums);
        return maxpro % mod;
    }
};

107821