Java, 5ms
For ith soldier: consider he/she is the middle person of 3 person team.\n\nThen, for a given line up, soldier can be part of smallL * bigR teams if the ratings 
For `i`th soldier: consider he/she is the middle person of 3 person team.\n\nThen, for a given line up, soldier can be part of `smallL * bigR` teams if the ratings are in `ascending` order. \nSimalarly, `bigL * smallR` for ratings in `descending` order.\n```\nclass Solution {\n    public int numTeams(int[] rating) {\n        int teams = 0;\n        int smallL = 0, smallR = 0;\n        int bigL = 0, bigR = 0;\n        \n        for(int i = 1; i < rating.length-1; i++) {\n            for(int j = i-1; j >=0; j--) {\n               if(rating[i] > rating[j]) {\n                   smallL++;\n               } else {\n                   bigL++;\n               }\n            }\n            for(int j = i+1; j < rating.length; j++) {\n                if(rating[i] < rating[j]) {\n                    bigR++;\n                } else {\n                    smallR++;\n                }\n            }\n            teams += (smallL * bigR) + (bigL * smallR);\n            smallL = smallR = bigR = bigL = 0;\n        }\n        return teams;\n    }\n}\n```
[]