[Java] Recursive solution
\nclass Solution {\n    private static final long MOD = (long) 1e9 + 7;\n    \n    public int numFactoredBinaryTrees(int[] A) {\n        Arrays.sort(A);\n      
```\nclass Solution {\n    private static final long MOD = (long) 1e9 + 7;\n    \n    public int numFactoredBinaryTrees(int[] A) {\n        Arrays.sort(A);\n        int n = A.length;\n        Map<Integer, Long> dp = new HashMap<>();\n\n        long answer = 0;\n        for (int num : A) {\n            dfs(dp, A, num);\n            answer = (answer + dp.get(num)) % MOD;\n        }\n        \n        return (int) answer;\n    }\n    \n    private long dfs(Map<Integer, Long> dp, int[] A, int cur) {\n        if (cur < 0) {\n            return 0;\n        }\n        \n        if (cur == 1) {\n            return 1;\n        }\n        \n        if (dp.containsKey(cur)) {\n            return dp.get(cur);\n        }\n        \n        long answer = 0;\n        for (int num : A) {\n            if (cur % num == 0) {\n                int quotient = cur / num;\n                \n                if (quotient == 1) {\n                    answer = (answer + 1) % MOD;\n                } else if (Arrays.binarySearch(A, quotient) >= 0) {\n                    answer = (answer + dfs(dp, A, quotient) * dfs(dp, A, num)) % MOD;\n                }\n            }\n        }\n        \n        dp.put(cur, answer);\n        return answer;\n    }\n}\n```
[]