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

int MOD = 1e9 + 7;

class Solution
{
    vector<pair<int, int>> getFactors(int n)
    {
        vector<pair<int, int>> ans;

        for (int i = 2; i * i <= n; i++)
        {
            if (n % i == 0)
            {
                ans.push_back({i, n / i});
            }
        }

        return ans;
    }

    long long numFactoredBinaryTrees(set<int> &st, int par, unordered_map<int, long long>& dpMap)
    {
        if(dpMap.count(par) > 0)
        {
            return dpMap[par];
        }

        vector<pair<int, int>> factors = getFactors(par);

        long long ans = 1;

        for (int i = 0; i < factors.size(); i++)
        {
            int a = factors[i].first;
            int b = factors[i].second;

            if (st.find(a) != st.end() && st.find(b) != st.end())
            {
                if (a == b)
                {
                    ans += (numFactoredBinaryTrees(st, a, dpMap) % MOD) * (numFactoredBinaryTrees(st, b, dpMap) % MOD);

                    ans %= MOD;
                }
                else
                {
                    ans += 2 * (numFactoredBinaryTrees(st, a, dpMap) % MOD) * (numFactoredBinaryTrees(st, b, dpMap) % MOD);

                    ans %= MOD;
                }
            }
        }

        return dpMap[par] = ans;
    }

public:
    int numFactoredBinaryTrees(vector<int> &arr)
    {
        set<int> st;
        int n = arr.size();
        for (int i = 0; i < n; i++)
        {
            st.insert(arr[i]);
        }

        unordered_map<int, long long> dpMap;

        long long ans = 0;

        for (auto it : st)
        {
            int smallAns = numFactoredBinaryTrees(st, it, dpMap)% MOD;
            ans += smallAns;
            ans %= MOD;
        }

        return ans;
    }
};
36300