C++ dp O(N^2) 88ms
\n\n    int numFactoredBinaryTrees(vector& arr) {\n        std::sort(arr.begin(), arr.end());\n        int n = arr.size();\n        vector dp(n, 1);\n        in
\n\n    int numFactoredBinaryTrees(vector<int>& arr) {\n        std::sort(arr.begin(), arr.end());\n        int n = arr.size();\n        vector<long long> dp(n, 1);\n        int mod = 1e9 + 7;\n        std::unordered_map<int, int> pos;\n        for(int i = 0; i < n; ++i) {\n            pos[arr[i]] = i;\n        }\n        \n        for(int i = 0; i < n; ++i) {\n            for(int j = 0; j < i; ++j) {\n                if (arr[i] % arr[j] > 0 ) continue;\n                int current = arr[i] / arr[j];\n                if (pos.find(current) != pos.end()) {\n                    dp[i] += dp[pos[current]] * dp[j];\n                }\n            }\n        }\n        \n        long long res = 0;\n        for(auto& c : dp) {\n            res += c;\n        }\n        return res % mod;\n    }
[]