Easy Solution || 99.21% Beat ||
\n\n\n# Code\n\nclass Solution {\npublic:\n    int numFactoredBinaryTrees(vector<int>& arr)\n    {\n        sort(arr.begin(), arr.end());\n        int len = arr
\n![image.png](https://assets.leetcode.com/users/images/32afac75-4ec0-4827-b832-65e043c7d39d_1698286943.1287987.png)\n\n# Code\n```\nclass Solution {\npublic:\n    int numFactoredBinaryTrees(vector<int>& arr)\n    {\n        sort(arr.begin(), arr.end());\n        int len = arr.size();\n        long ans = 0;\n        unordered_map<int, long> m;\n        for (int num : arr) {\n            long ways = 1;\n            double lim = sqrt(num);\n            for (int j = 0, fA = arr[0]; fA <= lim; fA = arr[++j]) {\n                if (num % fA != 0) continue;\n                int fB = num / fA;\n                if (m.find(fB) != m.end())\n                    ways = (ways + m[fA] * m[fB] * (fA == fB ? 1 : 2)) % 1000000007;\n            }\n            m[num] = ways;\n            ans = (ans + ways) % 1000000007;\n        }\n        return (int)ans;\n    }\n};\n```
['C++']