Java easy understanding DP solution with explanation
Start to calculate from the smallest element to the biggest element in int[] A.\nIf there are two elements A[s], A[e] where A[s]*A[e] == A[i] and s<i && e<i\nth
Start to calculate from the smallest element to the biggest element in ```int[] A```.\nIf there are two elements ```A[s]```, ```A[e]``` where ```A[s]*A[e] == A[i] and s<i && e<i```\nthat means we have found ```dp[s]*dp[e]``` more trees for element ```A[i]```.\n```\nclass Solution {\n    public int numFactoredBinaryTrees(int[] A) {\n        long res = 0L, mod = (long) 1000000007;\n        long[] dp = new long[A.length];\n        Arrays.fill(dp,1);\n        Arrays.sort(A);\n        for(int i = 1; i < A.length; i++) {\n            int s = 0, e = i-1;\n            while(s <= e) {\n                if(A[s]*A[e] > A[i])e--;\n                else if(A[s]*A[e] < A[i])s++;\n                else {\n                    dp[i] = ((dp[s]*dp[e]*(A[s] == A[e]? 1:2)%mod+dp[i]))%mod;\n                    s++;\n                }\n            }\n        }\n        for(long d: dp) {\n            res = (d+res)%mod;\n        }\n        return (int)res;\n    }\n}\n```
[]