Easy Python O(nlogn) using SortedList
Look at each soldier, and consider how many teams can be made using them as the middle member. That can be calculated as follows:\n\n(smaller soldiers to left) 
Look at each soldier, and consider how many teams can be made using them as the middle member. That can be calculated as follows:\n\n(smaller soldiers to left) * (larger soldiers to right) + (larger soldiers to left) * (smaller soldiers to right)\n\nTo accomplish this, we use a SortedList and do an initial pass to find how many smaller and larger soldiers exist before each soldier, and then another pass to find how many smaller and larger soldiers exist after each of them.\n\n```\nfrom sortedcontainers import SortedList\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        before = SortedList()\n        before_smaller = [0] * len(rating)\n        before_larger = [0] * len(rating)\n\n        for i, soldier in enumerate(rating):\n            before.add(soldier)\n            index = before.index(soldier)\n            before_smaller[i] = index\n            before_larger[i] = len(before) - index - 1\n        \n        total = 0\n        after = SortedList()\n\n        for i in range(len(rating) - 1, -1, -1):\n            soldier = rating[i]\n            after.add(soldier)\n            index = after.index(soldier)\n            total += before_smaller[i] * (len(after) - index - 1)\n            total += before_larger[i] * index\n   \n        return total\n```
[]