<p>Given an array of unique integers, <code>arr</code>, where each integer <code>arr[i]</code> is strictly greater than <code>1</code>.</p>

<p>We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node&#39;s value should be equal to the product of the values of its children.</p>

<p>Return <em>the number of binary trees we can make</em>. The answer may be too large so return the answer <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [2,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can make these trees: <code>[2], [4], [4, 2, 2]</code></pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [2,4,5,10]
<strong>Output:</strong> 7
<strong>Explanation:</strong> We can make these trees: <code>[2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2]</code>.</pre>

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

<ul>
	<li><code>1 &lt;= arr.length &lt;= 1000</code></li>
	<li><code>2 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
	<li>All the values of <code>arr</code> are <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    const int mod = 1e9+7;
    // int solve(vector<vector<pair<int,int>>> &arr, int i, vector<int> &org, vector<int> &dp){
    //     if(i==arr.size()) return 0;
    //     if(dp[i]!=-1) return dp[i];
    //     int ans = 1;
    //     for(auto p : arr[i]){
    //         int tmp1 = solve(arr,p.first,org,dp);
    //         int tmp2 = solve(arr,p.second,org,dp);
    //         if(org[p.first]==org[p.second]){
    //             ans+=(tmp1*tmp2);
    //         }else{
    //             ans+=(2*tmp1*tmp2);
    //         }
    //     }
    //     return dp[i]=ans;
    // }

    int numFactoredBinaryTrees(vector<int>& arr) {
        int sz = arr.size();
        vector<vector<pair<int,int>>> temp(sz);
        sort(arr.begin(),arr.end());
        for(int i=0;i<sz;i++){
            unordered_map<int,int> umap;
            for(int j=0;j<i;j++){
                if(arr[i]%arr[j]==0 && umap.find(arr[j])==umap.end()){
                    int tmp = arr[i]/arr[j];
                    bool pres = binary_search(arr.begin(),arr.end(),tmp);
                    if(pres){
                        umap[arr[j]]=1;
                        umap[tmp]=1;
                        int indx = lower_bound(arr.begin(),arr.end(),tmp)-arr.begin();
                        temp[i].push_back({j,indx});
                    }

                }
            }
        }
        vector<long long> dp(sz,1);
        
        // write bottem up dp
        for(int i=0;i<sz;i++){
            for(auto p : temp[i]){
                long long tmp1 = dp[p.first];
                long long tmp2 = dp[p.second];
                if(arr[p.first]==arr[p.second]){
                    dp[i]=(dp[i]%mod+((tmp1%mod)*(tmp2%mod))%mod)%mod;
                }else{
                    dp[i]=(dp[i]%mod+(2*(tmp1%mod)*(tmp2%mod))%mod)%mod;
                }
            }
        }
        int ans = 0;
        for(int x : dp){
            ans=(ans%mod+x%mod)%mod;
        }
        return ans;
    }
};
15300