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

typedef long long int ll;
class Solution {
public:
    int M = (int)1e9+7;
    vector<int> findFactors(int n){
        vector<int> factors;
        for(int i=1;i*i<=n;i++){
            if(n%i==0){
                factors.push_back(i);
            }
        }
        return factors;
    }

    ll mulmod(ll a, ll b, ll mod)
{
    ll res = 0; // Initialize result
    a = a % mod;
    while (b > 0)
    {
        // If b is odd, add 'a' to result
        if (b % 2 == 1)
            res = (res + a) % mod;
 
        // Multiply 'a' with 2
        a = (a * 2) % mod;
 
        // Divide b by 2
        b /= 2;
    }
 
    // Return result
    return res % mod;
}

    int memo[(int)1e3+1];
    int dp(int i,vector<int> &arr,map<int,int> &mp){
        vector<int> factors = findFactors(arr[i]);
        if(factors.size()==1){
            return 1;
        }
        if(memo[i]!=-1){
            return memo[i];
        }
        int ans = 0;
        for(auto factor:factors){
            if(factor==1){
                ans+=1;
            }
            else{
                if(mp.find(factor)!=mp.end() && mp.find(arr[i]/factor)!=mp.end()){
                    int left = dp(mp[factor],arr,mp);
                    int right = dp(mp[arr[i]/factor],arr,mp);
                    if(factor!=arr[i]/factor){
                        ans= (ans%M + mulmod(2*left,right,M))%M;
                    }
                    else{
                        ans= (ans%M + mulmod(left,right,M))%M;
                    }
                }
            }
        }
        return memo[i] =  ans;
    }

    int numFactoredBinaryTrees(vector<int>& arr) {
        int n = arr.size();
        memset(memo,-1,sizeof(memo));
        map<int,int> mp;
        for(int i=0;i<n;i++){
            mp[arr[i]] = i;
        }
        int ans = 0;
        for(int i=0;i<n;i++){
            ans= (ans%M + dp(i,arr,mp)%M)%M;
        }
        return ans;
    }
};
36900