Java simple and easy to understand solution, using DP, clean code with comments
PLEASE UPVOTE IF YOU LIKE THIS SOLUTION\n\n\nclass Solution {\n    static int MOD = 1_000_000_007;\n    public int numFactoredBinaryTrees(int[] arr) {\n        
**PLEASE UPVOTE IF YOU LIKE THIS SOLUTION**\n\n```\nclass Solution {\n    static int MOD = 1_000_000_007;\n    public int numFactoredBinaryTrees(int[] arr) {\n        int n = arr.length;\n        \n        Arrays.sort(arr);\n        \n        //consider as root for each number\n        //save the count of BT\n        long[] dp = new long[n];\n        Arrays.fill(dp, 1);\n        \n        //num to index mapping\n        Map<Integer, Integer> mapIndex = new HashMap();\n        for(int i = 0; i < n; i++)\n            mapIndex.put(arr[i], i);\n        \n        for(int rootIndex = 0; rootIndex < n; rootIndex++){\n            int root = arr[rootIndex];\n            \n            for(int leftIndex = 0; leftIndex < rootIndex; leftIndex++){\n                int left = arr[leftIndex];\n                \n                if(root % left == 0){\n                    int right = root / left;\n                    \n                    if(mapIndex.containsKey(right)){\n                        int rightIndex = mapIndex.get(right);\n                        \n                        dp[rootIndex] = (dp[rootIndex] + (dp[leftIndex] * dp[rightIndex])) % MOD; \n                    }\n                }\n            }\n        }\n        \n        //count all the trees\n        long counts = 0;\n        for(long count : dp) counts += count;\n        \n        \n        return (int) (counts % MOD);\n    }\n}\n```
['Dynamic Programming']