Clean python O(n^2) solution using dictionaries
\nclass Solution:\n    def numTeams(self, arr: List[int]) -> int:\n\t\t# cache to store indices which have a value greater than current index\n        memoGreat
```\nclass Solution:\n    def numTeams(self, arr: List[int]) -> int:\n\t\t# cache to store indices which have a value greater than current index\n        memoGreater = {}\n        \n\t\t# cache to store indices which have a value lesser than current index\n\t\tmemoLesser = {}\n        length = len(arr)\n        \n\t\tfor i in range(length):\n            memoGreater[i] = []\n            memoLesser[i] = []\n            for j in range(i+1, length):\n                if arr[j] > arr[i]:\n                    memoGreater[i].append(j)\n                elif arr[j] < arr[i]:\n                    memoLesser[i].append(j)\n        \n        numTeams = 0\n\t\t# add the teams using the conditions specified\n        for i in range(length):\n            for index in memoGreater[i]:\n                numTeams += len(memoGreater[index])\n            for index in memoLesser[i]:\n                numTeams += len(memoLesser[index])\n        \n        return numTeams\n```
['Python', 'Python3']