python, >97.98%, simple logic and very detailed explanation
\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        asc = dsc = 0\n        for i,v in enumerate(rating):\n            llc = rgc = lgc 
```\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        asc = dsc = 0\n        for i,v in enumerate(rating):\n            llc = rgc = lgc = rlc =0\n            for l in rating[:i]:\n                if l < v:\n                    llc += 1\n                if l > v:\n                    lgc += 1\n            for r in rating[i+1:]:\n                if r > v:\n                    rgc += 1\n                if r < v:\n                    rlc += 1\n            asc += llc * rgc\n            dsc += lgc * rlc            \n        return asc + dsc\n```\n\nThe logic is so simple - it actually passed all tests on the first try :)\n\nSo here is how it works.\n\nWe are going to have a main for loop and we are going to pick one element from the `ratings` list at a time. Then we are going to  have two other loops. One is going to scan all elements before and the other one is going to scan all elements after. Those two loops will count number of elements less and greater than the one we picked. So let\'s say we have 2 elements less than the selected one to the left and 3 elements greater to the right. That means the number of ascending sequences we can build is `2 * 3 = 6` . Now we just need to extend the same logic to all descending sequences and return the total.\n\nIt may sound crazy, but if you want a more visual picture - imaging walking accross city and crossing streets. You come to a street, look at your left, count, cross half way, look to the right , count, then finish crossing the street and walk to the next one. While the underlying activity is quite different, when you write the code - that visual helps to implement the proper structure.\n\nOk, so let\'s write the code now:\n\nFirst, let\'s define the variables. Technically we can use just one counter, but for clarity, let\'s use two, `asc` for ascending and `dsc` for descendig sequences:\n\n```\n        asc = dsc = 0\n```\n\nNow let\'s start our main loop to scan all elements:\n\n```\n        for i,v in enumerate(rating):\n```\n\nInside every cycle we are going to use 4 local counters: `llc` - stands for `Left, Less, Counter` - count of elements less then the current one to the left. `rgc` - `Right, Greater, Counter` - count of elements greater than the current one to the right.  I think you can guess what the other 2 are?\n\n```\n            llc = rgc = lgc = rlc =0\n```\n\nOk, now we picked some element, we are going to look to the left and count element less and greater than the current one:\n\n```\n            for l in rating[:i]:\n                if l < v:\n                    llc += 1\n                if l > v:\n                    lgc += 1\n```\n\nAnd now we look to the right:\n\n```\n            for r in rating[i+1:]:\n                if r > v:\n                    rgc += 1\n                if r < v:\n                    rlc += 1\n```\n\nAnd update the total counters:\n\n```\n            asc += llc * rgc\n            dsc += lgc * rlc            \n```\n\nOnce the main loop is complete, we just return the total total:\n```\n        return asc + dsc\n```\n\nOk, all done!\n\nWe can add some polishing touches:\n\n1. Since we really need just one counter, we can replace `asc` and `dsc` with a single counter `c`.\n2. Since we need to have same elements to the left and to the right, we can skip the the first and the last elements in the main loop scan.\n3. Replace 2nd `if` with `elsif` - that would reduce number of logical operations - since if the first condition is `True`, the second won\'t be evaluated.\n\nSo here is the improved version:\n\n\n```\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        c = 0\n        for i,v in enumerate(rating[1:-1]):\n            llc = rgc = lgc = rlc = 0\n            for l in rating[:i+1]:\n                if l < v:\n                    llc += 1\n                elif l > v:\n                    lgc += 1\n            for r in rating[i+2:]:\n                if r > v:\n                    rgc += 1\n                elif r < v:\n                    rlc += 1\n            c += llc * rgc + lgc * rlc           \n        return c\n```\n\nI can a few tests and it looks like the 1st version peaked at 94% and the 2nd at 97.98%, so maybe those improvements added a few per cents.\n\nThanks for reading!
[]