[C++/Java/Python] DP solution
Sort the list A at first. Scan A from small element to bigger.\n\nDP equation:\ndp[i] = sum(dp[j] * dp[i / j])\nres  = sum(dp[i])\nwith i, j, i / j in the list 
Sort the list `A` at first. Scan `A` from small element to bigger.\n\nDP equation:\n`dp[i] = sum(dp[j] * dp[i / j])`\n`res  = sum(dp[i])`\nwith `i, j, i / j` in the list `L`\n\n\n**C++**\n```cpp\n    int numFactoredBinaryTrees(vector<int>& A) {\n        long res = 0, mod = pow(10, 9) + 7;\n        sort(A.begin(), A.end());\n        unordered_map<int, long> dp;\n        for (int i = 0; i < A.size(); ++i) {\n            dp[A[i]] = 1;\n            for (int j = 0; j < i; ++j)\n                if (A[i] % A[j] == 0)\n                    dp[A[i]] = (dp[A[i]] + dp[A[j]] * dp[A[i] / A[j]]) % mod;\n            res = (res + dp[A[i]]) % mod;\n        }\n        return res;\n    }\n```\n**Java**\n```java\n    public int numFactoredBinaryTrees(int[] A) {\n        long res = 0L, mod = (long)1e9 + 7;\n        Arrays.sort(A);\n        HashMap<Integer, Long> dp = new HashMap<>();\n        for (int i = 0; i < A.length; ++i) {\n            dp.put(A[i], 1L);\n            for (int j = 0; j < i; ++j)\n                if (A[i] % A[j] == 0)\n                    dp.put(A[i], (dp.get(A[i]) + dp.get(A[j]) * dp.getOrDefault(A[i] / A[j], 0L)) % mod);\n            res = (res + dp.get(A[i])) % mod;\n        }\n        return (int) res;\n    }\n```\n\n**Python**\n```py\n    def numFactoredBinaryTrees(self, A):\n        dp = {}\n        for a in sorted(A):\n            dp[a] = sum(dp[b] * dp.get(a / b, 0) for b in dp if a % b == 0) + 1\n        return sum(dp.values()) % (10**9 + 7)\n```\n
[]