Java DP
\nclass Solution {\n    int[] arr;\n    public int numTeams(int[] rating) {\n        arr = rating;\n        return findLarge() + findSmall();\n    }\n    privat
```\nclass Solution {\n    int[] arr;\n    public int numTeams(int[] rating) {\n        arr = rating;\n        return findLarge() + findSmall();\n    }\n    private int findLarge() {\n        int[] dp = new int[arr.length];\n        // first loop, count of smaller on the left;\n        for (int i = 0; i < arr.length; i++) {\n            for (int j = 0; j < i; j++) {\n                if (arr[j] < arr[i]) {\n                    dp[i]++;\n                }\n            }\n        }\n        //second loop, collct count of subsequence ending of i;\n        int res = 0;\n        for (int i = 0; i < arr.length; i++) {\n            for (int j = 0; j < i; j++) {\n                if (arr[j] < arr[i]) {\n                    res += dp[j];\n                }\n            }\n        }\n        return res;\n    }\n    private int findSmall() {  // same as count Larger\n        int[] dp = new int[arr.length];\n        for (int i = 0; i < arr.length; i++) {\n            for (int j = 0; j < i; j++) {\n                if (arr[j] > arr[i]) {\n                    dp[i]++;\n                }\n            }\n        }\n        int res = 0;\n        for (int i = 0; i < arr.length; i++) {\n            for (int j = 0; j < i; j++) {\n                if (arr[j] > arr[i]) {\n                    res += dp[j];\n                }\n            }\n        }\n        return res;\n    }\n}\n```
[]