[Java] DP solution O(n^2)
\nclass Solution {\n    public int numFactoredBinaryTrees(int[] arr) {\n        int mod = 1000000007;\n        Map<Integer,Long>trees = new HashMap<>();\n\t\tAr
```\nclass Solution {\n    public int numFactoredBinaryTrees(int[] arr) {\n        int mod = 1000000007;\n        Map<Integer,Long>trees = new HashMap<>();\n\t\tArrays.sort(arr);\n        \n        for (int i=0;i<arr.length;i++){\n            long currentTreesNum=1;\n            for (int j=0;j<i;j++){\n                if (arr[i]%arr[j]==0 && trees.containsKey(arr[i]/arr[j])){\n                    currentTreesNum=(currentTreesNum+(trees.get(arr[i]/arr[j])*trees.get(arr[j]))%mod)%mod;\n                }\n            }\n            trees.put(arr[i], currentTreesNum);\n        }\n        long res = 0;\n        for (long treeNum: trees.values())res=(res+treeNum)%mod;\n        return (int)res;\n    }\n}\n```
[]