Efficient Algorithm || 91.15% faster || 91.15% space optimised || O(n^2) time
\nclass Solution {\n    public int numFactoredBinaryTrees(int[] a) {\n        int n = a.length;\n        long mod = (long)(1e9) + 7;\n        Arrays.sort(a);\n 
```\nclass Solution {\n    public int numFactoredBinaryTrees(int[] a) {\n        int n = a.length;\n        long mod = (long)(1e9) + 7;\n        Arrays.sort(a);\n        \n        long ans[] = new long[n];\n        long sum = 0;\n        for(int i=0;i<n;i++){\n            long now = 1;\n            int j = 0;\n            int k = i-1;\n            while(j<=k){\n                long mul = (long)(a[j]) * a[k];\n                if(mul > (long)a[i]){\n                    k--;\n                    continue;\n                }\n                else if(mul < (long)a[i]){\n                    j++;\n                    continue;\n                }\n                else{\n                    if(j == k)\n                        now = (now + (ans[j] * ans[k])%mod)%mod;\n                    else\n                        now = (now + (ans[j] * ans[k] * 2)%mod)%mod;\n                    j++;\n                    k--;\n                }\n            }\n            ans[i] = (int)now;\n            sum = (sum+ans[i])%mod;\n        }\n        return (int)sum;\n    }\n}\n```
['Dynamic Programming', 'Java']