C++ Clear Clean & Understandable Solution
m stores for each integer in arr the count of binarytrees\n  that can be represented when this integer is the head\n\nPython\n\nclass Solution:\n    def numFact
m stores for each integer in arr the count of binarytrees\n  that can be represented when this integer is the head\n\n**Python**\n```\nclass Solution:\n    def numFactoredBinaryTrees(self, arr: List[int]) -> int:\n        mod = 10 ** 9 + 7\n        m = {}\n        res = 0\n        \n        arr.sort()\n        \n        for head in arr:\n            m[head] = 1\n            for child1 in m.keys():\n                child2 = head / child1\n                if child2 in m:\n                    tmp = (m[child1] * m[child2]) % mod\n                    m[head] = (m[head] + tmp) % mod\n                    \n            res = (res + m[head]) % mod\n            \n        return res\n```\n**C++**\n\n```\nclass Solution {\npublic:\n    int numFactoredBinaryTrees(vector<int>& arr) {\n        int mod = 1e9 + 7;\n        unordered_map<int, int> m;\n        int res = 0;\n        \n        sort(arr.begin(), arr.end());\n        \n        for (int head : arr)\n        {\n            m[head] = 1;\n            for (auto [child1, value] : m)\n            {\n                int child2 = head / child1;\n                if (child1 *child2 == head && m.find(child2) != m.end())\n                {\n                    long tmp = ((long)m[child1] * (long)m[child2]) % mod;\n                    m[head] = (m[head] + tmp) % mod;\n                }\n            }\n            res = (res + m[head]) % mod;\n        }\n       \n        return res;\n    }\n};\n```
[]