There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:

Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if:  (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).

Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
 
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1). 

Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.

Example 3:
Input: rating = [1,2,3,4]
Output: 4

 
Constraints:

n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5

Difficulty: interview

Starter code:

class Solution:
    def numTeams(self, rating: List[int]) -> int:
        

Solutions:
["class Solution:\n    def increment_index(self, nums, index):\n        index += 1\n        while index < len(nums):\n            nums[index] += 1\n            index += (index & -index)\n\n    def prefix_sum(self, nums, index):\n        index += 1\n        current_sum = 0\n        while index > 0:\n            current_sum += nums[index]\n            index -= (index & -index)\n        return current_sum\n\n    def numTeams(self, rating):\n        if len(rating) < 3:\n            return 0\n\n        n = len(rating)\n        sorted_nums = rating.copy()\n        sorted_nums.sort()\n\n        index = {}\n        for i in range(n):\n            index[sorted_nums[i]] = i\n\n        fenwick_tree = [0] * (len(sorted_nums) + 1)\n\n        lesser_before = [0] * n\n        for i in range(n):\n            rate_i = rating[i]\n            index_i = index[rate_i]\n            lesser_before[i] = self.prefix_sum(fenwick_tree, index_i)\n            self.increment_index(fenwick_tree, index[rating[i]])\n\n        for i in range(len(fenwick_tree)):\n            fenwick_tree[i] = 0\n\n        lesser_after = [0] * n\n        for i in range(n - 1, -1, -1):\n            rate_i = rating[i]\n            index_i = index[rate_i]\n            lesser_after[i] = self.prefix_sum(fenwick_tree, index_i)\n            self.increment_index(fenwick_tree, index[rating[i]])\n\n        num_teams = 0\n        for i in range(n - 1):\n            num_teams += lesser_before[i] * (n - 1 - i - lesser_after[i])\n            num_teams += (i - lesser_before[i]) * lesser_after[i]\n\n        return num_teams\n", "class FenwickTree:\n    def __init__(self, nums_node: int):\n        self.nums_node = nums_node\n        self.arr = [0] * nums_node\n        self.total = 0\n\n    def get_sum(self, index: int):\n        if index < 0:\n            return self.total - self.get_sum(~index)\n        if index >= self.nums_node:\n            return self.total\n        result = 0\n        while index >= 0:\n            result += self.arr[index]\n            index = (index & (index + 1)) - 1\n        return result\n\n    def update(self, index: int, delta: int):\n        self.total += delta\n        while index < self.nums_node:\n            self.arr[index] += delta\n            index = index | (index + 1)\n\n\nclass Solution:\n    def numTeams(self, rating: list) -> int:\n        count = 0\n        rating_len = len(rating)\n\n        sort_map = {r: i for i, r in enumerate(sorted(rating))}\n\n        left_tree = FenwickTree(rating_len)\n        right_tree = FenwickTree(rating_len)\n\n        for rat in rating:\n            right_tree.update(sort_map[rat], 1)\n\n        for rat in rating:\n            index = sort_map[rat]\n            right_tree.update(index, -1)\n            count += (left_tree.get_sum(index) *\n                      right_tree.get_sum(~index))\n            count += (left_tree.get_sum(~index) * right_tree.get_sum(index))\n            left_tree.update(index, 1)\n\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        res = 0\n        \n        for i in range(1, len(rating) -1):\n            l_small, l_great = 0,0\n            for j in range(i):\n                if rating[i]<rating[j]:\n                    l_great += 1\n                elif rating[i]>rating[j]:\n                    l_small+=1\n                    \n            r_small, r_great = 0,0\n            for k in range(i+1, len(rating)):\n                if rating[i]>rating[k]:\n                    r_small+=1\n                elif rating[k]>rating[i]:\n                    r_great+=1\n                    \n            res += l_small*r_great\n            res += l_great*r_small\n            \n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n\n        \n        count = 0\n        \n        for i in range(len(rating)):\n            map_g = {}\n            map_l = {}\n            for j in range(i + 1, len(rating)):\n                count += sum([k < rating[j] for k in list(map_l.keys())])+ sum([k > rating[j] for k in list(map_g.keys())])\n                if rating[i] < rating[j]:\n                    map_l[rating[j]] = map_l.get(rating[j], 0) + 1\n                if rating[i] > rating[j]:\n                    map_g[rating[j]] = map_g.get(rating[j], 0) + 1\n        return count\n", "# x x x x x\n#   i   j\n\n# xi != xj\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            firstChoice = rating[i]\n            for j in range(i + 1, len(rating)):\n                secondChoice = rating[j]\n                for k in range(j + 1, len(rating)):\n                    thirdChoice = rating[k]\n                    if (firstChoice < secondChoice < thirdChoice) or (firstChoice > secondChoice > thirdChoice):\n                        count += 1\n        return count        \n            \n", "import itertools\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        return sum(\n            1\n            for ri, rj, rk in itertools.combinations(rating, 3)\n            if (ri < rj < rk) or (ri > rj > rk)\n        )\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cpt = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                if rating[j]>rating[i]:\n                    for k in range(j+1,len(rating)):\n                        if rating[k]>rating[j]:\n                            cpt += 1\n                else:\n                    for k in range(j+1,len(rating)):\n                        if rating[k]<rating[j]:\n                            cpt += 1\n        return cpt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        return len([1 for i,j,k in itertools.combinations_with_replacement(rating,3) if i<j<k or i>j>k])", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in rating:\n            for j in rating[rating.index(i)+1:]:\n                for k in rating[rating.index(j)+1:]:\n                    if i < j < k or k < j < i:\n                        cnt += 1\n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(len(rating)):\n            fst = rating[i]\n            for j in range(i + 1, len(rating)):\n                scn = rating[j]\n                if fst == scn:\n                    break\n                a = fst > scn\n                for k in range(j + 1, len(rating)):\n                    thr = rating[k]\n                    if scn == thr:\n                        break\n                    \n                    if a and scn > thr:\n                        cnt = cnt + 1\n                        \n                    if not a and scn < thr:\n                        cnt = cnt + 1\n                        \n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n       \n        def getTeams(k, l, prev = None, asc=True):\n            if (k>len(l)):\n                return 0\n            if ((prev is None) & (k == 1)):\n                return len(l)\n            if (k == 1):\n                if asc: \n                    teamCount = sum([x>prev for x in l])\n                else:\n                    teamCount = sum([x<prev for x in l])\n            else:\n                teamCount = 0\n                larger_x = True\n                if prev is not None:\n                    larger_x = l[0]>prev\n                    \n                    \n                if ((larger_x == asc) | (prev is None)):\n                    #include this one\n                    teamCount +=getTeams(k-1, l[1:], l[0], asc)\n                    #exclude this one\n                teamCount +=getTeams(k, l[1:], prev, asc)\n                    \n                    \n                if(0):    \n                    if asc:\n                        if (larger_x | (prev is None)):\n                        #include this one\n                            teamCount +=getTeams(k-1, l[1:], l[0], asc)\n                        #exclude this one\n                        teamCount +=getTeams(k, l[1:], prev, asc)\n\n                    else:\n                        if (larger_x == False | (prev is None)):\n                            #print(prev, l[0], larger_x)\n                        #include this one\n                            teamCount +=getTeams(k-1, l[1:], l[0], asc)\n                        #exclude this one\n                        teamCount +=getTeams(k, l[1:], prev, asc)\n                #print('getTeams: ', k, l, prev, asc,'   result: ', teamCount)\n            return teamCount\n        \n        return getTeams(3, rating, None, True) + getTeams(3, rating, None, False)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n       \n        def getTeams(k, l, prev = None, asc=True):\n            if (k>len(l)):\n                return 0\n            if ((prev is None) & (k == 1)):\n                return len(l)\n            if (k == 1):\n                if asc: \n                    teamCount = sum([x>prev for x in l])\n                else:\n                    teamCount = sum([x<prev for x in l])\n            else:\n                teamCount = 0\n                larger_x = True\n                if prev is not None:\n                    larger_x = l[0]>prev\n                    \n                    \n                if ((larger_x == asc) | (prev is None)):\n                    #include this one\n                    teamCount +=getTeams(k-1, l[1:], l[0], asc)\n                    #exclude this one\n                teamCount +=getTeams(k, l[1:], prev, asc)\n                    \n            return teamCount\n        \n        return getTeams(3, rating, None, True) + getTeams(3, rating, None, False)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        l = len(rating)\n        res = 0\n        for i, vi in enumerate(rating):\n            if i >= l - 2:\n                break\n                \n            for j, vj in enumerate(rating[i+1:]):\n                if j >= l - 1:\n                    break\n                \n                for k, vk in enumerate(rating[i+j+1:]):\n                    if (vi < vj and vj < vk) or (vi > vj and vj > vk):\n                        res += 1\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n=len(rating)\n        teams=0\n        for i in range(1,n-1):\n            lt_l,lt_r,gt_l,gt_r=0,0,0,0\n            for j in range(n):\n                if rating[i]<rating[j]:\n                    if i<j:\n                        gt_r+=1\n                    else:\n                        gt_l+=1\n                elif rating[i]>rating[j]:\n                    if i<j:\n                        lt_r+=1\n                    else:\n                        lt_l+=1\n            teams+=lt_l*gt_r+gt_l*lt_r\n        \n        # for i in range(n):\n        #     for k in range(i+2,n):\n        #         if rating[i]<rating[k]:\n        #             teams+=len([r for r in rating[i+1:k] if rating[i]<r<rating[k]])\n        #         else:\n        #             teams+=len([r for r in rating[i+1:k] if rating[i]>r>rating[k]])\n        \n        # for i in range(n):\n        #     for j in range(i+1,n):\n        #         for k in range(j+1,n):\n        #             if rating[i]<rating[j]<rating[k] or rating[i] > rating[j] > rating[k]:\n        #                 teams+=1\n        return teams\n", "class Solution:\n    def numTeams(self, r: List[int]) -> int:\n        ct=0\n        for i in range(len(r)):\n            for j in range(i+1, len(r)):\n                b=-1\n                if r[i]>r[j]:\n                    b = 1 # need number id dec order\n                else : b = 0 # nedd number in asc order\n                for k in range(j+1, len(r)):\n                    if b == 1 and r[k]<r[j]:\n                        #print(r[i],r[j],r[k])\n                        ct += 1\n                        continue\n                    if b == 0 and r[k]>r[j]:\n                        #print(r[i],r[j],r[k])\n                        ct += 1\n                        continue\n        return ct\n                        \n                    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n       \n        def getTeams(k, l, prev = None, asc=True):\n            if (k>len(l)):\n                return 0\n            if ((prev is None) & (k == 1)):\n                return len(l)\n            if (k == 1):\n                if asc: \n                    teamCount = sum([x>prev for x in l])\n                else:\n                    teamCount = sum([x<prev for x in l])\n            else:\n                teamCount = 0\n                larger_x = True\n                if prev is not None:\n                    larger_x = l[0]>prev\n                    \n                if asc:\n                    if (larger_x | (prev is None)):\n                    #include this one\n                        teamCount +=getTeams(k-1, l[1:], l[0], asc)\n                    #exclude this one\n                    teamCount +=getTeams(k, l[1:], prev, asc)\n                        \n                else:\n                    if (larger_x == False | (prev is None)):\n                        #print(prev, l[0], larger_x)\n                    #include this one\n                        teamCount +=getTeams(k-1, l[1:], l[0], asc)\n                    #exclude this one\n                    teamCount +=getTeams(k, l[1:], prev, asc)\n            #print('getTeams: ', k, l, prev, asc,'   result: ', teamCount)\n            return teamCount\n        \n        return getTeams(3, rating, None, True) + getTeams(3, rating, None, False)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cpt = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                if rating[j]>rating[i]:\n                    for k in range(j+1,len(rating)):\n                        if rating[k]>rating[j]:\n                            cpt += 1\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                if rating[j]<rating[i]:\n                    for k in range(j+1,len(rating)):\n                        if rating[k]<rating[j]:\n                            cpt += 1\n        return cpt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ret, n = 0, len(rating)\n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                if rating[j] > rating[i]:\n                    for k in range(j+1, n):\n                        if rating[k] > rating[j]:\n                            ret += 1\n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                if rating[j] < rating[i]:\n                    for k in range(j+1, n):\n                        if rating[k] < rating[j]:\n                            ret += 1\n        return ret", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        num_teams = 0\n        for i,r1 in enumerate(rating):\n            for j,r2 in enumerate(rating[(i+1):]):\n                for k,r3 in enumerate(rating[(i+j+1):]):\n                    num_teams += ((r1 < r2 < r3) or (r1 > r2 > r3))\n        return num_teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        incr = True\n        for i in range(len(rating)-1):\n                \n            for j in range(i+1, len(rating)):\n                if rating[i] < rating[j]: incr = True\n                elif rating[i] > rating[j]: incr = False\n                else: continue\n                    \n                for k in range(j+1, len(rating)):\n                    if ((rating[j] < rating[k] and incr) or \n                        (rating[j] > rating[k] and not incr)):\n                            count += 1\n        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        numT = 0 \n        \n        if (len(rating) < 3): \n            return 0 \n        \n        blah = list(range(len(rating)))\n        \n        for first_idx in blah[:-2]:\n            first = rating[first_idx]\n\n            for second_idx in blah[first_idx:]:\n                second = rating[second_idx]\n                \n                for third_idx in blah[second_idx:]:\n                    third = rating[third_idx]\n                    if (first < second and second < third):\n                        numT += 1\n                    if (first > second and second > third):\n                        numT += 1\n                        \n        return numT\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        team = 0\n        for i in range(len(rating)-2):\n            for j in range(i+1, len(rating)-1):\n                if rating[j] > rating[i]:\n                    for k in range(j+1, len(rating)):\n                        if rating[k] > rating[j]:\n                            team = team + 1\n                elif rating[j] < rating[i]:\n                    for k in range(j+1, len(rating)):\n                        if rating[k] < rating[j]:\n                            team = team + 1\n        return team", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n\n        p = 0 \n        res = 0 \n        while p < len(rating):\n            left_less = sum([1 for i in range(p) if rating[i] < rating[p]])\n            right_more = sum([1 for i in range(p+1, len(rating)) if rating[i] > rating[p]])\n            \n            left_more = sum([1 for i in range(p) if rating[i] > rating[p]])\n            right_less = sum([1 for i in range(p+1, len(rating)) if rating[i] < rating[p]])\n            \n            res += left_less * right_more + left_more * right_less\n            \n            p += 1\n            \n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        index = {}\n        \n        n = len(rating)\n        for i in range(n):\n            index[rating[i]] = i\n        rating.sort()\n        count = 0\n        for i in range(n):\n            for j in range(i+1,n):\n                if index[rating[i]] < index[rating[j]]:\n                    for k in range(j+1,n):\n                        if index[rating[j]] < index[rating[k]]:\n                            count += 1\n\n        rating.sort(reverse=True)\n        for i in range(n):\n            for j in range(i+1,n):\n                if index[rating[i]] < index[rating[j]]:\n                    for k in range(j+1,n):\n                        if index[rating[j]] < index[rating[k]]:\n                            count += 1\n\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i, r_i in enumerate(rating):\n            for j, r_j in enumerate(rating[i+1:], i+1):\n                for k, r_k in enumerate(rating[j+1:], j+1):\n                    count += r_i < r_j < r_k or r_i > r_j > r_k\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        return len(Solution.generate_teams(rating))\n    \n    @staticmethod\n    def generate_teams(rating):\n        teams = []\n        rating_copy = rating\n        negative_rating = [-num for num in rating]\n        \n        for rating in (rating_copy, negative_rating):\n            for i in range(len(rating)):\n                curr_first_soldier = rating[i]\n                for j in range(i + 1, len(rating)):\n                    curr_second_soldier = rating[j]\n                    if i != j and curr_second_soldier > curr_first_soldier:\n                        for k in range(j + 1, len(rating)):\n                            curr_third_soldier = rating[k]\n                            if k != j and curr_third_soldier > curr_second_soldier:\n                                teams.append((curr_first_soldier, curr_second_soldier, curr_third_soldier))\n        return teams\n    \nclass SolutionTest:\n    @staticmethod\n    def test_generate_teams():\n        test_ratings = [2, 3, 7, 1, 8, 9]\n        \n        actual_teams = [(2,3,7),(2,3,8),(2,3,9),(2,7,8),(2,7,9),(2,8,9),(3,7,8),(3,7,9),(3,8,9),(7,8,9),(1,8,9)]\n        expected_teams = Solution.generate_teams(test_ratings)\n        \n        return actual_teams == expected_teams\n    \nprint(SolutionTest.test_generate_teams())", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        import itertools\n        return len([1 for i,j,k in itertools.combinations(rating,3) if i>j>k or i<j<k])", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans = 0\n        for pt1 in range(len(rating)-2):\n            for pt2 in range(pt1+1,len(rating)-1):\n                if rating[pt1]<rating[pt2]:\n                    for pt3 in range(pt2+1,len(rating)):\n                        if rating[pt2]<rating[pt3]:\n                            ans += 1\n                if rating[pt1]>rating[pt2]:\n                    for pt3 in range(pt2+1,len(rating)):\n                        if rating[pt2]>rating[pt3]:\n                            ans += 1\n                            \n        return ans\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res=0\n        record = []\n        rec = []\n        for i in range(0,len(rating)):\n            record.append([])\n            for j in range(i+1):\n                record[i].append(None)\n            for j in range(i+1,len(rating)):\n                if rating[j] > rating[i]:\n                    record[i].append(True)\n                elif rating[j] < rating[i]:\n                    record[i].append(False)\n        for i in range(0,len(rating)-1):\n\n            for j in range(i+1,len(rating)):\n                if record[i][j]:\n                    for k in range(j+1,len(rating)):\n                        if record[j][k]:\n                            res += 1\n                            rec.append((rating[i],rating[j],rating[k]))\n                elif record[i][j]==False:\n                    for k in range(j+1,len(rating)):\n                        if record[j][k]==False:\n                            res += 1\n                            rec.append((rating[i], rating[j], rating[k]))\n        \n        return res\n", "class Solution:\n    def numTeams(self,rating):\n        return sum(x<y<z or x>y>z for x,y,z in combinations(rating,3))", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        if len(rating) > 2:\n            for i in rating[:-2]:\n                for j in rating[(rating.index(i)+1):-1]:\n                    for k in rating[(rating.index(j)+1):]:\n                        if (i < j) and (j < k):\n                            count += 1\n                        elif (i > j) and (j > k):\n                            count += 1\n                        else:\n                            count += 0\n        else:\n            count = 0\n        return count\n                    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(0, len(rating)-2):\n            if rating[i] == rating[i+1]:\n                continue\n            for j in range(i+1, len(rating)-1):\n                for k in range(j+1, len(rating)):\n                    if rating[i] > rating[j]:\n                        if rating[j] > rating[k]:\n                            cnt += 1\n                    elif rating[j] < rating[k]:\n                        cnt += 1\n        return cnt ", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(len(rating)):\n    \n            for j in range(i+1,len(rating)):\n        \n                if rating[i]<rating[j]:\n                    for k in range(j+1,len(rating)):\n                        if rating[j]<rating[k]:\n                            count+=1\n        \n                elif rating[i]>rating[j]:\n                    for k in range(j+1,len(rating)):\n                        if rating[j]>rating[k]:\n                            count+=1\n        return count ", "from itertools import combinations\nclass Solution:\n    \n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(0, len(rating)-2):\n            if rating[i] == rating[i+1]:\n                continue\n            for j in range(i+1, len(rating)-1):\n                for k in range(j+1, len(rating)):\n                    if rating[i] > rating[j]:\n                        if rating[j] > rating[k]:\n                            cnt += 1\n                    elif rating[j] < rating[k]:\n                        cnt += 1\n        return cnt              \n        \n        \n        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans = 0\n        for i in range(len(rating)):\n            a = rating[i]\n            for j in range(i+1, len(rating)):\n                b = rating[j]\n                for k in range(j+1, len(rating)):\n                    c = rating[k]\n                    if a < b < c or a > b > c:\n                        ans += 1\n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i, r_i in enumerate(rating):\n            for j, r_j in enumerate(rating[i+1:], i+1):\n                if r_i == r_j:\n                    continue\n\n                if r_i < r_j:\n                    for k, r_k in enumerate(rating[j+1:], j+1):\n                        count += r_j < r_k\n                else:\n                    for k, r_k in enumerate(rating[j+1:], j+1):\n                        count += r_j > r_k\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n\n        p = 0 \n        res = 0 \n        while p < len(rating):\n            less = sum([1 for i in range(p) if rating[i] < rating[p]])\n            more = sum([1 for i in range(p+1, len(rating)) if rating[i] > rating[p]])\n            \n            more1 = sum([1 for i in range(p) if rating[i] > rating[p]])\n            less1 = sum([1 for i in range(p+1, len(rating)) if rating[i] < rating[p]])\n            \n            res += less * more + more1 * less1\n            \n            p += 1\n            \n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = 0\n        \n        \n        #Get the first element at rating[base]\n        for base in range(len(rating)):\n            second = base + 1\n            \n            while second  < len(rating):\n                #If the second element is greater than the first, look for the third after that position\n                if rating[second] > rating[base]:\n                    third = second + 1\n                    \n                    while third < len(rating):\n                        if rating[third] > rating[second]:\n                            teams += 1\n                    \n                        #print(base, second, third, rating[base], rating[second], rating[third])\n                        third += 1\n                    \n                second += 1\n        \n        for base in range(len(rating)):\n            second = base + 1\n            \n            while second < len(rating):\n                \n                if rating[second] < rating[base]:\n                    third = second + 1\n                    \n                    while third < len(rating):\n                        if rating[third] < rating[second]:\n                            teams += 1\n                        \n                        third += 1\n                second += 1\n                \n        return teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3:\n            return 0\n        \n        \n        teams: int = 0\n        for i, s1 in enumerate(rating):\n            for j in range(i, len(rating)):\n                for k in range(j, len(rating)):\n                    s2 = rating[j]\n                    s3 = rating[k]\n                    if s1 > s2 > s3 or s1 < s2 < s3:\n                        teams += 1\n        \n        return teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        possibleTeams = []\n        temp = []\n        for i in range(len(rating)):\n            soldier_i = rating[i]\n            for j in range(i+1, len(rating)):\n                soldier_j = rating[j]\n                if soldier_j > soldier_i:\n                    increasing = True\n                else:\n                    increasing = False\n                for k in range(j+1, len(rating)):\n                    soldier_k = rating[k]\n                    if increasing and soldier_k > soldier_j:\n                        possibleTeams.append((soldier_i, soldier_j, soldier_k))\n                    if increasing == False and soldier_k < soldier_j:\n                        possibleTeams.append((soldier_k, soldier_j, soldier_i))\n        return len(possibleTeams)\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        sayac = 0\n        for i in range(len(rating)):\n            temp1 = rating[i]\n            for j in range(int(i+1),len(rating)):\n                temp2 = rating[j]\n                for k in range(int(j+1),len(rating)):\n                    temp3 = rating[k]\n\n                    if temp1 > temp2 and temp2 > temp3:\n                        sayac+=1\n                    if temp3 > temp2 and temp2 > temp1:\n                        sayac+=1    \n        return sayac\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        l = len(rating)\n        for i in range(0, l - 2):\n            for j in range(i + 1, l - 1):\n                res += len([x for x in rating[j+1:] if (rating[j] < x) == (rating[i] < rating[j])])\n\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = 0\n        if len(rating) < 3:\n            return teams\n        # rating = [2,5,3,4,1]\n        # at each index, i want to see if the following numbers have at least 2 increasing or 2 decreasing\n        for i in range(len(rating)):\n            first = rating[i]\n            for j in range(i + 1, len(rating)):\n                second = rating[j]\n                for k in range(j + 1, len(rating)):\n                    third = rating[k]\n                    if first > second and second > third:\n                        teams += 1\n                    if first < second and second < third:\n                        teams += 1\n                        \n        return teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = 0\n        \n        \n        #Get the first element at rating[base]\n        for base in range(len(rating) - 2):\n            second = base + 1\n            \n            while second  < len(rating):\n                #If the second element is greater than the first, look for the third after that position\n                if rating[second] > rating[base]:\n                    third = second + 1\n                    \n                    while third < len(rating):\n                        if rating[third] > rating[second]:\n                            teams += 1\n                    \n                        #print(base, second, third, rating[base], rating[second], rating[third])\n                        third += 1\n                    \n                second += 1\n        \n        for base in range(len(rating)):\n            second = base + 1\n            \n            while second < len(rating):\n                \n                if rating[second] < rating[base]:\n                    third = second + 1\n                    \n                    while third < len(rating):\n                        if rating[third] < rating[second]:\n                            teams += 1\n                        \n                        third += 1\n                second += 1\n                \n        return teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        combos = 0\n\n        for index, numba in enumerate(rating[:-2]):\n            for numbay in rating[index+1:-1]:\n                for numbaz in rating[rating.index(numbay)+1:]:\n                    # print(\\\"numba:\\\", numba, \\\"numbay:\\\", numbay, \\\"numbaz:\\\", numbaz)\n                    if numba < numbay and numbay < numbaz:\n                        combos += 1\n                    elif numba > numbay and numbay > numbaz:\n                        combos += 1\n\n        return combos", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        N = len(rating)\n        ans = 0\n        for i in range(N):\n            for j in range(i+1, N):\n                for k in range(j+1, N):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        ans += 1\n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for a, soldier_1 in enumerate(rating):\n            for b, soldier_2 in enumerate(rating[a+1:]):\n                for c, soldier_3 in enumerate(rating[a+b+1:]):\n                        if soldier_1 < soldier_2 < soldier_3:\n                            count += 1\n                        if soldier_1 > soldier_2 > soldier_3:\n                            count += 1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        temp_list = []\n        temp1 = 0\n        temp2 = 0\n        for i in range(0,len(rating)):\n            temp1 = rating[i]\n            for j in range(i+1,len(rating)):\n                if rating[j]>temp1:\n                    temp2 = rating[j]\n                    for k in range(j+1,len(rating)):\n                        if rating[k] > temp2:\n                            temp_list.append([temp1,temp2,rating[k]])\n            for j in range(i+1,len(rating)):\n                if rating[j]<temp1:\n                    temp2 = rating[j]\n                    for k in range(j+1,len(rating)):\n                        if rating[k] < temp2:\n                            temp_list.append([temp1,temp2,rating[k]])\n        return len(temp_list)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        resultCounter = 0\n        n = len(rating)\n        for i in range(n-2):\n            for j in range(i, n):\n                for k in range (j, n):\n                    if rating[k] < rating[j] < rating[i] or rating[k] > rating[j] > rating[i]:\n                        resultCounter +=1\n        return resultCounter\n                    \n                    \n                    \n                    \n                \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt=0\n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    if rating[i]>rating[j]>rating[k] or rating[i]<rating[j]<rating[k]:\n                        cnt += 1\n                        # print(i, j, k)\n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        n = len(rating)\n        num_valid_teams = 0\n        for i in range(n):\n            for j in range(i+1, n):\n                for k in range(j+1,n):\n                    if (rating[i]>rating[j]>rating[k]) or (rating[i]<rating[j]<rating[k]):\n                        num_valid_teams += 1\n                        \n        return num_valid_teams\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        \n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                        count += 1\n        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        counter = 0\n        while len(rating) > 0:\n            r = rating.pop(0)\n            # print(r)\n            counter += countDown(rating, r, 1)\n            counter += countUp(rating, r, 1)\n        return counter\n    \ndef countUp(rating, value, num_in_team):\n    # print(f'countUp({rating}, {value}, {num_in_team}):')\n    if len(rating) == 0:\n        return 0\n    counter = 0\n    for i, num in enumerate(rating):\n        if  num > value and num_in_team == 2:\n            # print(f'{num} is larger than {value} and tahts a team')\n            counter += 1\n        elif num > value:\n            # print(f'{num} is larger than {value}')\n            counter += countUp(rating[i:], num, 2)\n    # print(f'returning counter with value {counter}')\n    return counter\n\ndef countDown(rating, value, num_in_team):\n    # print(f'countDown({rating}, {value}, {num_in_team}):')\n    if len(rating) == 0:\n        return 0\n    counter = 0\n    for i, num in enumerate(rating):\n        if  num < value and num_in_team == 2:\n            # print(f'{num} is smaller than {value} and tahts a team')\n            counter += 1\n        elif num < value:\n            counter += countDown(rating[i:], num, 2)\n    # print(f'returning counter with value {counter}')\n    return counter\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        r = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        r += 1\n        return r", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt=0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                        cnt+=1\n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n\n        \n        elm_less_than_cur_to_left,elm_less_than_cur_to_right, elm_greater_than_cur_to_left, elm_greater_than_cur_to_right = [0]*len(rating),[0]*len(rating), [0]*len(rating), [0]*len(rating)\n        \n        \n        \n        \n        i = 0 \n        res = 0\n        \n        while i < len(rating):\n            k = i-1\n            while k >=0:\n                if rating[i] > rating[k]:\n                    elm_less_than_cur_to_left[i] += 1\n                \n                elif rating[i] < rating[k]:\n                    elm_greater_than_cur_to_left[i] += 1\n                k-=1\n            \n            k = i +1\n            while k < len(rating):\n                if rating[i] > rating[k]:\n                    elm_less_than_cur_to_right[i] += 1\n                \n                elif rating[i] < rating[k]:\n                    elm_greater_than_cur_to_right[i] += 1\n                k+=1\n            \n            i+=1\n        # print(elm_less_than_cur_to_left,elm_less_than_cur_to_right, elm_greater_than_cur_to_left, elm_greater_than_cur_to_right ) \n        for i in range(0, len(rating)):\n            res+= (elm_less_than_cur_to_left[i] * elm_greater_than_cur_to_right[i]) + (elm_less_than_cur_to_right[i] *elm_greater_than_cur_to_left[i])\n            \n        return res\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        n = len(rating)\n        for i in range(n):\n            for j in range(i + 1, n):\n                for k in range(j + 1, n):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        cnt += 1\n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        final = 0\n        n = len(rating)\n        for i in range(n):\n            for j in range(i+1,n):\n                for k in range(j+1,n):\n                    #print(rating[i] , rating[j],rating[k])\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        final += 1\n        return final", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        c=0\n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)-1):\n                for k in range(j+1,len(rating)):\n                    if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                        c+=1\n        return c", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        c=0\n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)-1):\n                for k in range(j+1,len(rating)):\n                    if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                        c+=1\n        return c", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        ct = 0\n        \n        if n<3:\n            return 0\n        \n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                for k in range(j+1, n):\n                    #print(rating[i],rating[j],rating[k])\n                    if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                        ct+=1\n        #print(out)\n        return ct", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        c = 0\n        i, j, k = (0,0,0)\n        n = len(rating)\n        \n        for i in range(0, n - 2):\n            for j in range(i + 1, n - 1):\n                for k in range(j+ 1, n):\n                    if rating[i] > rating[j]:\n                        break\n                    if rating[i] < rating[j] < rating[k]:\n                        c += 1\n        \n        for i in range(0, n - 2):\n            for j in range(i + 1, n - 1):\n                for k in range(j+ 1, n):\n                    if rating[i] < rating[j]:\n                        break\n                    if rating[i] > rating[j] > rating[k]:\n                        c += 1\n        return c\n                        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n       \n    \n        count = 0\n        \n        for i in range(0, len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        count += 1\n                        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        l = len(rating)\n        for i in range(l):\n            for j in range(i+1,l):\n                for k in range(j+1,l):\n                    if rating[i] < rating[j] < rating[k] : \n                        count += 1\n                    elif rating[i] > rating[j] > rating[k]:\n                        count += 1\n                        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        count = 0\n        for i in range(n):\n            for j in range(i+1, n):\n                for k in range(j+1, n):\n                    if(rating[i] < rating[j] < rating[k]):\n                        count += 1\n                    elif(rating[i] > rating[j] > rating[k]):\n                        count += 1\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        number_of_teams = 0\n        \n        if len(rating) < 3:\n            return 0\n        else:\n            for i in range(0, len(rating)):\n                for j in range(i + 1, len(rating)):\n                    for k in range(j + 1, len(rating)):\n                        if (rating[i] < rating[j] and rating[j] < rating[k]) or (rating[i] > rating[j] and rating[j] > rating[k]):\n                            number_of_teams += 1\n        \n        return number_of_teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        num_teams = 0\n        greater_than = [0] * n\n        less_than = [0] * n\n        for left in range(n - 1, -1, -1):\n            for right in range(left+1, n):\n                if rating[right] > rating[left]:\n                    greater_than[left] += 1\n                    num_teams += greater_than[right]\n                else:\n                    less_than[left] += 1\n                    num_teams += less_than[right]\n        return num_teams\n                    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        result = 0\n        \n        def dfs(i: int, cur_len:int, last_elem: int, flag: str):\n            nonlocal result\n            if cur_len == 3:\n                result += 1\n                return\n            \n            for x in range(i, len(rating)):\n                if flag == 'l':\n                    if rating[x] > last_elem:\n                        dfs(x + 1, cur_len + 1, rating[x], 'l')\n                elif flag == 's':\n                    if rating[x] < last_elem:\n                        dfs(x + 1, cur_len + 1, rating[x], 's')\n                        \n        for i in range(len(rating) - 1):\n            dfs(i + 1, 1, rating[i], 'l')\n            dfs(i + 1, 1, rating[i], 's')\n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        \n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    if rating[i] < rating[j] < rating[k]:\n                        count += 1\n                    elif rating[i] > rating[j] > rating[k]:\n                        count += 1\n        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        N = len(rating)\n        res = 0\n        for i in range(N):\n            for j in range(i + 1, N):\n                for k in range(j + 1, N):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        res += 1\n        \n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        combos = 0\n\n        for index, numba in enumerate(rating[:-2]):\n            for indexy, numbay in enumerate(rating[index+1:-1]):\n                for numbaz in rating[rating.index(numbay)+1:]:\n                    # print(\\\"numba:\\\", numba, \\\"numbay:\\\", numbay, \\\"numbaz:\\\", numbaz)\n                    if numba < numbay and numbay < numbaz:\n                        combos += 1\n                    elif numba > numbay and numbay > numbaz:\n                        combos += 1\n\n        return combos", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i, len(rating)):\n                for k in range(j, len(rating)):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        count += 1\n        return count\n        \n        \n        \n        \n        \n        \n        # for i in range(len(rating) - 2):\n        #     for j in range(i + 1, len(rating) -1):\n        #         for k in range(i + 2, len(rating)):\n        #             if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n        #                 count += 1\n        # return count\n    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)-1):\n                for k in range(j+1,len(rating)):\n                    if(rating[i]>rating[j] and rating[j]>rating[k]):\n                        count+=1\n                    elif(rating[i]<rating[j] and rating[j]<rating[k]):\n                        count+=1\n        return(count)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)-2):\n            for j in range(i+1, len(rating)-1):\n                for k in range(j+1, len(rating)):\n                    if(rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]):\n                        count += 1\n        return count                \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        count = 0\n        for i in range(0,n):\n            for j in range(i,n):\n                    for k in range(j,n):\n                        if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                                count += 1\n        return(count)", "class Solution:\n    def numTeams(self, rating) -> int:\n        n = len(rating)\n        ans = 0\n        for i in range(n - 2):\n            for j in range(i + 1, n - 1):\n                for k in range(j + 1, n):\n                    if rating[i] < rating[j] < rating[k]:\n                        ans += 1\n                    if rating[i] > rating[j] > rating[k]:\n                        ans += 1\n\n        return ans\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        output = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if rating[i] > rating[j] > rating[k]:\n                        output += 1\n                    if rating[i] < rating[j] < rating[k]:\n                        output += 1\n        return output", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        def helper(rating):\n            \n            resDict = {}\n            res = 0\n            for i in range(len(rating)):\n                for j in range(i, len(rating)):\n                    if rating[i] > rating[j]:\n                        resDict[i] = resDict.get(i, 0) + 1\n\n            for i in range(len(rating)):\n                for j in range(i, len(rating)):\n                    if rating[i] > rating[j]:\n                        res += resDict.get(j, 0)\n            return res\n        return helper(rating) + helper(rating[::-1])            \n        \n                \n                    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n=len(rating)\n        teams=0\n        for i in range(n):\n            for j in range(i+1,n):\n                for k in range(j+1,n):\n                    if rating[i]<rating[j]<rating[k] or rating[i] > rating[j] > rating[k]:\n                        teams+=1\n        return teams\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        c = 0\n        i, j, k = (0,0,0)\n        n = len(rating)\n        for i in range(0, n - 2):\n            for j in range(i + 1, n - 1):\n                for k in range(j+ 1, n):\n                    if rating[i] > rating[j]:\n                        break\n                    if rating[i] < rating[j] < rating[k]:\n                        c += 1\n        for i in range(0, n - 2):\n            for j in range(i + 1, n - 1):\n                for k in range(j+ 1, n):\n                    if rating[i] < rating[j]:\n                        break\n                    if rating[i] > rating[j] > rating[k]:\n                        c += 1\n        return c\n                        \n", "class Solution:\n    def numTeams(self, s: List[int]) -> int:\n        count = 0\n        if len(s) < 3:\n            return 0\n        for i in range(len(s)):\n            for j in range(i + 1, len(s)):\n                for k in range(j + 1, len(s)):\n                    if s[i] < s[j] and s[j] < s[k]:\n                        count += 1\n                    if s[i] > s[j] and s[j] > s[k]:\n                        count += 1\n        return count   ", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        total = 0\n        for i in range(n-2):\n            for j in range(i + 1, n-1):\n                for k in range(j+1, n):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        total += 1\n                    elif rating[i] > rating[j] and rating[j] > rating[k]:\n                        total += 1\n        \n        return total\n#         n = len(rating)\n#         grid = [[0 for i in range(n)] for i in range(n)]\n        \n#         for win in range(3, n+1):\n#             for start in range(n - win + 1):\n#                 end = start + win - 1\n#                 count = 0\n#                 for num in rating[start+1: start + win-1]:\n                    \n#                     if rating[start] < num and rating[end] > num:\n#                         count += 1\n#                         print(rating[start], num, rating[end])\n#                 print(count)\n#                 grid[start][end] = grid[start + 1][end] + grid[start][end-1] + count\n        \n#         ascend = grid[0][n-1]\n        \n#         for i in range(n):\n#             for j in range(n):\n#                 grid[i][j] = 0\n                \n                \n#         for win in range(3, n+1):\n#             for start in range(n - win + 1):\n#                 end = start + win - 1\n#                 count = 0\n#                 for num in rating[start+1: start + win-1]:\n                    \n#                     if rating[start] > num and rating[end] < num:\n#                         count += 1\n#                         print(rating[start], num, rating[end])\n                \n#                 grid[start][end] = grid[start + 1][end] + grid[start][end-1] + count\n        \n#         descend = grid[0][n-1]\n        \n#         print(ascend)\n#         print(descend)\n        \n#         return ascend + descend\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        new=sorted(rating)\n        if new==rating:\n            return len(rating)\n        for i in range(len(rating)-2):\n            for j in range(i+1, len(rating)-1):\n                for k in range(j+1, len(rating)):\n                    if (rating[i] < rating[j] and rating[j] < rating[k]) or (rating[i] > rating[j] and rating[j] > rating[k]):\n                        count=count+1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        length = len(rating)\n        if length < 3:\n            return 0\n        \n        for i in range(length):\n            for j in range(i+1, length):\n                for k in range(j + 1, length):\n                    if (rating[i] > rating[j] > rating[k]) or (rating[i] < rating[j] < rating[k]):\n                        count += 1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        \n        for i in range(len(rating)):\n            for j in range(i, len(rating)):\n                for k in range(j, len(rating)):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        count +=1\n                    elif rating[i] > rating[j] and rating[j] > rating[k]:\n                        count +=1\n        \n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        count = 0\n        \n        n = len(rating)\n        m = 3\n        \n        for i in range(n-2):\n            \n            for j in range(i+1, n-1):\n                \n                for k in range(j+1, n):\n                    \n                    if rating[i] < rating[j] and rating[j]<rating[k]:\n                        count += 1\n                    elif rating[i] > rating[j] and rating[j] > rating[k]:\n                        count +=1\n                    else:\n                        continue\n                        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        l = len(rating)\n        c = 0\n        for i in range(l):\n            for j in range(i+1,l):\n                for k in range(j+1,l):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        c += 1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        c += 1     \n        return c\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        lstcount=0\n        for i in range(0,len(rating)-2):              \n           for j in range(i+1,len(rating)-1):       \n             for k in range(j+1,len(rating)):\n                 if ((rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k])):\n                    lstcount=lstcount+1\n        return lstcount", "class Solution:\n    def numTeams(self, r: List[int]) -> int:\n        s=0\n        for i in range(len(r)):\n            a=0\n            b=0\n            c=0\n            d=0\n            for j in range(0,i):\n                if r[j]<r[i]:\n                    a+=1\n                else:\n                    b+=1\n            for j in range(i+1,len(r)):\n                if r[j]>r[i]:\n                    c+=1\n                else:\n                    d+=1\n            s+=a*c+b*d\n        return s\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(0, len(rating)):\n            for j in range(i + 1, len(rating)):\n                for k in range(j + 1, len(rating)):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        count += 1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        count += 1\n        return count\n        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int: \n        \n        # can use DP to speed up \n        \n        # iteraton methods\n        res = 0\n        for com in combinations(rating, 3): \n            if com[0] > com[1] > com[2] or com[0] < com[1] < com[2]: \n                res += 1 \n                \n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        l=len(rating)\n        ans=0\n        for i in range(l):\n            for j in range(i, l):\n                for k in range(j, l):\n                    if rating[i]<rating[j]<rating[k]:\n                        ans+=1\n                    if rating[i]>rating[j]>rating[k]:\n                        ans+=1\n        return ans\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        r = 0\n        for i in range(n):\n            for j in range(i,n):\n                for k in range(j,n):\n                    if rating[i] < rating[j] < rating[k]:\n                        r += 1\n                    if rating[i] > rating[j] > rating[k]:\n                        r += 1\n        return r", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        for com in combinations(rating, 3): \n            if com[0] > com[1] > com[2] or com[0] < com[1] < com[2]: \n                res += 1 \n                \n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans=0\n        for i in range(len(rating)):\n            for j in range(i,len(rating)):\n                for k in range(j,len(rating)):\n                    if (rating[i]<rating[j] and rating[j]<rating[k]) or (rating[i]>rating[j] and rating[j]>rating[k]):ans+=1\n        return ans\n", "class Solution:\n    def numTeams(self, r: List[int]) -> int:\n        # we can do this in brute force with n^3\n        \n        # trivially impossible if fewer than 3 soldiers\n        if len(r) < 3:\n            return 0\n        \n        teams = 0\n        \n        # brute force\n        \n        for i in range(0, len(r) - 2):\n            for j in range(i + 1, len(r) - 1):\n                if i >= j:\n                    break\n                for k in range(j + 1, len(r)):\n                    if j >= k:\n                        break\n                        \n                    if r[i] < r[j] < r[k] or r[i] > r[j] > r[k]:\n                        teams += 1\n        return teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i, len(rating)):\n                for k in range(j, len(rating)):\n                    if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                        #print((rating[i], rating[j],rating[k]))\n                        count += 1\n                        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j + 1, len(rating)):\n                    if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                        count+=1\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        l = [0]*len(rating)\n        m = [0]*len(rating)\n        for p,q in enumerate(rating):\n            for j in range(p):\n                if rating[j] < q:\n                    l[p] += 1\n                    res += l[j]\n                else:\n                    m[p] += 1\n                    res += m[j]\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        g, s = [0] * n, [0] * n\n        for i in range(n):\n            for j in range(i + 1, n):\n                print((rating[j]))\n                if rating[j] > rating[i]:\n                    g[i] += 1\n                else:\n                    s[i] += 1\n        print(g)\n        print(s)\n        ans = 0\n        for i in range(n):\n            for j in range(i + 1, n):\n                if rating[j] > rating[i]:\n                    ans += g[j]\n                else:\n                    ans += s[j]\n        \n        return ans\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            l = [rating[i]]\n            for j in range(i+1,len(rating)):\n                l.append(rating[j])\n                for k in range(j+1,len(rating)):\n                    if (l[0] > l[1] and l[1] > rating[k]) or (l[0] < l[1] and l[1] < rating[k]):\n                        count += 1\n                l = l[:1]\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n, cnt = len(rating), 0\n        for i in range(n):\n            for j in range(i + 1, n):\n                for k in range(j + 1, n):\n                    cnt += (rating[i] > rating[j] > rating[k]) or (rating[i] < rating[j] < rating[k])\n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        # brute force\n        length = len(rating)\n        ans = []\n\n        for i in range(length-2):\n            temp = rating[i]\n\n            for j in range(i+1, length):\n                if rating[j] > temp:\n                    for k in range(j+1, length):\n                        if rating[k] > rating[j]:\n                            ans.append([rating[i], rating[j], rating[k]])\n\n                if rating[j] < temp:\n                    for k in range(j+1, length):\n                        if rating[k] < rating[j]:\n                            ans.append([rating[i], rating[j], rating[k]])\n\n        return len(ans)\n", "import itertools\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        # mycom = [c for c in itertools.combinations(rating, 3)]\n        ans = 0\n        for item in itertools.combinations(rating, 3):\n            if item[0] < item[1] and item[1] < item[2]:\n                ans += 1\n            if item[0] > item[1] and item[1] > item[2]:\n                ans += 1\n        return ans\n", "class Solution:\n    def numTeams(self, ratings: List[int]) -> int:\n        n = len(ratings)\n        count = 0\n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                for k in range(j+1, n):\n                    if ratings[i] < ratings[j] < ratings[k]:\n                        count += 1\n                    if ratings[i] > ratings[j] > ratings[k]:\n                        count += 1\n                        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(0, len(rating)):\n            for j in range(i, len(rating)):\n                for k in range(j, len(rating)):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        count += 1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        count += 1\n        return count\n        \n", "class Solution:\n    def numTeams(self, nums: List[int]) -> int:\n        n=len(nums)\n        res = 0\n        for i in range(n):\n            for j in range(i+1,n):\n                for k in range(j+1,n):\n                    res+= 1 if (nums[i]<nums[j] and nums[j]<nums[k]) or (nums[i]>nums[j] and nums[j]>nums[k]) else 0\n        return res\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n=len(rating)\n        count=0\n        for i in range(0,n):\n                for j in range(i+1,n):\n                        for k in range(j+1,n):\n                                if rating[i]>rating[j] and rating[j]>rating[k]:\n                                        count+=1\n                                elif rating[i]<rating[j] and rating[j]<rating[k] :\n                                        count+=1\n        return count\n                \n                        \n                \n", "# Version B:  O(n^2) solution with (primitive) nested loops for building our 4 counting variables.\nclass Solution:\n    def numTeams(self, A) -> int:\n        L = len(A)\n        result = 0\n        for j in range(1,L-1):\n            x, loL, loR, hiL, hiR = A[j], 0, 0, 0, 0\n            for i in range(j):\n                if A[i]<x:\n\n                    print('x',x)\n                    print('A[i]',A[i])\n                    loL += 1\n                else:\n                    hiL += 1\n            for k in range(j+1,L):\n                if A[k]<x:\n                    loR += 1\n                else:\n                    hiR += 1\n            result += loL*hiR + hiL*loR\n            print('resut',result)\n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                if rating[i] > rating[j] or rating[i] < rating[j]:\n                    for k in range(j+1,len(rating)):\n                        if rating[i] > rating[j] and rating[j] > rating[k]:\n                            cnt += 1\n                        elif rating[i] < rating[j] and rating[j] < rating[k]:\n                            cnt += 1\n        return cnt\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if((rating[i]<rating[j]<rating[k] and i < j < k) or (rating[i]>rating[j]>rating[k] and i<j<k)):\n                        count+=1\n        return count", "class Solution:\n    def numTeams(self, a: List[int]) -> int:\n        count=0\n        for i in range(len(a)-2):\n            for j in range(i+1,len(a)-1):\n                for k in range(j+1,len(a)):\n                    if a[i]<a[j]<a[k]:\n                        count+=1\n                    if a[i]>a[j]>a[k]:\n                        count+=1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        n = len(rating)\n        team_sum = 0\n        teams = []\n        for i in range(n):\n            for j in range(i+1,n):\n                for k in range(j+1,n):\n                    if rating[i] < rating[j] < rating[k]:\n                        team_sum += 1\n                    if rating[i] > rating[j] > rating[k]:\n                        team_sum += 1\n                        \n        return team_sum", "class Solution:\n    def numTeams(self, nums: List[int]) -> int:\n        n=len(nums)\n        return sum([1 if (nums[i]<nums[j] and nums[j]<nums[k]) or (nums[i]>nums[j] and nums[j]>nums[k]) else 0 for i in range(n) for j in range(i+1,n) for k in range(j+1,n)])\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0 \n        n= len(rating)\n        for i in range(n-2):\n            for j in range(i+1,n-1):\n                for k in range(j+1,n):\n                    if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                        count +=1\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        # just use i,j,k three pointers. then it is O^3\n        # brute-force O^3 solution would be to use three index pointers\n        # spacewise, this would be just O(1)\n        \n        count = 0\n        \n        for i in range(0, len(rating)-2):\n            for j in range(i+1, len(rating)-1):\n                for k in range(j+1, len(rating)):\n                    if ((rating[i] < rating[j]) and (rating[j] < rating[k])) or ((rating[i] > rating[j]) and (rating[j] > rating[k])):\n                        count += 1\n                    \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        # just use i,j,k three pointers. then it is O^3\n        # brute-force O^3 solution would be to use three index pointers\n        # spacewise, this would be just O(1)\n        \n        count = 0\n        \n        for i in range(0, len(rating)-2):\n            for j in range(i+1, len(rating)-1):\n                for k in range(j+1, len(rating)):\n                    if ((rating[i] < rating[j]) and (rating[j] < rating[k])) or ((rating[i] > rating[j]) and (rating[j] > rating[k])):\n                        count += 1\n                    \n        return count\n        \n", "class Solution:\n    def numTeams(self, rat: List[int]) -> int:\n        if(len(rat)<3):\n            return 0\n        else:\n            c=0\n            for i in range(len(rat)):\n                for j in range(i+1,len(rat)):\n                    for k in range(j+1,len(rat)):\n                        if(rat[i]>rat[j]>rat[k]):\n                            c+=1\n            rat.reverse()                \n            for i in range(len(rat)):\n                for j in range(i+1,len(rat)):\n                    for k in range(j+1,len(rat)):\n                        if(rat[i]>rat[j]>rat[k]):\n                            c+=1                \n            return c                ", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            less_left = 0\n            less_right = 0\n            greater_left = 0\n            greater_right = 0\n            \n            for j in range(len(rating)):\n                if j < i:\n                    if rating[i] > rating[j]:\n                        less_left += 1\n                    elif rating[i] < rating[j]:\n                        greater_left += 1\n                elif j > i:\n                    if rating[i] > rating[j]:\n                        less_right += 1\n                    elif rating[i] < rating[j]:\n                        greater_right += 1\n            count += less_left * greater_right + greater_left * less_right\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for idx0 in range(len(rating)-2):\n            for idx1 in range(idx0+1, len(rating)-1):\n                for idx2 in range(idx1+1, len(rating)):\n                    #print(idx0,idx1,idx2)\n                    if rating[idx0]<rating[idx1] and rating[idx1] < rating[idx2]:\n                        count+=1\n                    elif rating[idx0]>rating[idx1] and rating[idx1] > rating[idx2]:\n                        count+=1\n        return count\n        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        cnt += 1\n                    elif rating[i] < rating[j] and rating[j] < rating[k]:\n                        cnt += 1\n        return cnt\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        c=0\n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    if i<j<k:\n                        if rating[i]>rating[j]>rating[k] or rating[i]<rating[j]<rating[k]:\n                            c+=1\n        return c\n", "from itertools import combinations\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        valid = 0\n        for i in combinations(rating, 3):\n            if (i[0] < i[1]):\n                if (i[1] < i[2]):\n                    valid += 1\n            elif (i[0] > i[1]):\n                if (i[1] > i[2]):\n                    valid += 1\n        return valid\n", "from itertools import combinations\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        valid = 0\n        for i in combinations(rating, 3):\n            if (i[0] < i[1] < i[2]) or (i[0] > i[1] > i[2]):\n                valid += 1\n        return valid\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = 0\n        for i in range(len(rating)):\n            for j in range(i, len(rating)):\n                for k in range(j, len(rating)):\n                    if((rating[i]>rating[j] and rating[j]>rating[k]) or (rating[i]<rating[j] and rating[j]<rating[k])):\n                        teams+=1\n        return teams\n", "from itertools import combinations\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        result = 0\n        for tupl in combinations(rating, 3):\n            if (tupl[0] < tupl[1] < tupl[2]) or (tupl[0] > tupl[1] > tupl[2]):\n                result += 1\n        return result\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3: \n            return 0\n        \n        ans = 0\n        \n        for i in range(0, len(rating)-2):\n            for j in range(i + 1, len(rating) - 1):\n                for k in range(j + 1, len(rating)):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        ans += 1\n                    elif rating[i] > rating[j] and rating[j] > rating[k]:\n                        ans += 1 \n        return ans\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(0, len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if rating[i]< rating[j] and rating[j] < rating[k]:\n                        count+=1 \n                    elif rating[i]> rating[j] and rating[j]>rating[k]:\n                        count+=1\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        out = 0\n        for i in range(1,n-1):\n            lless = lgreat = rless = rgreat = 0\n            for j in range(i-1,-1,-1):\n                if rating[j]>rating[i]:\n                    lgreat+=1\n                if rating[j]<rating[i]:\n                    lless+=1\n            for j in range(i+1,n):\n                if rating[j]>rating[i]:\n                    rgreat+=1\n                if rating[j]<rating[i]:\n                    rless+=1              \n            out += (lless*rgreat)+(lgreat*rless)\n        return out", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n= len(rating)\n        count  = 0\n        for i in range(n-2):\n            for j in range(i+1,n-1):\n                for k in range(j+1,n):\n                    if (rating[i]>rating[j]>rating[k]) or (rating[i]<rating[j]<rating[k]):\n                        count += 1\n                    else:\n                        k += 1\n        return count\n", "from itertools import combinations\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        return len([(x, y, z) for x, y, z in list(combinations(rating, 3)) if (x < y and y < z) or (x > y and y > z)])", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n=len(rating)\n        c=0\n        for i in range(n):\n            for j in range(i+1,n):\n                for k in range(j+1,n):\n                    if rating[i]<rating[j] and rating[j]<rating[k]:\n                        c+=1\n                    if rating[i]>rating[j] and rating[j]>rating[k]:\n                        c+=1\n        return c", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for id_i, i in enumerate(rating):\n            for id_j, j in enumerate(rating[id_i:]):\n                id_ij = id_i + id_j\n                for id_k, k in enumerate(rating[id_ij:]):\n                    id_ijk = id_ij + id_k\n                    if 0<=i and id_i < id_ij and id_ij < id_ijk:\n                        if i < j and j < k:\n                            count += 1\n                        if i > j and j > k:\n                            count += 1\n        return count", "class Solution:\n    def numTeams(self, a: List[int]) -> int:\n        list1=[]\n        for i in range(len(a)-2):\n            for j in range(i+1,len(a)-1):\n                for k in range(j+1,len(a)):\n                    if a[i]<a[j]<a[k]:\n                        list1.append((1))\n                    if a[i]>a[j]>a[k]:\n                        list1.append((1))\n        return len(list1)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        result = 0\n        n = len(rating)\n        for i in range(0,n-2):\n            for j in range(i+2,n):\n                for k in range(i+1,j):\n                    if rating[k] > rating[i] and rating[k] < rating[j]:\n                        result += 1\n                    elif rating[k] < rating[i] and rating[k] > rating[j]:\n                        result += 1\n                        \n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3:\n            return 0\n\n        teamsCount = 0\n\n        for i in range(len(rating) - 2):\n            for j in range(i + 1, len(rating) - 1):\n                for k in range(j + 1, len(rating)):\n                    if (\n                        (rating[i] > rating[j] > rating[k])\n                        or (rating[i] < rating[j] < rating[k])\n                    ):\n                        teamsCount += 1\n        \n        return teamsCount", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        c=0\n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)-1):\n                for k in range(j+1,len(rating)):\n                    if rating[i]<rating[j] and rating[j]<rating[k]:\n                        c+=1\n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)-1):\n                for k in range(j+1,len(rating)):\n                    if rating[i]>rating[j] and rating[j]>rating[k]:\n                        c+=1\n        return c\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        l = len(rating)\n        count = 0\n        \n        for i in range(0,l-2):\n            for j in range(i+1,l-1):\n                for k in range(j+1,l):\n                    if rating[i] > rating[j] and rating[j]>rating[k]:\n                        count += 1\n                    if rating[i] < rating[j] and rating[j]<rating[k]:\n                        count += 1\n        return count\n                    \n                        \n                    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        i = 0\n        j = 0\n        k = 0\n        numOfTeams = 0\n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)-1):\n                for k in range(j+1,len(rating)):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        numOfTeams += 1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        numOfTeams += 1\n        return numOfTeams\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        greater = defaultdict(int)\n        less = defaultdict(int)\n        ans = 0\n  \n        for i in range(len(rating)-1):\n            for j in range(i+1, len(rating)):\n                if rating[j] > rating[i]:\n                    greater[i] += 1\n                else:\n                    less[i] += 1\n        for i in range(len(rating)-2):\n            for j in range(i+1, len(rating)):\n                if rating[i] < rating[j]:\n                    ans += greater[j]\n                else:\n                    ans += less[j]\n        \n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        unique = []\n        \n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        unique.append((rating[i], rating[j], rating[k]))\n                        \n        \n        return len(unique)", "class Solution:\n    \n    def numTeams(self, rating: List[int]) -> int:\n        \n        return self.increaser(rating,0,0)+ self.decreaser(rating,0,pow(10,5)+1)\n    \n    \n    def increaser(self,rating,num, last):\n        \n        if num==3:\n            return 1\n        \n        if len(rating)==0:\n            return 0\n        \n        count=0\n        for i in range(len(rating)):\n            \n            if rating[i]>last:\n                count+=self.increaser(rating[i+1:],num+1,rating[i])\n                \n        return count\n    \n    def decreaser(self,rating,num, last):\n        \n        if num==3:\n            return 1\n        \n        if len(rating)==0:\n            return 0\n        \n        count=0\n        for i in range(len(rating)):\n            \n            if rating[i]<last:\n                count+=self.decreaser(rating[i+1:],num+1,rating[i])\n                \n        return count\n    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        counts = 0\n        \n        for ix in range(len(rating) - 2):\n            for iy in range(ix, len(rating) - 1):\n                for iz in range(iy, len(rating)):\n                    if rating[ix] < rating[iy] and rating[iy] < rating[iz]:\n                        counts += 1\n                    elif rating[ix] > rating[iy] and rating[iy] > rating[iz]:\n                        counts += 1\n        return counts", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i + 1):\n                for k in range(j + 1):\n                    if rating[i] < rating[j] < rating[k]:\n                        count += 1\n                    \n                    if rating[i] > rating[j] > rating[k]:\n                        count += 1\n        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        #Brute Force.\n        teams = 0\n        for i in range(0, len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    \n                    if (i<j<k) and(rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) :\n                        teams += 1\n        \n        return teams\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans = 0\n        for i in range(len(rating)-2):\n            for j in range(i + 1,len(rating)-1):\n                for k in range(j + 1,len(rating)):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        ans += 1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        ans += 1\n            \n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans = 0\n        \n        n = len(rating)\n        for i in range(n):\n            for j in range(i, n):\n                for k in range(j, n):\n                    if rating[i]<rating[j] and rating[j] <rating[k]:\n                        ans+=1\n        rating.reverse()\n        for i in range(n):\n            for j in range(i, n):\n                for k in range(j, n):\n                    if rating[i]<rating[j] and rating[j] <rating[k]:\n                        ans+=1\n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)-1):\n                for k in range(j+1,len(rating)):\n                    if rating[i]<rating[j] and rating[j]<rating[k]:\n                        count+=1\n                    if rating[i]>rating[j] and rating[j]>rating[k]:\n                        count+=1\n                        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans=0\n        \n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)-1,1):\n                for k in range(j+1,len(rating),1):\n                    if (rating[i]<rating[j]) and (rating[j]<rating[k]):\n                        ans+=1\n                    if (rating[i]>rating[j]) and (rating[j]>rating[k]):\n                        ans+=1\n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        lenn = len(rating)\n        for mid in range(1,lenn-1):\n            left_less,right_more = 0,0\n            left_more,right_less = 0,0\n            \n            for j in range(mid):\n                if rating[j]<rating[mid]:\n                    left_less+=1\n                elif rating[j]>rating[mid]:\n                    left_more+=1\n                    \n            for j in range(mid+1,lenn):\n                if rating[j]<rating[mid]:\n                    right_less+=1\n                elif rating[j]>rating[mid]:\n                    right_more+=1\n        \n            res += (left_less*right_more)+(left_more*right_less)\n        return res\n        \n        #Brute Force\n        # lenn = len(rating)\n        # res = 0\n        # for i in range(lenn):\n        #     for j in range(i+1,lenn):\n        #         for k in range(j+1,lenn):\n        #             if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n        #                 res+=1\n        # return res\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) <= 2: \n            return 0\n        n = len(rating)\n        cnt = 0\n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                for k in range(j+1, n):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        cnt += 1\n        \n        return cnt\n    \n    def numTeams1(self, rating: List[int]) -> int:\n        pass\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        if n < 3:\n            return 0\n        return sum(rating[i] < rating[j] < rating[k]\n                   or rating[i] > rating[j] > rating[k]\n                   for i in range(n-2)\n                   for j in range(i+1, n-1)\n                   for k in range(j+1, n))", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    if (rating[i] < rating[j] and rating[j] < rating[k]) or (rating[i] > rating[j] and rating[j] > rating[k]):\n                        count += 1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        # brute force attempt\n        n = len(rating)\n        ans = 0\n        \n        for i in range(n):\n            for j in range(i, n):\n                for k in range(j, n):\n                    \n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        ans += 1\n                    \n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        ans += 1\n        return ans", "class Solution:\n    def numTeams(self, a: List[int]) -> int:\n        greater,lesser=0,0\n        c=0\n        n=len(a)\n        for i in range(0,n):\n            for j in range(i+1,n):\n                if(a[i]<a[j]):\n                    greater=0\n                    lesser=1\n                elif(a[i]>a[j]):\n                    lesser=0\n                    greater=1\n                else:\n                    continue\n                for k in range(j+1,n):\n                    if(a[j]<a[k] and lesser==1):\n                        c=c+1\n                    elif(a[j]>a[k] and greater==1):\n                        c=c+1\n        return c\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        count = 0\n        \n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        count += 1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        count += 1\n        return count\n    \n", "from math import comb\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        sum = 0\n        for i in range(len(rating) -2):\n            for j in range(i, len(rating) - 1):\n                for k in range(j, len(rating)):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        sum += 1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        sum += 1\n        return sum\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0 \n        for i in range(0,len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                        res += 1\n        return res", "class Solution:\n    \n    def three(self,three,final):\n        \n        if three==True:\n            print(final)\n            \n            \n    def numTeams(self, rating: List[int]) -> int:\n        \n        i=0\n        j=0\n        k=0\n        \n        final=[]\n        \n        three=None\n        count=0\n        while i<len(rating):  #2,5,7\n            j=i+1\n            while j < len(rating):\n                k=j+1\n                while k < len(rating):\n                    if (rating[i] < rating[j] < rating[k]) or  (rating[i] > rating[j] > rating[k]):\n                        count += 1\n                    k+=1\n                j+=1\n            i+=1\n        return count\n                    \n        \n    \n    def numTeams_praj(self, rating: List[int]) -> int:\n        \n        i=0\n        final=[]\n        \n        three=None\n        count=0\n        while i<len(rating):  #2,5,7\n            final.append(rating[i])#2\n            j=i+1\n            while j<len(rating):\n                final.append(rating[j])\n                if rating[j]>rating[i]:\n                    new=rating[j+1:]\n                    for ele in new:\n                        if ele>rating[j]:\n                            final.append(ele)\n                            three=True\n                            \n                            self.three(three,final)\n                            count+=1\n                            final=[]\n                            final.append(rating[i])\n                            final.append(rating[j])\n                            \n                        else:\n                            three=False\n                            rating[j]=ele\n            \n                    \n                 \n             \n                j+=1\n            i+=1\n        return count\n        \n        \n\n                        \n                        \n                     \n               \n                \n                \n          \n          \n        \n        \n            \n", "from itertools import cycle, islice\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        # solution 1\n        count = 0\n        for id_i, i in enumerate(rating):\n            for id_j, j in enumerate(rating[id_i:]):\n                id_ij = id_i + id_j\n                for id_k, k in enumerate(rating[id_ij:]):\n                    id_ijk = id_ij + id_k\n                    if 0<=i:\n                        if i < j and j < k:\n                            count += 1\n                        if i > j and j > k:\n                            count += 1\n        return count\n    \n    \n        # solution 2 - itertools-islice\n        # count = 0\n        # print(list(islice(rating, 0)))\n        # return count\n    \n        # solution 3 - numpy\n        # count = 0\n        # arr = rating\n        # for i in range(arr.shape[0]):\n        #     ij = i+j\n        #     for j in range(arr[i:]):\n        #         for k in range(arr[ij:]):\n        #             ijk = ij + k\n        #             if arr[i] > 0\n            \n        #return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        \n        if n < 3:\n            return 0\n        \n        greater = [0] * n\n        smaller = [0] * n\n        for i in range(len(rating)-1):\n            for j in range(i+1, len(rating), 1):\n                if rating[j] > rating[i]:\n                    greater[j] += 1\n                elif rating[j] < rating[i]:\n                    smaller[j] += 1\n\n        total = 0\n        for j in range(1, len(rating), 1):\n            for k in range(j+1, len(rating), 1):\n                if rating[k] > rating[j]:\n                    total += greater[j]\n                elif rating[k] < rating[j]:\n                    total += smaller[j]\n\n        return total", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        if n < 3:\n            return 0\n        return sum((rating[j] - rating[i]) * (rating[k] - rating[j]) > 0\n                   for i in range(n-2)\n                   for j in range(i+1, n-1)\n                   for k in range(j+1, n))", "class Solution:\n    \n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        @lru_cache(maxsize = None)\n        def check(a,b):\n            if rating[a]>rating[b]:\n                return 1\n            elif rating[a]<rating[b]:\n                return 2\n            else:\n                return 0\n        for ind,val in enumerate(rating[2:]):\n            ind+=2\n            for a in range(ind-1):\n                for b in range(a+1,ind):\n                    c = check(a,b)\n                    if c == 1 and val < rating[b] or c==2 and val > rating[b]:\n                        res +=1\n        return res\n", "class Solution:\n    def my_function(self, start: int, end: int) -> int:\n        if end - start <= 1:\n            return 0\n        s = sorted(range(end-start), key=lambda x: self.rating[start + x])\n        return abs(s.index(0) - s.index(end-start-1)) - 1\n        \n    def numTeams(self, rating: List[int]) -> int:\n        self.rating = rating\n        total = 0\n        l = len(rating)\n        for i in range(l):\n            for j in range(l-i-1):\n                a = self.my_function(i, l-j)\n                total += a\n                \n        return total", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        #find all ascending and descending combos in order of length three\n        #o(n^3) solution\n        count = 0\n        i = 0\n        while i < len(rating):\n            j = i + 1\n            while j < len(rating):\n                k = j + 1\n                while k < len(rating):\n                    if (rating[i] > rating[j] > rating[k]) or (rating[i] < rating[j] < rating[k]):\n                        count += 1\n                    k += 1\n                j += 1\n            i += 1\n        return count\n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        def isValid(a,b,c):\n            return (a < b < c) or (a > b > c)\n        \n        count = 0\n        for i in range(len(rating)-2):\n            for j in range(i, len(rating)-1):\n                for k in range(j, len(rating)):\n                    if isValid(rating[i], rating[j], rating[k]):\n                        count += 1\n        return count\n                        \n                    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        comb = list(itertools.combinations(rating, r=3))\n        \n        ans = 0\n        for tup in comb:\n            if tup[0] < tup[1] < tup[2] or tup[0] > tup[1] > tup[2]:\n                ans+=1\n                \n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        from itertools import combinations\n        com = list(combinations(rating,3))\n\n        cnt = 0\n        if len(rating) < 2:\n          return 0\n        for t in com:\n          if t[0] > t[1] > t[2] or t[0] < t[1] < t[2]:\n            cnt += 1\n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        i=0\n        final=[]\n        add=0\n        while i<len(rating):\n            \n            j=len(rating)-1\n            while j>i+1:\n                # print(rating[i],rating[j])\n                k=j-1\n                while k>i:\n                    # print(rating[i],rating[k],rating[j])\n                    if rating[i]<rating[k] and rating[k]<rating[j]:\n                        add+=1\n                    if rating[i]>rating[k] and rating[k]>rating[j]:\n                        add+=1\n                    k-=1\n            \n                j-=1\n            i+=1\n        return add\n#             j=i+1\n#             k=len(rating)-1\n            \n#             while j<k:\n#                 print(rating[i],rating[j],rating[k])\n                \n#                 if rating[i]<rating[j] and rating[j]<rating[k]:\n#                     final.append((rating[i],rating[j],rating[k]))\n                    \n#                 elif rating[i]>rating[j] and rating[j]>rating[k]:\n#                     final.append((rating[i],rating[j],rating[k]))\n                \n#                 j+=1\n                \n#                 print(rating[i],rating[j],rating[k])\n#                 print()\n#             i+=1\n#         print(final)\n", "from itertools import combinations\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = list(combinations(rating,3))\n        count = 0\n        for i in res :\n            if i[0] > i[1] > i[2] or i[0] < i[1] < i[2] :\n                count +=1\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        if len(rating) < 3:\n            return 0\n        \n        from functools import lru_cache\n        @lru_cache(maxsize = None)\n        def bt(start, lastVal, countAdded, sign):\n            if countAdded == 3:\n                return 1\n            \n            if start == len(rating):\n                return 0\n            \n            totalCount = 0\n            for i in range(start, len(rating)):\n                if lastVal*sign < rating[i]*sign:\n                    totalCount += bt(i + 1, rating[i], countAdded + 1, sign)\n            return totalCount\n        \n        return bt(0, 0, 0, 1) + bt(0, float('inf'), 0, -1)\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        i = 0\n        j = 1\n        k = 2\n        count = 0\n        while i < len(rating)-2:\n            while j < len(rating)-1:\n                while k < len(rating):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        count += 1\n                    k += 1\n                j += 1\n                k = j + 1\n            i += 1\n            j = i + 1\n            k = i + 2\n        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        track = []\n        res = 0\n        def backtrack(increase, index):\n            nonlocal res\n            if len(track) == 3:\n                \n                res +=1\n                return\n            for i in range(index, len(rating)):\n                if index == 0:\n                    track.append(rating[i])\n                    backtrack(increase, i+1)\n                    track.pop()\n                else:\n                    if increase:\n                        if rating[i] > track[-1]:\n                            track.append(rating[i])\n                            backtrack(True, i+1)\n                            track.pop()\n                    if not increase:\n                        if rating[i] < track[-1]:\n                            track.append(rating[i])\n                            backtrack(False, i+1)\n                            track.pop()\n        backtrack(True, 0)\n        backtrack(False, 0)\n        return res\n", "class Solution:\n    \n    def checkList(self, head: List[List[int]], tail: List[int], init: int) -> int:\n        result = init\n        head_copy = head[:]\n        num = tail[0]\n        \n        for i in head_copy:\n            if len(i) == 2:\n                if (i[0] < i[1] and i[1] < num) or (i[0] > i[1] and i[1] > num):\n                    result+=1   \n            else:\n                head.append(i + [num])    \n\n        if len(tail) == 1: \n            return result\n        else:\n            return self.checkList(head, tail[1:], result)\n        \n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3: \n           return 0 \n        return self.checkList([[],[rating[0]]], rating[1:], 0)   ", "class Solution:\n    \n    def checkList(self, head: List[List[int]], tail: List[int], init: int) -> int:\n        result = init\n        head_copy = list(head)\n        num = tail[0]\n        \n        for i in head_copy:\n            if len(i) == 2:\n                if (i[0] < i[1] and i[1] < num) or (i[0] > i[1] and i[1] > num):\n                    result+=1   \n            else:\n                head.append(i + [num])    \n\n        if len(tail) == 1: \n            return result\n        else:\n            return self.checkList(head, tail[1:], result)\n        \n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3: \n           return 0 \n        return self.checkList([[],[rating[0]]], rating[1:], 0)   ", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        return sum( 1 for k in range(len(rating)) for j in range(k) for i in range(j) if (rating[i]<rating[j]<rating[k]) or (rating[i]>rating[j]>rating[k]))\n    \n       \n                    \n        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                k = j+1\n                while(k<len(rating)):\n                    if (rating[k]>rating[j]) and (rating[j]>rating[i]):\n                        count = count+1\n                    elif(rating[k]<rating[j]) and (rating[j]<rating[i]):\n                        count = count+1\n                    k = k+1\n            \n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        i = 0\n        j = 1\n        k = 2\n        count = 0\n        while i < len(rating)-2:\n            while j < len(rating)-1:\n                while k < len(rating):\n                    if rating[i] < rating[j] < rating[k]:\n                        count += 1\n                    elif rating[i] > rating[j] > rating[k]:\n                        count += 1\n                    k += 1\n                j += 1\n                k = j + 1\n            i += 1\n            j = i + 1\n            k = i + 2\n        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3:\n            return 0\n        else:\n            res = 0\n            for i in range(len(rating)-2):\n                for j in range(i, len(rating)-1):\n                    for k in range(j, len(rating)):\n                        if(rating[i]>rating[j] and rating[j]>rating[k]):\n                            res += 1\n                        elif(rating[i]<rating[j] and rating[j]<rating[k]):\n                            res += 1\n            return res\n                            \n                \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        table = [[1 if rating[i] > rating[j]  else 0  for j in range(i+1,len(rating))] for i in range(0,len(rating)-1)]\n        table = table + [[]]\n        count = 0\n        \n        for i in range(0,len(table)):\n            for j in range(0,len(table[i])):\n                if table[i][j] == 1:\n                    count += sum(table[i+1+j])\n                else:\n                    count += (len(table[i+1+j])-sum(table[i+1+j]))\n                    \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if (rating[i] > rating[j] and rating[j] > rating[k]) or (rating[i] < rating[j] and rating[j] < rating[k]):\n                        count += 1\n        return count", "class Solution:\n    def numTeams(self, a: List[int]) -> int:\n        output = 0\n        for i in range(len(a[:-2])):\n            for j in range(len(a[:-1])):\n                if a[j]>a[i] and j>i:\n                    for k in range(len(a)):\n                        if a[k]>a[j] and k>j:\n                            output +=1\n                elif a[j]<a[i] and j>i:\n                    for k in range(len(a)):\n                        if a[k]<a[j] and k>j:\n                            output +=1\n        return output\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        result = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                        result+=1\n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n      res = []\n      for i in range(len(rating)):\n        for j in range(i, len(rating)):\n          for k in range(j, len(rating)):\n            if(rating[i] > rating[j] and rating[j] > rating[k]):\n              res.append((rating[i], rating[j], rating[k]))\n            if(rating[i] < rating[j] and rating[j] < rating[k]):\n              res.append((rating[i], rating[j], rating[k]))\n      return len(res)\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            l = [rating[i]]\n            for j in range(i+1,len(rating)):\n                l.append(rating[j])\n                for k in range(j+1,len(rating)):\n                    if (l[0] > l[1] and l[1] > rating[k]) or (l[0] < l[1] and l[1] < rating[k]):\n                        count += 1\n                    \n                l = l[:1]\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        \n        n = len(rating)\n        \n        for i in range(n):\n            for j in range(i+1,n):\n                for k in range(j+1,n):\n                    if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                        count += 1\n        \n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        ratings = rating\n        for i in range(len(ratings)):\n            for j in range(i + 1, len(ratings)):\n                for k in range(j + 1, len(ratings)):\n                    if ratings[i] > ratings[j] > ratings[k]:\n                        count += 1\n                    elif ratings[i] < ratings[j] < ratings[k]:\n                        count += 1\n        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        combs = combinations(rating,3)\n        return sum((c[0] < c[1] < c[2]) or (c[0] > c[1] > c[2]) for c in combs)\n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        a = rating\n        n = len(a)\n        ans = 0\n        for i in range(n):\n            left = [0, 0]\n            for j in range(i):\n                if a[j] < a[i]:\n                    left[0] += 1\n                elif a[j] > a[i]:\n                    left[1] += 1\n            right = [0, 0]\n            for j in range(i + 1, n):\n                if a[j] < a[i]:\n                    right[0] += 1\n                elif a[j] > a[i]:\n                    right[1] += 1\n            ans += left[0] * right[1] + left[1] * right[0]\n        return ans", "class Solution:\n    def numTeams(self, r: List[int]) -> int:\n        c=0\n        for i in range(len(r)-2):\n            for j in range(i+1,len(r)-1):\n                for k in range(j+1,len(r)):\n                    if(r[i]>r[j]>r[k]) or (r[i]<r[j]<r[k]):\n                        c+=1\n        return c\n                        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i,len(rating)):\n                for k in range(j,len(rating)):\n                    if rating[i]<rating[j]<rating[k]:\n                        count += 1\n                    if rating[i]>rating[j]>rating[k]:\n                        count += 1\n        return count\n", "from itertools import combinations\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        return len([comb for comb in list(combinations(rating, 3)) if (comb[0]<comb[1] and comb[1]<comb[2]) or (comb[0]>comb[1] and comb[1]>comb[2])])", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        out = []\n        \n        if n<3:\n            return 0\n        \n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                for k in range(j+1, n):\n                    #print(rating[i],rating[j],rating[k])\n                    if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                        out.append([i,j,k])\n        #print(out)\n        return len(out)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        count = 0\n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                for k in range(j+1, n):\n                     if rating[i] > rating[j] > rating[k] or rating[i] < rating[j] < rating[k]:\n                            count += 1\n                            \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3:\n            return 0\n        \n        \n        teams: Set[Tuple[int, int, int]] = set()\n        for i, s1 in enumerate(rating):\n            for j in range(i, len(rating)):\n                for k in range(j, len(rating)):\n                    s2 = rating[j]\n                    s3 = rating[k]\n                    if s1 > s2 > s3 or s1 < s2 < s3:\n                        teams.add((s1, s2, s3))\n        \n        return len(teams)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans = 0\n        N = len(rating)\n        for i in range(N):\n            for j in range(i + 1, N):\n                for k in range(j + 1, N):\n                    ans += int(rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k])\n        \n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i, len(rating)):\n                for k in range(j, len(rating)):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        count += 1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        teams_count = 0\n        \n        i = 0\n        \n        while i < len(rating)-2:\n            j = i+1\n            while j < len(rating) -1:\n                k = j+1\n                while k < len(rating):\n                    if ((rating[i] < rating[j]) and (rating[j] < rating[k])) or ((rating[k] < rating[j]) and (rating[j] < rating[i])):\n                        teams_count +=1\n                 \n                    k+=1\n                j+=1\n            i+= 1\n            \n        return teams_count\n            \n                    \n                \n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        def get_triplets(r):\n            res = 0\n            for i in range(n):\n                for j in range(i + 1, n):\n                    for k in range(j + 1, n):\n                        res += 1 if (r[i] < r[j] < r[k]) else 0\n            return res\n\n        return get_triplets(rating) + get_triplets(rating[::-1])", "# class Solution:\n#     def numTeams(self, rating: List[int]) -> int:\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", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n      N = len(rating)\n      numteams = 0\n      \n      # strictly increasing\n      for i in range(N):\n        left = 0\n        right = 0\n        \n        for j in range(N):\n          if(j < i):\n            if(rating[j] < rating[i]):\n              left += 1\n          elif(j > i):\n            if(rating[i] < rating[j]):\n              right += 1\n        numteams += (left*right)\n        \n      # strictly decreasing\n      for i in range(N):\n        left = 0\n        right = 0\n        \n        for j in range(N):\n          if(j < i):\n            if(rating[i] < rating[j]):\n              left += 1\n          elif(j > i):\n            if(rating[i] > rating[j]):\n              right += 1\n        numteams += (left*right)\n    \n      return numteams\n        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        lens=len(rating)\n        count=0\n        for i in range(lens-1):\n            for j in range(i+1,lens-1):\n                for k in range(j+1,lens):\n                    if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                        count+=1\n                    \n                \n        return count", "from itertools import combinations\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) in rating and len(rating) == 3:\n            return 0\n        else:\n            output = list(combinations(rating,3))\n            count = len(output)\n            for i in output:\n                if i[0]<i[1] and i[1]>i[2]:\n                    count -= 1\n                elif i[0]>i[1] and i[1]<i[2]:\n                    count -= 1\n            return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        teams = []\n        \n        for i in range(0, len(rating)): \n            \n            for j in range(i + 1, len(rating)):\n    \n                for k in range(j + 1, len(rating)):\n            \n                    if (rating[i] < rating[j] and rating[j] < rating[k]) or (rating[i] > rating[j] and rating[j] > rating[k]):\n                    \n                        teams.append([i, j, k])\n        \n        return len(teams)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3:\n            return 0\n        l = len(rating)\n        count = 0\n        \n        for i in range(l - 2):\n            for j in range(i + 1, l-1):\n                for k in range(j + 1, l):\n                    if rating[k] > rating[j] > rating[i] or rating[k] < rating[j] < rating[i]:\n                        count += 1\n                        \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        result = 0\n        for i in range(len(rating)-2):\n            for j in range(i, len(rating)-1):\n                for k in range(j, len(rating)):\n                    if rating[i] > rating[j] > rating[k]:\n                        result += 1\n                    if rating[i] < rating[j] < rating[k]:\n                        result += 1\n        return result\n                    \n                    \n                    \n#         up_list = []\n#         down_list = []\n#         result = 0\n#         for i in range(len(rating)):\n#             up_list.append([i])\n#             down_list.append([i])\n#             for sub_list in up_list:\n#                 if len(sub_list) == 1 and sub_list[0] < rating[i]:\n#                     new_list = sub_list + [rating[i]]\n#                     up_list.append(new_list)\n#                 elif len(sub_list) == 2 and sub_list[-1] < rating[i]:\n#                     up_list.append([sub_list[0], rating[i]])\n#                     up_list.append([sub_list[1], rating[i]])\n#                     result += 1\n#                 elif len(sub_list) == 2 and sub_list[0] < rating[i]:\n#                     up_list.append([sub_list[0], rating[i]])\n#                     up_list.append([rating[i], sub_list[1]])\n            \n#             for sub_list in down_list:\n#                 if len(down_list) == 1 and down_list[0] > rating[i]:\n#                     new_list = sub_list + [rating[i]]\n#                     down_list.append(new_list)\n#                 elif len(sub_list) == 2 and sub_list[-1] > rating[i]:\n#                     down_list.append([sub_list[0], rating[i]])\n#                     down_list.append([sub_list[1], rating[i]])\n#                     result += 1\n#                 elif len(sub_list) == 2 and sub_list[0] > rating[i]:\n#                     down_list.append([sub_list[0], rating[i]])\n#                     down_list.append([rating[i], sub_list[1]])\n#         return result\n                    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = []\n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1,len(rating)):\n                    if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                        teams.append([i,j,k])\n        return len(teams)", "class Solution:\n    def canFormATeam(self, r1: int, r2: int, r3: int) -> bool:\n        return (r1 < r2 < r3) or (r1 > r2 > r3)\n\n\n    def numTeams(self, rating: List[int]) -> int:\n        n_teams: int = 0\n\n        for i in range(len(rating) - 2):\n            for j in range(i + 1, len(rating) - 1):\n                for k in range(j + 1, len(rating)):\n                    if self.canFormATeam(rating[i], rating[j], rating[k]):\n                        n_teams += 1\n                        \n        return n_teams\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        answer = 0\n        n = len(rating)\n\n        for i in range(n):\n            for j in range(n):\n                if j < i:\n                    continue\n                for k in range(n):\n                    if j < k:\n                        if rating[i] < rating[j] < rating[k]:\n                            answer += 1\n                        elif rating[i] > rating[j] > rating[k]:\n                            answer += 1\n\n        return answer", "class Solution:\n    \n    def numTeams(self, rating: List[int]) -> int:\n        arr = []\n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)-1):\n                for k in range(j+1,len(rating)):\n                    if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                        arr.append([rating[i] , rating[j] , rating[k]])\n        if arr:\n            return len(arr)\n        return 0\n                    \n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = range(1, len(rating) + 1)\n        teams = list(zip(teams, rating))\n        teams_inc = sorted(teams, key=lambda x: x[1])\n        teams_dec = sorted(teams, key=lambda x: -x[1])\n        ans = 0\n        for i in range(0, len(teams) - 2):\n            for j in range(i + 1, len(teams) - 1):\n                for k in range(j + 1, len(teams)):\n                    if teams_inc[i][0] < teams_inc[j][0] < teams_inc[k][0]:\n                        ans += 1\n        for i in range(0, len(teams) - 2):\n            for j in range(i + 1, len(teams) - 1):\n                for k in range(j + 1, len(teams)):\n                    if teams_dec[i][0] < teams_dec[j][0] < teams_dec[k][0]:\n                        ans += 1\n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        less_r = [0] * n\n        bigger_r = [0] * n\n        less_l = [0] * n\n        bigger_l = [0] * n\n        for i in range(n):\n            for j in range(i):\n                less_l[i] += rating[i] > rating[j]\n                bigger_l[i] += rating[i] < rating[j]\n            for j in range(i + 1, n):\n                less_r[i] += rating[i] > rating[j]\n                bigger_r[i] += rating[i] < rating[j]\n        ans = 0\n        for i in range(n):\n            ans += less_l[i] * bigger_r[i] + less_r[i] * bigger_l[i]\n        return ans\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res=0\n        for i in range(0,len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if ((rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k])):\n                        res+=1\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        stack, ans = [], 0\n        for r in rating:\n            new = [[r]]\n            for team in stack:\n                if len(team) == 1:\n                    new.append(team + [r])\n                elif len(team) == 2:\n                    if team[0] < team[1] < r or team[0] > team[1] > r:\n                        ans += 1\n            stack.extend(new)\n        return ans\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res= 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if rating[j] > rating[i] and rating[k] > rating[j]:\n                        res+=1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        res+=1\n        return res\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        size = len(rating)\n        count = 0\n        for i in range(size):\n            for j in range(i+1,size):\n                for k in range(j+1,size):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        count += 1\n        return count", "import itertools\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        mycom = [c for c in itertools.combinations(rating, 3)]\n        ans = 0\n        for item in mycom:\n            if item[0] < item[1] and item[1] < item[2]:\n                ans += 1\n            if item[0] > item[1] and item[1] > item[2]:\n                ans += 1\n        return ans\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1, len(rating)):\n                    if (rating[k]<rating[j])&(rating[j]<rating[i]):\n                        count+=1\n                    elif (rating[k]>rating[j])&(rating[j]>rating[i]):\n                        count+=1\n        return count", "from itertools import combinations\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        c=0\n        k=list(combinations(rating,3))\n        for i in k:\n            if( i[0]<i[1] and i[1]<i[2]) or (i[0]>i[1] and i[1]>i[2]):\n                c+=1\n        return c", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        size = len(rating)\n        count = 0\n        for i in range(size):\n            for j in range(i + 1, size):\n                for k in range(j + 1, size):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        count += 1\n                    elif rating[i] > rating[j] and rating[j] > rating[k]:\n                        count += 1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        n=len(rating)\n        \n        if n<3:\n            return 0\n        \n        teams=0\n        for x in range(n):\n            for y in range(x+1,n):\n                for z in range(y+1,n):\n                    a,b,c=rating[x],rating[y],rating[z]\n                    if (a>b>c) or (a<b<c):\n                        teams+=1\n        \n        return teams\n", "class Solution:\n    \n    def checkList(self, head: List[List[int]], tail: List[int], init: int) -> int:\n        result = init\n        head_copy = list(head)\n        for i in head_copy:\n            num = tail[0]\n            ilen = len(i)\n            if ilen == 2:\n                if (i[0] < i[1] and i[1] < num) or (i[0] > i[1] and i[1] > num):\n                    result+=1   \n            else:\n                head.append(i + [num])    \n\n        if len(tail) == 1: \n            return result\n        else:\n            return self.checkList(head, tail[1:], result)\n        \n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3: \n           return 0 \n        return self.checkList([[],[rating[0]]], rating[1:], 0)   ", "class Solution:\n    \n    def createAscGraph(self, ratings):\n        graph = {}\n        for index, rating in enumerate(ratings):\n            graph[rating] = []\n            for rate in ratings[index + 1:]:\n                if rate > rating:\n                    graph[rating] += [rate]\n        return graph\n        \n    def createDescGraph(self, ratings):\n        graph = {}\n        for index, rating in enumerate(ratings):\n            graph[rating] = []\n            for rate in ratings[index + 1:]:\n                if rate < rating:\n                    graph[rating] += [rate]\n        return graph\n    \n    def count_sequences(self, graph):\n        counter = 0\n        for key in graph.keys():\n            for item in graph[key]:\n                for item in graph[item]:\n                    counter += 1\n                    \n        return counter\n    \n    def numTeams(self, rating: List[int]) -> int:\n        asc_graph = self.createAscGraph(rating)\n        desc_graph = self.createDescGraph(rating)\n        \n        return self.count_sequences(asc_graph) + self.count_sequences(desc_graph)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        return self.dfs1(rating, []) + self.dfs2(rating, [])\n\n    def dfs1(self, array, path):\n        #print(array, path)\n        if len(path) == 3:\n            return 1\n        val = 0\n        for i in range(len(array)):\n            if not path or path[-1] > array[i]:\n                val += self.dfs1(array[i + 1:], path + [array[i]])\n        return val\n\n    def dfs2(self, array, path):\n        #print(array, path)\n        if len(path) == 3:\n            return 1\n        val = 0\n        for i in range(len(array)):\n            if not path or path[-1] < array[i]:\n                val += self.dfs2(array[i + 1:], path + [array[i]])\n        return val", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(0, len(rating)):\n            for j in range(i + 1, len(rating)):\n                for k in range(j + 1, len(rating)):\n                    curr = (rating[i], rating[j], rating[k])\n                    if (rating[i] < rating[j] and rating[j] < rating[k]) or (rating[i] > rating[j] and rating[j] > rating[k]): cnt += 1                \n        return cnt\n                        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        length = len(rating); numberOfTeams = 0\n        for i in range(0, length):\n            for j in range(0, length):\n                if i < j:\n                    for k in range(0, length):\n                        if j < k:\n                            if rating[i] < rating[j] and rating[j] < rating[k]:\n                                numberOfTeams = numberOfTeams + 1\n                            elif rating[i] > rating[j] and rating[j] > rating[k]:\n                                numberOfTeams = numberOfTeams + 1\n        return numberOfTeams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(len(rating) - 2):\n            for j in range(i, len(rating) - 1):\n                for k in range(j, len(rating)):\n                    if (rating[i] > rating[j]) & (rating[j] > rating[k]):\n                        cnt += 1\n                    elif (rating[i] < rating[j]) & (rating[j] < rating[k]):\n                        cnt += 1\n                    # print([i, j, k], [rating[i], rating[j], rating[k]], cnt)\n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3:\n            return 0\n        count = 0\n        for i in range(len(rating)-2):\n            for j in range(i+1, len(rating)-1):\n                for k in range(j+1, len(rating)):\n                    if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                        count += 1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        length = len(rating)\n        for i in range(length - 2):\n            for j in range(i, length - 1):\n                for k in range(j, length):\n                    if rating[i] > rating[j] > rating[k]:\n                        cnt += 1\n                    if rating[i] < rating[j] < rating[k]:\n                        cnt += 1\n        return cnt\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        self.ans = 0\n        def traverse(idx, arr, direction):\n            if len(arr) == 3:\n                self.ans +=1\n                return\n            for j in range(idx, len(rating) - (2-len(arr))):\n                \n                if direction == 'up' and rating[j] > arr[-1]:\n                    traverse(j+1, arr.copy() + [rating[j]], direction)\n                if direction == 'down' and rating[j] < arr[-1]:\n                    traverse(j+1, arr.copy() + [rating[j]], direction)\n\n        \n        for i in range(0, len(rating) -2):\n            traverse(i+1, [rating[i]], 'up')\n            traverse(i+1, [rating[i]], 'down')\n            \n        return self.ans\n            \n        \n", "from itertools import combinations\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for c in combinations([i for i in range(len(rating))],3):\n            i,j,k = c[0],c[1],c[2]\n            if rating[i] < rating[j] and rating[j] < rating[k]:\n                cnt +=1\n            if rating[i] > rating[j] and rating[j] > rating[k]:\n                cnt +=1\n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        team_cand_a = [[] for _ in range(n)]\n        a_count = 0\n        team_cand_d = [[] for _ in range(n)]\n        d_count = 0\n        for i in range(n-1):\n            for j in range(i+1, n):\n                if rating[j] > rating[i]:\n                    team_cand_a[i].append([i,j])\n                elif rating[j] < rating[i]:\n                    team_cand_d[i].append([i,j])\n        print(team_cand_a)\n        print(team_cand_d)\n        for ca in team_cand_a:\n            for ij in ca:\n                a_count += len(team_cand_a[ij[-1]])\n        for cd in team_cand_d:\n            for ij in cd:\n                d_count += len(team_cand_d[ij[-1]])\n        \n        return a_count + d_count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        pairs = []\n        if len(rating) == 1 or len(rating) == 2:\n            return 0\n        else:\n            for i in range(len(rating)):\n                for j in range(i+1,len(rating)):\n                    for k in range(j+1,len(rating)):\n                        if rating[i] > rating[j] > rating[k]:\n                            pairs.append([rating[i],rating[j],rating[k]])\n                        elif rating[i] < rating[j] < rating[k]:\n                            pairs.append([rating[i],rating[j],rating[k]])\n            \n            return len(pairs)\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        output = 0\n        for item in itertools.combinations([x for x in range(len(rating))],3):\n            if rating[item[0]] < rating[item[1]] < rating[item[2]] or rating[item[0]] > rating[item[1]] > rating[item[2]]:\n                output += 1\n        return output", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3:\n            return 0\n        teams = set()\n        for i in range(0, len(rating) - 2):\n            for j in range(i + 1, len(rating) -1):\n                for k in range(j + 1, len(rating)):\n                    if (rating[i] < rating[j] < rating[k]):\n                        teams.add((i, j, k))\n                    if (rating[i] > rating[j] > rating[k]):\n                        teams.add((i, j, k))\n                        \n        return len(teams)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        output = 0\n        for item in itertools.combinations([x for x in range(len(rating))],3):\n            #print(item)\n            if rating[item[0]] < rating[item[1]] < rating[item[2]] or rating[item[0]] > rating[item[1]] > rating[item[2]]:\n                output += 1\n        return output        ", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans = []\n        n = len(rating)\n        for i in range(n):\n            for j in range(i,n):\n                for k in range(j,n):\n                    if rating[i] < rating[j]:\n                        if rating[j] < rating[k]:\n                            ans.append([rating[i], rating[j], rating[k]])\n                    if rating[i] > rating[j]:\n                        if rating[j] > rating[k]:\n                            ans.append([rating[i], rating[j], rating[k]])\n\n        return len(ans)", "from itertools import combinations\nclass Solution:\n    \n    def numTeams(self, rating: List[int]) -> int:\n        ### Store the index in dictionary \n        from itertools import combinations\n        output = 0\n        for item in combinations([x for x in range(len(rating))],3):\n            if rating[item[0]] < rating[item[1]] < rating[item[2]] or rating[item[0]] > rating[item[1]] > rating[item[2]]:\n                output += 1\n        return output\n#         dic = {}\n#         for i, x in enumerate(rating):\n#             dic[x] = i\n#         rating.sort()\n#         cnt = 0\n#         combinations = list(combinations(rating,3))\n\n#         for comb in combinations:\n#             if (dic[comb[0]] < dic[comb[1]] and dic[comb[1]] < dic[comb[2]]) or (dic[comb[0]] > dic[comb[1]] and dic[comb[1]] > dic[comb[2]]):\n                \n#                 cnt += 1\n#         return cnt\n    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        count = [0]\n        def generate(team,pos,method):\n            if len(team)==3:\n                count[0]+=1\n                return\n            else:\n                for i in range(pos,n):\n                    if team!=[]:\n                        if method=='i':\n                            if rating[i]>team[-1]:\n                                generate(team+[rating[i]],i+1,method)\n                            else:\n                                continue\n                        elif method=='d':\n                            if rating[i]<team[-1]:\n                                generate(team+[rating[i]],i+1,method)\n                            else:\n                                continue\n                    if team==[]:\n                        generate(team+[rating[i]],i+1,method)\n        generate([],0,'i')\n        generate([],0,'d')\n        return count[0]", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                        ans+=1\n        return ans\n            \n", "import itertools\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        candidates = list(combinations(rating, 3))\n        return sum([candidate[0] <= candidate[1] <= candidate[2] or candidate[0] >= candidate[1] >= candidate[2] for candidate in candidates])", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            l = [0,0]\n            g = [0,0]\n            for j in range(len(rating)):\n                if i!=j:\n                    if rating[j] < rating[i]:\n                        l[i<j]+=1\n                    if rating[j] > rating[i]:\n                        g[i<j]+=1\n            count+= l[0]*g[1] + l[1]*g[0]\n        return count\n", "def findmyTeams(myrating: List[int], member_list: List[int], current_member: int) -> int:\n  my_new_member_list = member_list.copy()\n  if(len(member_list) == 3):\n    #print(member_list)\n    return 1\n  elif(len(myrating) == 0):\n    return 0\n  \n  score = 0\n  for a in range(len(myrating)):\n    if(myrating[a] > current_member):\n      #print(myrating[a], current_member)\n      my_new_member_list.append(myrating[a])\n      score += findmyTeams(myrating[a:], my_new_member_list, myrating[a])\n      my_new_member_list.pop()\n  \n  return score\n\n\nclass Solution:\n  def numTeams(self, rating: List[int]) -> int:\n    #We will iterate through the list from left to right first.\n    #So that means we will have two for loops\n    answer = 0\n    for i in range(len(rating)):\n      answer += findmyTeams(rating[i:], [rating[i]], rating[i])\n    \n    #print(\\\"Normal:\\\", answer)\n    \n    rating.reverse()\n    for i in range(len(rating)):\n      answer += findmyTeams(rating[i:], [rating[i]], rating[i])\n      \n      \n    #print(\\\"Reversed: \\\", answer)\n    \n    \n    return(answer)\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        output = 0\n        for item in itertools.combinations([x for x in range(len(rating))],3):\n            if rating[item[0]] < rating[item[1]] < rating[item[2]] or rating[item[0]] > rating[item[1]] > rating[item[2]]:\n                output += 1\n        return output\n            \n", "class Solution:\n    \n    def checkList(self, head: List[List[int]], tail: List[int], init: int) -> int:\n        result = init\n        head_copy = head.copy()\n        for i in head_copy:\n            num = tail[0]\n            ilen = len(i)\n            if ilen == 2:\n                if (i[0] < i[1] and i[1] < num) or (i[0] > i[1] and i[1] > num):\n                    result+=1   \n            elif ilen < 2:\n                head.append(i + [num])    \n\n        if len(tail) == 1: \n            return result\n        else:\n            return self.checkList(head, tail[1:], result)\n        \n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3: \n           return 0 \n        return self.checkList([[]], rating, 0)    ", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        num = 0\n        for i in range(len(rating)):\n            for j in range(i + 1, len(rating)):\n                for k in range(j + 1, len(rating)):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        num += 1\n        return num\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        self.res = 0\n        for i in range(len(rating)):\n            self.dfs_increase([rating[i]], rating[i+1:], 1)\n            self.dfs_decrease([rating[i]], rating[i+1:], 1)\n        return self.res\n    \n    def dfs_increase(self, stack, rating, count):\n        if len(stack) == 3:\n            self.res += 1\n            return\n        for i in range(len(rating)):\n            if rating[i] > stack[-1]:\n                self.dfs_increase(stack + [rating[i]], rating[i+1:], count + 1)\n    \n    def dfs_decrease(self, stack, rating, count):\n        if len(stack) == 3:\n            self.res += 1\n            return\n        for i in range(len(rating)):\n            if rating[i] < stack[-1]:\n                self.dfs_decrease(stack + [rating[i]], rating[i+1:], count + 1)\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3:\n            return 0\n        \n        ratingCount = 0\n        for i in range (0, len(rating) - 2):\n            for j in range (i+1, len(rating) - 1):\n                for k in range (j+1, len(rating)):\n                    if rating[i] > rating[j] > rating[k] or rating[i] < rating[j] < rating[k]:\n                        ratingCount += 1\n        \n        return ratingCount", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        teams = 0\n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                for k in range(j+1,n):\n                    if(rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]):\n                        teams += 1\n        \n        return teams", "\n\nclass Solution:\n    def numTeams(self, rating):\n        length = len(rating)\n        num_combinations = 0\n        for i in range(length - 2):\n            for j in range(i + 1, length - 1):\n                for k in range(j + 1, length):\n                    if rating[i] < rating[j]:\n                        if rating[j] < rating[k]:\n                            num_combinations = num_combinations + 1\n                    elif rating[i] > rating[j]:\n                        if rating[j] > rating[k]:\n                            num_combinations = num_combinations + 1\n        return num_combinations\n\n\n\n", "class Solution:\n    def _is_valid_team(self, rating: List[int], i: int, j: int, k: int) -> bool:\n        if rating[i] < rating[j] and rating[j] < rating[k]:\n            return True\n        \n        if rating[i] > rating[j] and rating[j] > rating[k]:\n            return True\n        \n        return False\n\n    def numTeams(self, rating: List[int]) -> int:\n        num_teams = 0\n        \n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    if self._is_valid_team(rating, i, j, k):\n                        num_teams += 1\n                        \n        return num_teams", "class Solution:\n    def numTeams(self, arr: List[int]) -> int:\n        k = 3\n        self.memo = {}; ans = 0\n        \n        for i in range(len(arr)):\n            ans += self.helper(i, arr, k-1, True)\n        \n        self.memo = {}\n        for i in range(len(arr)):\n            ans += self.helper(i, arr, k-1, False)\n        \n        return ans\n    \n    def helper(self, i, arr, k, lookForIncreasing):\n        if k == 0:\n            return 1\n        \n        if i == len(arr):\n            return 0\n        \n        if (i, k) in self.memo:\n            return self.memo[(i, k)]\n        \n        res = 0\n        for j in range(i+1, len(arr)):\n            if arr[j] > arr[i] and lookForIncreasing:\n                res += self.helper(j, arr, k-1, lookForIncreasing)\n            \n            elif arr[j] < arr[i] and not lookForIncreasing:\n                res += self.helper(j, arr, k-1, lookForIncreasing)\n        \n        self.memo[(i, k)] = res\n        return res\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(0, len(rating)-2):\n            for j in range(i+1, len(rating)-1):\n                for k in range(j+1, len(rating)):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        count += 1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        count +=1\n                    \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        # rating.sort()\n        def ascend(nums, curLargest, count):\n            if count == 3:\n                return 1\n            if not nums:\n                return 0\n            total = 0\n            for i in range(len(nums)):\n                if nums[i] > curLargest:\n                    total += ascend(nums[i+1:], nums[i], count+1)\n            return total\n        \n        def descend(nums, curSmallest, count):\n            if count == 3:\n                return 1\n            if not nums:\n                return 0\n            total = 0\n            for i in range(len(nums)):\n                if nums[i] < curSmallest:\n                    total += descend(nums[i+1:], nums[i], count+1)\n            return total\n        \n        return ascend(rating, 0, 0) + descend(rating, float('inf'), 0)\n                \n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)-1):\n                for k in range(j+1,len(rating)):\n                    if rating[i]<rating[j] or rating[i] > rating[j]:\n                        if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                            if rating[j] < rating[k] or rating[j] > rating[k]:\n                                count=count+1\n        return count\n\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(len(rating)):\n            for j in range(i,len(rating)):\n                for k in range(j,len(rating)):\n                    if rating[i] > rating[j] > rating[k] or rating[i] < rating[j] < rating[k]:\n                        cnt += 1\n        return cnt\n                        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        total = 0\n        n = len(rating)\n        for i in range(n):\n            for j in range(i + 1, n):\n                for k in range(j + 1, n):\n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        total = total + 1\n        return total", "from itertools import combinations\nimport numpy as np\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        rating=np.array(rating)\n        for i in range(len(rating)):\n            if (sum(rating[i]<rating[i+1:])>=2):\n                soldiers=[x for x in rating[i+1:] if x>rating[i]]\n                comb_soldiers=combinations(soldiers,2)\n                count=count+len([comb for comb in comb_soldiers if list(comb)==sorted(comb)])\n\n            \n            if (sum(rating[i]>rating[i+1:])>=2):\n                soldiers=[x for x in rating[i+1:] if x<rating[i]]\n                comb_soldiers=combinations(soldiers,2)\n                count=count+len([comb for comb in comb_soldiers if list(comb)==sorted(comb, reverse=True)])\n        return count\n            \n            \n", "class Solution:\n    res = 0\n    def numTeams(self, rating: List[int]) -> int:\n        \n        def dfs(rating, start, prev):\n            if len(prev) == 3:\n                self.res += 1\n                return\n            for i in range(start, len(rating)):\n                if not prev or prev[-1] < rating[i]:\n                    dfs(rating, i+1, prev + [rating[i]])\n        \n        dfs(rating, 0, [])\n        dfs(rating[::-1], 0, [])\n        return self.res\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        res += self.increse_dfs(rating, [], 0)\n        res += self.decrese_dfs(rating, [], 0)\n        \n        return res\n        \n        \n    def increse_dfs(self, rating: List[int], tmp: List[int], res: int) -> int:\n        if len(tmp) == 3:\n            res += 1 \n            return res\n        if len(rating) == 0:\n            return res\n        \n        for idx, n in enumerate(rating):\n            if len(tmp) == 0:\n                tmp.append(n)\n                res = self.increse_dfs(rating[idx+1:], tmp, res)\n                tmp.pop()\n            else:\n                if n > tmp[-1]:\n                    tmp.append(n)\n                    res = self.increse_dfs(rating[idx+1:], tmp, res)\n                    tmp.pop()\n        return res\n    \n    def decrese_dfs(self, rating: List[int], tmp: List[int], res: int) -> int:\n        if len(tmp) == 3:\n            res += 1 \n            return res\n        if len(rating) == 0:\n            return res\n        \n        for idx, n in enumerate(rating):\n            if len(tmp) == 0:\n                tmp.append(n)\n                res = self.decrese_dfs(rating[idx+1:], tmp, res)\n                tmp.pop()\n            else:\n                if n < tmp[-1]:\n                    tmp.append(n)\n                    res = self.decrese_dfs(rating[idx+1:], tmp, res)\n                    tmp.pop()\n\n        return res\n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        teams = []\n        \n        for i in range(0, len(rating)):\n            for j in range(i + 1, len(rating)):\n                for k in range(j + 1, len(rating)):\n                    curr = (rating[i], rating[j], rating[k])\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        teams.append(curr)\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        teams.append(curr)\n                        \n        # print(teams)\n        return len(teams)\n                        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = 0\n        n = len(rating)\n        for i in range(n-2):\n            for j in range(i,n-1):\n                if rating [i] < rating[j] or rating[i] > rating[j]:\n                    for k in range(j, n):\n                        if rating[i] < rating[j] < rating[k]: teams += 1\n                        if rating[i] > rating[j] > rating[k]: teams += 1\n                            \n        return teams", "class node:\n    \n    def __init__(self,val):\n        \n        self.val = val\n        \n        self.right = None\n        \n        self.left = None\n\nclass Solution:\n    \n    def update(self,i,j,ind,root):\n    \n        if i == j:\n            root.val += 1\n        else:\n            \n            mid = (i+j) // 2\n            \n            if ind <=mid:\n                self.update(i,mid,ind,root.left)\n            elif ind > mid:\n                self.update(mid+1,j,ind,root.right)\n                \n            root.val = 0\n            \n            if root.left:\n                root.val +=root.left.val\n            if root.right:\n                root.val += root.right.val\n                \n                \n      \n    def findSum(self,i,j,left,right,root):\n        \n        if left<=i and right>=j:\n            return root.val\n        \n        elif right<i or left > j:\n            return 0\n        \n        mid = (i+j) // 2\n        \n        if right<=mid:\n            return self.findSum(i,mid,left,right,root.left)\n        elif left>mid:\n            return self.findSum(mid+1,j,left,right,root.right)\n        \n        else:\n            \n            x = self.findSum(i,mid,left,mid,root.left)\n            \n            y = self.findSum(mid+1,j,mid+1,right,root.right)\n            \n            return x+y\n        \n    \n    def build(self,i,j):\n        \n        if i == j:\n            root = node(0)\n            \n        else:\n            \n            mid = (i+j) // 2\n            root = node(0)\n            root.left = self.build(i,mid)\n            root.right = self.build(mid+1,j)\n            \n            \n            \n            if root.left:\n                root.val+=root.left.val\n                \n            if root.right:\n                root.val+=root.right.val\n                \n                \n        return root\n            \n   \n    def solve(self,rating):\n        n = max(rating) + 1\n        \n        self.arr = [0]*n\n        \n        length = len(rating)\n        \n        left = [0]*length\n        right = [0]*length\n        rootLeft = self.build(0,n)\n        rootRight = self.build(0,n)\n        \n        \n        for i in range(length):\n            \n            left[i] = self.findSum(0,n,0,rating[i]-1,rootLeft)\n            self.update(0,n,rating[i],rootLeft)\n                \n        for i in range(length-1,-1,-1):\n            \n            right[i] = self.findSum(0,n,rating[i]+1,n,rootRight)\n            self.update(0,n,rating[i],rootRight)\n            \n        \n        ans = 0\n        for i in range(1,length-1):\n            \n            ans = ans + left[i]*right[i]\n        \n        # print(left)\n        # print(right)\n        # print(\\\"************\\\")\n        return ans\n        \n    def numTeams(self, rating: List[int]) -> int:\n        \n        if len(rating)<3:\n            return 0\n        \n        temp = rating[:]\n        temp.sort()\n        \n        value = 1\n        \n        self.mp = {}\n        \n        for x in temp:\n            \n            if x not in self.mp:\n                \n                self.mp[x] = value\n                \n                value +=1\n                \n            \n        for i in range(len(rating)):\n            \n            rating[i] = self.mp[rating[i]]\n            \n            \n        \n        ans = self.solve(rating)+self.solve(rating[::-1])\n        \n        return ans\n        \n        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        team = 0\n        for i in range(len(rating)-2):\n            for j in range(i+1, len(rating)-1):\n                for k in range(j+1, len(rating)):\n                    if (rating[i]>rating[j]) and (rating[j]>rating[k]):\n                        team = team + 1\n                    elif (rating[i]<rating[j]) and (rating[j]<rating[k]):\n                        team = team + 1\n        return team", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)-2):\n            for j in range(i, len(rating)-1):\n                for k in range(j, len(rating)):\n                    if ((rating[i] < rating[j] and rating[j] < rating[k]) or\n                        (rating[i] > rating[j] and rating[j] > rating[k])):\n                        count+=1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                        if((rating[i]>rating[j] and rating[j]>rating[k]) or (rating[i]<rating[j] and rating[j]<rating[k])):\n                            count += 1\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        dobar = 0\n        for i in range(n-2):\n            for j in range(i+1,n-1):\n                for k in range(j+1,n):\n                    if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                        dobar += 1\n        return dobar", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(0,len(rating)-2):\n            for j in range(i,len(rating)-1):\n                for k in range(j,len(rating)):\n                    if rating[i]<rating[j]<rating[k]:\n                        count+=1\n                    elif rating[i]>rating[j]>rating[k]:\n                        count+=1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        \n        ratings = [[]]\n        \n#         for items in rating:            \n#             ratings += [item + [items] for item in ratings if len(item) < 3]\n          \n#         count = 0\n#         for item in ratings:\n#             if len(item) == 3:\n#                 if item[0] > item[1] and item[1] > item[2]:\n#                     count += 1\n#                 elif item[0] < item[1] and item[1] < item[2]:\n#                     count += 1\n\n        n = len(rating)\n\n        count = 0\n        for index_1 in range(0, n):            \n            for index_2 in range(index_1+1, n):                \n                for index_3 in range(index_2+1, n):\n                    if rating[index_1] < rating[index_2] and rating[index_2] < rating[index_3]:\n                        count += 1\n                        \n        for index_1 in range(n-1, -1, -1):  \n            for index_2 in range(index_1-1, -1, -1):  \n                for index_3 in range(index_2 -1, -1, -1):\n                    if rating[index_1] < rating[index_2] and rating[index_2] < rating[index_3]:\n                        count += 1\n                \n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        l = len(rating)\n        for i in range(0, l - 2):\n            for j in range(i + 1, l - 1):\n                for k in range(j + 1, l):\n                    if (rating[i] < rating[j] and rating[j] < rating[k]) or (rating[i] > rating[j] and rating[j] > rating[k]):\n                        res += 1\n\n        return res", "class Solution:\n    def solve_inc(self, rating, count, idx, last):\n        if(count == 3):\n            return 1\n        if(idx >= len(rating)):\n            return 0\n        if(rating[idx] > last):\n            return self.solve_inc(rating, count+1, idx+1, rating[idx]) + self.solve_inc(rating, count, idx+1, last)\n        else:\n            return self.solve_inc(rating, count, idx+1, last)\n    def solve_dec(self, rating, count, idx, last):\n        if(count == 3):\n            return 1\n        if(idx >= len(rating)):\n            return 0\n        if(rating[idx] < last):\n            return self.solve_dec(rating, count+1, idx+1, rating[idx]) + self.solve_dec(rating, count, idx+1, last)\n        else:\n            return self.solve_dec(rating, count, idx+1, last)\n    def numTeams(self, rating: List[int]) -> int:\n        idx = 0\n        res = 0\n        # print(self.solve_inc(rating, 1, 1, rating[0]))\n        for i in range(len(rating)):\n            res += self.solve_inc(rating, 1, i+1, rating[i])\n            res += self.solve_dec(rating, 1, i+1, rating[i])\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        # brute force\n        n = len(rating)\n        total = 0\n        for i in range(n):\n            for j in range(i,n):\n                for k in range(j,n):\n                    total += any([\n                        rating[i] < rating[j] < rating[k],\n                        rating[i] > rating[j] > rating[k]\n                    ])   \n        return total", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        count = 0\n        \n        for i in range(n):\n            for j in range(i+1,n-1):\n                for k in range(j+1,n):\n                    if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                        count +=1\n        return count                ", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        result = 0\n        \n        for i in range(len(rating)):\n            for j in range(len(rating)):\n                if 0 <= i < j:\n                    for k in range(len(rating)):\n                        if j < k < len(rating):\n                            if (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]):\n                                result += 1\n        \n        return result\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        from itertools import combinations\n        \n        def satisfies_condition(inp):\n            if inp[0] < inp[1] < inp[2] or inp[0] > inp[1] > inp[2]:\n                return True\n        \n        team_count = 0\n        for x in list(combinations(rating, 3)):\n            team_count += 1 if satisfies_condition(x) else 0\n        \n        return team_count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        possible_list = []\n        count = 0\n        for i, first_num in enumerate(rating[:-2]):\n            for j, sec_num in enumerate(rating[i+1:-1]):\n                for k, third_num in enumerate(rating[j+i+2:]):\n                    possible_list.append((first_num, sec_num, third_num))\n        for pair in possible_list:\n            if pair[0] > pair[1] > pair[2] or pair[2] > pair[1] > pair[0]:\n                count += 1\n        return count\n\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        #s1: recursive approach, needs to be made into dp\n        \n        def solve(arr,n,k,arr_max):\n\n            #base condition\n            if k==0:\n                return 1\n\n            if n==len(arr):\n                return 0\n\n            #recursive logic\n            if arr[n] > arr_max:\n                return solve(arr,n+1,k-1,arr[n]) + solve(arr,n+1,k,arr_max)\n            else:\n                return solve(arr,n+1,k,arr_max)\n        \n        return solve(rating,0,3,0)+solve(rating[::-1],0,3,0)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        total = 0\n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                for k in range(j+1, n):\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        total += 1\n                    elif rating[i] < rating[j] and rating[j] < rating[k]:\n                        total += 1\n        return total", "class Solution:\n    def solve_inc(self, rating, count, idx, last):\n        if(count == 3):\n            return 1\n        if(idx >= len(rating)):\n            return 0\n        if(rating[idx] > last):\n            return self.solve_inc(rating, count+1, idx+1, rating[idx]) + self.solve_inc(rating, count, idx+1, last)\n        else:\n            return self.solve_inc(rating, count, idx+1, last)\n    def solve_dec(self, rating, count, idx, last):\n        if(count == 3):\n            return 1\n        if(idx >= len(rating)):\n            return 0\n        if(rating[idx] < last):\n            return self.solve_dec(rating, count+1, idx+1, rating[idx]) + self.solve_dec(rating, count, idx+1, last)\n        else:\n            return self.solve_dec(rating, count, idx+1, last)\n    def numTeams(self, rating: List[int]) -> int:\n        idx = 0\n        res = 0\n        for i in range(len(rating)):\n            res += self.solve_inc(rating, 1, i+1, rating[i])\n            res += self.solve_dec(rating, 1, i+1, rating[i])\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        stack=[]\n        list1=[]\n        n=len(rating)\n        for i in range(0,n-2):\n            # check 3 items from in the list iterating using i,j,k.i until n-2,j until n-1 and k untill n\n            for j in range(i+1,n-1):\n                for k in range(j+1,n):\n                    if rating[i]<rating[j]<rating[k] or rating[i]>rating[j]>rating[k]:\n                        stack.append(rating[i])\n                        stack.append(rating[j])\n                        stack.append(rating[k])\n                        #print(\\\"stack\\\",stack)\n                        list1.append(stack)\n                    stack=[]\n        #print(list1)\n        return len(list1)\n                \n                        \n", "class Solution:\n    def numTeams(self, a: List[int]) -> int:\n        list1=[]\n        for i in range(len(a)-2):\n            for j in range(i+1,len(a)-1):\n                for k in range(j+1,len(a)):\n                    if a[i]<a[j]<a[k]:\n                        list1.append((a[i],a[j],a[k]))\n                    if a[i]>a[j]>a[k]:\n                        list1.append((a[i],a[j],a[k]))\n        return len(list1)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ratingLen = len(rating)\n        result = 0\n        for i in range(ratingLen-2):\n            for j in range(i+1, ratingLen-1):\n                for k in range(j+1, ratingLen):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        result += 1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        result += 1\n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                for k in range(j+1,len(rating)):\n                    if self.check(rating[i],rating[j],rating[k]):\n                        # print(rating[i],rating[j],rating[k])\n                        count+=1\n        return count\n    \n    def check(self,i,j,k):\n        if i<j and j<k:\n            return True\n        elif i>j and j>k:\n            return True\n        else:\n            return False", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        nTeams = 0\n        for i in range(len(rating)):\n            for j in range(i, len(rating)):\n                for k in range(j, len(rating)):\n                    if rating[j] > rating[i]:\n                        if rating[k] > rating[j]:\n                            nTeams += 1\n                    elif rating[j] < rating[i]:\n                        if rating[k] < rating[j]:\n                            nTeams += 1\n        return nTeams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        \n        if n < 3:\n            return 0\n        \n        results = 0\n        for i in range(n - 2):\n            for j in range(i + 1, n - 1):\n                for k in range (j + 1, n):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        results += 1\n                    elif rating[i] > rating[j] and rating[j] > rating[k]:\n                        results +=1\n        return results", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = 0\n        l = []\n        x = 0\n\n        while x <= len(rating)-2:\n            y = x + 1\n            while y <= len(rating)-1:\n                z = y + 1\n                while z < len(rating):\n                    if rating[x] < rating[y] and rating[y] < rating[z]:\n                        teams += 1\n                        l.append([rating[x], rating[y], rating[z]])\n                    elif rating[x] > rating[y] and rating[y] > rating[z]:\n                        teams += 1\n                        l.append([rating[x], rating[y], rating[z]])\n\n                    z += 1\n                y += 1\n            x += 1\n\n    \n        return teams\n    \n", "class Solution:    \n    def numTeams(self, ratings: List[int]) -> int:\n        #maintain a data structure where dict[index] stores another dict keyed by ranking (r) \\\\\n        #and stores number of rankings greater than that ranking in the rating list\n        res = 0\n        \n        numRatings = len(ratings)\n        sortedRatings = sorted(ratings)\n        \n        mapping = dict()\n        mapping_rev = dict()\n        mapping[len(ratings)] = dict(list(zip(ratings,[0]*numRatings)))\n        mapping_rev[len(ratings)] = dict(list(zip(ratings,[0]*numRatings)))\n        for index, rating in list(enumerate(ratings))[::-1]:\n            mapping[index] = dict()\n            mapping_rev[index] = dict()\n            for tmpRating in ratings:\n                if tmpRating < rating:\n                    mapping[index][tmpRating] = mapping[index+1][tmpRating] + 1\n                    mapping_rev[index][tmpRating] = mapping_rev[index+1][tmpRating]\n                else:\n                    mapping[index][tmpRating] = mapping[index+1][tmpRating]\n                    mapping_rev[index][tmpRating] = mapping_rev[index+1][tmpRating] + 1\n        \n        for i, rating_i in list(enumerate(ratings)):\n            #check triplets starting with rating[i]\n            for j, rating_j in list(dict(list(zip(list(range(i+1,numRatings+1)), ratings[i+1:]))).items()):\n                if rating_j > rating_i:\n                    res += mapping[j+1][rating_j]\n                else:\n                    res += mapping_rev[j+1][rating_j]\n        \n        return res\n            \n                \n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        def loop(prev, remains, i):\n            res = 0\n            if i >= len(rating):\n                return res\n            if rating[i] > prev:\n                if remains == 1:\n                    res += 1\n                else:\n                    res += loop(rating[i], remains-1, i+1)\n            res += loop(prev, remains, i+1)\n            return res\n        res = 0\n        res += loop(-1, 3, 0)\n        rating.reverse()\n        res += loop(-1, 3, 0)\n        return res", "class Solution:\n    def validTriple(self, a,b,c):\n        if (a < b and b < c) or (c < b and b < a):\n            return True\n        return False\n        \n    def numTeams(self, rating: List[int]) -> int:\n        ans = 0\n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                for k in range(j+1, len(rating)):\n                    if self.validTriple(rating[i], rating[j], rating[k]):\n                        ans += 1\n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        i = 0\n        j = i + 1\n        k = j+1\n        result = 0\n        while (i < len(rating)- 2):\n            if k == len(rating):\n                j += 1\n                k = j\n\n            if (j == len(rating)):\n                i += 1\n                j = i + 1\n                k = j\n\n            if rating[i] < rating[j] and rating[j] < rating[k]:\n                result += 1\n            elif rating[i] > rating[j] and rating[j] > rating[k]:\n                result += 1\n\n\n\n            k+= 1\n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        sum = 0\n        for i in range(len(rating)):\n            for j in range(i,len(rating)):\n                for k in range(j, len(rating)):\n                    if rating[i] < rating[j] < rating[k]:\n                        sum += 1\n                    if rating[len(rating)-1-i] < rating[len(rating)-1-j] < rating[len(rating)-1-k]:\n                        sum+=1\n        return sum\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = 0\n        n = len(rating)\n        for i in range(n-2):\n            for j in range(i,n-1):\n                for k in range(j, n):\n                    if i < j < k:\n                        if rating[i] < rating[j] < rating[k]: teams += 1\n                        if rating[i] > rating[j] > rating[k]: teams += 1\n                            \n        return teams", "class Solution:\n    \n    def checkList(self, head, tail, init: int) -> int:\n        result = init\n        head_copy = head.copy()\n        for i in head_copy:\n            num = tail[0]\n            ilen = len(i)\n            if ilen > 0:\n                if ilen == 2:\n                    if i[0] < i[1] and i[1] < num:\n                        result+=1   \n                    if i[0] > i[1] and i[1] > num:\n                        result+=1\n                elif ilen == 1:\n                    head.append(i + [num])   \n            else:\n                head.append([num])       \n\n        if len(tail) == 1: \n            return result\n        else:\n            del tail[0]\n            return self.checkList(head, tail, result)\n        \n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3: \n           return 0 \n        list = [[]]\n        return self.checkList(list, rating, 0)    ", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        # Backtrack for positive and backtrack for negative\n        res  = []\n        n = len(rating)\n        \n        def btInc(index: int, curr: List[int]):\n            if len(curr) == 3:\n                res.append(curr)\n                return\n            \n            for i in range(index+1, n):\n                if curr==[] or rating[i]> curr[-1]:\n                    btInc(i, curr+[rating[i]])\n        \n        def btDec(index: int, curr: List[int]):\n            if len(curr) == 3:\n                res.append(curr)\n                return\n            \n            for i in range(index+1, n):\n                if curr==[] or rating[i] < curr[-1]:\n                    btDec(i, curr+[rating[i]])\n                    \n        btInc(-1,[])\n        btDec(-1,[])\n        \n        return len(res)\n        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        for i in range(len(rating) - 2):\n            for j in range(i, len(rating) - 1):\n                for k in range(j, len(rating)):\n                    if rating[i] < rating[j] and rating[j] < rating[k]:\n                        res += 1\n                    if rating[i] > rating[j] and rating[j] > rating[k]:\n                        res += 1\n        return res", "class 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", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i, r in enumerate(rating):\n            if i >= len(rating) - 2:\n                break\n            \n            # check asc\n            li = [val for val in rating[i + 1:] if val > r]\n            for j, r2 in enumerate(li):\n                li2 = [val for val in li[j + 1:] if val > r2]\n                cnt += len(li2)\n\n            # check desc\n            li = [val for val in rating[i + 1:] if val < r]\n            for j, r2 in enumerate(li):\n                li2 = [val for val in li[j + 1:] if val < r2]\n                cnt += len(li2)\n            \n        return cnt\n    \n        \n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            l = [rating[i]]\n            for j in range(i+1,len(rating)):\n                l.append(rating[j])\n                for k in range(j+1,len(rating)):\n                    l.append(rating[k])\n                    if (l[0] > l[1] and l[1] > l[2]) or (l[0] < l[1] and l[1] < l[2]):\n                        count += 1\n                    l = l[:2]\n                l = l[:1]\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        return self.sumHigh(rating)\n\n    def sumHigh(self, rating: List[int]) -> int:\n        sum = 0\n        for i in range(len(rating)):\n            for j in range(i,len(rating)):\n                for k in range(j, len(rating)):\n                    if rating[i] < rating[j] < rating[k]:\n                        sum += 1\n                    if rating[len(rating)-1-i] < rating[len(rating)-1-j] < rating[len(rating)-1-k]:\n                        sum+=1\n        return sum\n    \n    def sumLow(self, rating: List[int]) -> int:\n        sum = 0\n        for i in range(len(rating)-1, -1, -1):\n            for j in range(i,-1, -1):\n                for k in range(j, -1, -1):\n                    if rating[i] < rating[j] < rating[k]:\n                        sum += 1   \n        return sum", "class Solution:\n    def numTeams(self, a: List[int]) -> int:\n        \n        def rec(a, start, path, res):\n                        \n            for i in range(start, len(a)):\n                \n                if len(path) == 0:\n                    path.append(a[i])\n                    rec(a, i + 1, path, res)\n                    path.pop()\n                    \n                elif len(path) == 1:\n                    if a[i] != path[-1]:\n                        path.append(a[i])\n                        rec(a, i + 1, path, res)\n                        path.pop()\n                                                                                \n                elif len(path) == 2:\n                    if path[0] < path[1] and path[1] < a[i]:\n                        path.append(a[i])\n                        res[0] += 1\n                        path.pop()\n                        \n                    elif path[0] > path[1] and path[1] > a[i]:\n                        path.append(a[i])\n                        res[0] += 1\n                        path.pop()\n                        \n            return\n        \n        res = [0]\n        rec(a, 0, [], res)\n        \n        return res[0]\n                        \n                    \n                \n                    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        \n        def backtrack(rating, cur, index):\n            \n            if len(cur) == 3:\n                self.count += 1\n                return\n            \n            if len(cur) > 3:\n                return\n            \n            for i in range(index, len(rating)):\n                if len(cur) == 2 and cur[0] < cur[1] and rating[i] < cur[1]:\n                    continue\n                if len(cur) == 2 and cur[0] > cur[1] and rating[i] > cur[1]:\n                    continue\n                    \n                backtrack(rating, cur + [rating[i]], i + 1)\n        \n        \n        self.count = 0\n        backtrack(rating, [], 0)\n        return self.count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        def checkLess(a,b,c):\n            return rating[a] < rating[b] < rating[c]\n        def checkG(a,b,c):\n            return rating[a] > rating[b] > rating[c]\n        if len(rating) < 3: return 0\n        if len(rating) == 3:\n            if checkLess(0,1,2) or checkG(0,1,2):\n                return 1\n            return 0\n        \n        result = 0\n        n = len(rating)\n        for i in range(n):\n            for j in range(i,n):\n                for k in range(j, n):\n                    if checkLess(i,j,k) or checkG(i,j,k):\n                        result += 1\n                \n        return result\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        i = 0\n        j = 1 \n        k = 2\n        while i < len(rating) - 2:\n            if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                res += 1\n            k += 1\n            if k > len(rating) - 1:\n                j += 1\n                k = j + 1\n            if j > len(rating) - 2:\n                i += 1\n                j = i + 1\n                k = i + 2\n        return res", "from itertools import combinations\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        indices = list(range(0, len(rating)))\n        all_combs = combinations(indices,3)\n        result = 0\n        for comb in all_combs:\n            if comb[0] < comb[1] and comb[1] < comb[2]:\n                if rating[comb[0]] < rating[comb[1]] and rating[comb[1]] < rating [comb[2]]:\n                    result += 1\n                elif rating[comb[0]] >rating[comb[1]] and rating[comb[1]] > rating[comb[2]]:\n                    result += 1\n        return result\n            \n        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        counter = 0\n        for i in range(len(rating)):\n            for j in range(len(rating)):\n                for k in range(len(rating)):\n                    #print(f\\\"Testing: {rating[i]} {rating[j]} {rating[k]}\\\")\n                    if(i < j and j < k):\n                        if(rating[i] < rating[j] and rating[j] < rating[k]):\n                            #print(f\\\"Counting up1 {counter}\\\")\n                            counter += 1\n                        elif(rating[i] > rating[j] and rating[j] > rating[k]):\n                            #print(f\\\"Counting up2 {counter}\\\")\n                            counter += 1\n        \n        return counter", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        z=len(rating)\n        final=[]\n        if z<3 :\n            return 0\n        for i in range (0,z):\n            for j in range (i+1,z):\n                for k in range (j+1,z):\n                    if  (rating[i]<rating[j] and rating[j]<rating[k]) or (rating[i]>rating[j] and rating[j]>rating[k]) :\n                        final.append((i,j,k))\n                        \n        final=set(final)\n        return len(final)\n                        \n            \n                        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        teams = 0\n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                if(rating[i] == rating[j]):\n                    continue\n                for k in range(j+1,n):\n                    if(rating[i] == rating[k] or rating[j] == rating[k]):\n                        continue\n                    if(rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]):\n                        teams += 1\n        \n        return teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i, r in enumerate(rating):\n            if i >= len(rating) - 2:\n                break\n            \n            # check asc\n            li = [val for val in rating[i + 1:] if val > r]\n            for j, r2 in enumerate(li):\n                li2 = [val for val in li[j + 1:] if val > r2]\n                cnt += len(li2)\n                \n#                 if li2:\n#                     print(r, r2, li2)\n            \n            # if li:\n            #     cnt += self.count_comb(li, r, True)\n            \n            # check desc\n            li = [val for val in rating[i + 1:] if val < r]\n            for j, r2 in enumerate(li):\n                li2 = [val for val in li[j + 1:] if val < r2]\n                cnt += len(li2)\n            \n#                 if li2:\n#                     print(r, r2, li2)\n            \n            # if li:\n            #     cnt += self.count_comb(li, r, False)\n            \n        return cnt\n    \n    \n    def count_comb(self, li, num, asc):\n        cnt = 0\n        \n        for i, r in enumerate(li):\n            if i >= len(li) - 1:\n                break\n            \n            if asc:\n                for j in range(i + 1, len(li)):\n                    if num < li[j]:\n                        cnt += 1\n            else:\n                for j in range(i + 1, len(li)):\n                    if num > li[j]:\n                        cnt += 1\n\n        return cnt\n        \n            \n", "class Solution:\n    def numTeams(self, ratings: List[int]) -> int:\n        teams = []\n        for i1, r1 in enumerate(ratings):\n            for si2, r2 in enumerate(ratings[i1+1:]):\n                i2 = i1+1+si2\n                for r3 in ratings[i2+1:]:\n                    teams.append((r1, r2, r3))\n        n = 0\n        for i, j, k in teams:\n            if i<j<k or i>j>k:\n                n +=1\n \n        return n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        teams = 0\n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                if(rating[i] == rating[j]):\n                    continue\n                for k in range(j+1,n):\n                    if(rating[i] == rating[k]):\n                        continue\n                    if(rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]):\n                        teams += 1\n        \n        return teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        c = 0\n        for i in range(len(rating)):\n            j = i + 1\n            while(j<len(rating)):\n                k= j + 1\n                while(k<len(rating)):    \n                    if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                        c += 1\n                    k += 1\n                \n                j += 1\n        \n        return c\n                        \n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        counter = 0\n        \n        for i in range(len(rating)):\n            for j in range(len(rating)):\n                for k in range(len(rating)):\n                    if i < j < k:\n                        if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]:\n                            counter += 1\n        \n        return counter\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        c=0\n        for i in range(len(rating)):\n            for j in range(len(rating)):\n                for k in range(len(rating)):\n                    if i<j<k:\n                        if rating[i]>rating[j]>rating[k] or rating[i]<rating[j]<rating[k]:\n                            c+=1\n        return c\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        def helper(team, rem):\n            if len(team)==3:\n                self.count+=1\n                return\n            \n            for i in range(len(rem)):\n                if len(team)==1:\n                    helper(team+[rem[i]], rem[i:])\n                else:\n                    if team[-2]<team[-1]:\n                        if rem[i]>team[-1]:\n                            helper(team+[rem[i]],rem[i:])\n                    elif team[-2]>team[-1]:\n                        if rem[i]<team[-1]:\n                            helper(team+[rem[i]],rem[i:])\n            \n        self.count=0\n        for i in range(len(rating)):\n            helper([rating[i]],rating[i:])\n        return self.count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(len(rating)):\n            for j in range(len(rating)):\n                for k in range(len(rating)):\n                    if i<j<k:\n                        if rating[i]>rating[j]>rating[k] or rating[i]<rating[j]<rating[k]:\n                            count+=1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        def combination(rating, tmp, index, ans, target):\n            if len(tmp) == target:\n                ans.append(tmp[:])\n                return\n            for i in range(index, len(rating)):\n                if len(tmp) == 2 and not (tmp[0] > tmp[1] > rating[i] or rating[i] > tmp[1] > tmp[0]):\n                    continue\n                tmp.append(rating[i])\n                combination(rating, tmp, i + 1, ans, target)\n                tmp.pop()\n\n        possibles = []\n        combination(rating, [], 0, possibles, 3)\n\n        count = 0\n        for a, b, c in possibles:\n            if a < b < c or a > b > c:\n                count += 1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams_all = list(itertools.combinations(rating, 3))\n        valid_teams = []\n        for team in teams_all:\n            if team[0] > team[1] > team[2] or team[0] < team[1] < team[2]:\n                valid_teams.append(team)\n        return len(valid_teams)\n                         \n", "from itertools import combinations\nclass Solution:\n    \n    def numTeams(self, rating: List[int]) -> int:\n        ### Store the index in dictionary \n        from itertools import combinations\n\n        dic = {}\n        for i, x in enumerate(rating):\n            dic[x] = i\n        rating.sort()\n        cnt = 0\n        combinations = list(combinations(rating,3))\n        \n        \n        for comb in combinations:\n            if (dic[comb[0]] < dic[comb[1]] and dic[comb[1]] < dic[comb[2]]) or (dic[comb[0]] > dic[comb[1]] and dic[comb[1]] > dic[comb[2]]):\n                \n                cnt += 1\n        return cnt\n    \n    \n    \n        # for i in range(len(rating)-2):\n        #     print(dic[rating[i]], dic[rating[i + 1]], dic[rating[i + 2]])\n        #     if dic[rating[i]] < dic[rating[i + 1]] and dic[rating[i + 1]] < dic[rating[i+ 2]]:\n        #         print(dic[rating[i]], dic[rating[i + 1]], dic[rating[i + 2]])\n        #         print((rating[i], rating[i+1], rating[i+2]))\n        #         cnt += 1\n        \n        \n        \n                \n                \n        \n        # for i in range(len(rating)-1, 1, -1):\n        #     if dic[rating[i]] > dic[rating[i -1]] and dic[rating[i - 1]] > dic[rating[i -2]]:\n        #         print(dic[rating[i]], dic[rating[i - 1]], dic[rating[i - 2]])\n        #         print((rating[i], rating[i-1], rating[i-2]))\n        #         cnt += 1\n                \n        return cnt                                                            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count_teams = 0\n        temp_team = []\n        all_possible_teams = []\n        for i in range(len(rating)):\n            \n            for j in range(i, len(rating)):\n                \n                \n                for k in range(j, len(rating)):\n                    \n                    if i != j and j != k:\n                        all_possible_teams.append((rating[i], rating[j], rating[k]))\n                        \n        \n        for i in all_possible_teams:\n            if i[0] < i[1] and i[1] < i[2]:\n                count_teams = count_teams + 1\n            if i[0] > i[1] and i[1] > i[2]:\n                count_teams = count_teams + 1\n        return count_teams\n        \n            \n", "class Solution:\n    def choose_inc(self,idx, num, gt, stack):\n        if num == 0:\n            self.soln.add(tuple(stack))\n            return\n        for jidx in range(idx, len(self.rating)):\n            jnum = self.rating[jidx]\n            #print(jidx, jnum, jnum> gt)\n            if jnum > gt:\n                self.choose_inc(jidx+1, num -1, jnum, stack + [jnum])\n    \n    def numTeams(self, rating: List[int]) -> int:\n        self.soln = set()\n        self.rating = rating\n        self.choose_inc(0, 3, 0, [])\n        sol_fwd = list(self.soln)\n        self.soln = set()\n        self.rating.reverse()\n        self.choose_inc(0,3,0,[])\n        return len(sol_fwd) + len(self.soln)", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        result = []\n        for i in range(len(rating)):\n            for j in range(len(rating)):\n                for k in range(len(rating)):\n                    if i<j<k and (rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]):\n                        result.append(rating)\n        return len(result)\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n#         target=3\n#         self.count=0\n#         self.lst=[]\n#         def isSolution(candidate_count,candid):\n#             if candidate_count==target and ((candid[0]<candid[1]<candid[2]) or (candid[0]>candid[1]>candid[2])):\n#                 return True\n#             # if candidate_count==target:return False\n            \n#         def backtrack(index,rating,candidates,candidate_count):\n#             if isSolution(candidate_count,candidates):\n#                 self.count+=1\n#                 return \n#             if candidate_count<3:\n#                 for i in range(index,len(rating)):\n#                     candidates.append(rating[i])\n#                     backtrack(i+1,rating,candidates,candidate_count+1)\n#                     candidates.pop()\n        \n#         directions=[1,-1]\n#         #-1 means decreasing order\n#         # for direction in directions:\n#         backtrack(0,rating,[],0)\n#         # return len(self.lst)\n#         return self.count\n        \n        \n        \n        \n        target=3\n        self.count=0\n        self.lst=[]\n        def isSolution(candidate_count):\n            if candidate_count==target:\n                return True\n            \n        def isViable(rating,i,candidates,candidate_count,direction):\n            if i==0 or candidate_count==0:return True\n            \n            if direction==-1 and rating[i]<candidates[-1]:\n                return True\n            if direction==1 and rating[i]>candidates[-1]:\n                return True\n            return False\n            \n            \n        def backtrack(index,rating,candidates,candidate_count,direction):\n            if isSolution(candidate_count):\n                self.count+=1\n                return \n            if candidate_count<3:\n                for i in range(index,len(rating)):\n                    if isViable(rating,i,candidates,candidate_count,direction):\n                        candidates.append(rating[i])\n                        backtrack(i+1,rating,candidates,candidate_count+1,direction)\n                        candidates.pop()\n        \n        directions=[1,-1]\n        #-1 means decreasing order\n        for direction in directions:\n            backtrack(0,rating,[],0,direction)\n        # return len(self.lst)\n        return self.count\n                \n                \n                \n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        \n        def dfs(i: int, prefix: List[int], increasing: bool) -> int:\n            if len(prefix) == 3: return 1\n            if i == len(rating): return 0\n            result = 0\n            rate = rating[i]\n            if (increasing and prefix[-1] < rate) or (not increasing and prefix[-1] > rate):\n                result += dfs(i+1, prefix + [rate], increasing)\n            result += dfs(i+1, prefix, increasing)\n            return result\n\n        \n        result = 0\n        for i in range(len(rating)):\n            result += dfs(i + 1, [rating[i]], True) + dfs(i + 1, [rating[i]], False)\n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i, s1 in enumerate(rating[:-2]):\n            for j, s2 in enumerate(rating[i+1:-1]):\n                if s2 > s1:\n                    for s3 in rating[i+j+2:]:\n                        if s3 > s2:\n                            count += 1\n                else:\n                    for s3 in rating[i+j+2:]:\n                        if s3 < s2:\n                            count += 1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        output = 0\n        for i in range(len(rating)):\n            for j in range(len(rating)):\n                if i >= j:\n                    continue\n                for k in range(len(rating)):\n                    if j >= k:\n                        continue\n                    if rating[i] > rating[j] > rating[k] or rating[i] < rating[j] < rating[k]:\n                        output += 1\n        return output                \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        target=3\n        self.count=0\n        self.lst=[]\n        def isSolution(candidate_count):\n            if candidate_count==target:\n                return True\n            \n        def isViable(rating,i,candidates,candidate_count,direction):\n            if i==0 or candidate_count==0:return True\n            \n            if direction==-1 and rating[i]<candidates[-1]:\n                return True\n            if direction==1 and rating[i]>candidates[-1]:\n                return True\n            return False\n            \n            \n        def backtrack(index,rating,candidates,candidate_count,direction):\n            if isSolution(candidate_count):\n                self.count+=1\n                return \n             \n            for i in range(index,len(rating)):\n                if isViable(rating,i,candidates,candidate_count,direction):\n                    candidates.append(rating[i])\n                    backtrack(i+1,rating,candidates,candidate_count+1,direction)\n                    candidates.pop()\n        \n        directions=[1,-1]\n        #-1 means decreasing order\n        for direction in directions:\n            backtrack(0,rating,[],0,direction)\n        # return len(self.lst)\n        return self.count\n                \n                \n                \n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n#         target=3\n#         self.count=0\n#         self.lst=[]\n#         def isSolution(candidate_count,candid):\n#             if candidate_count==target and ((candid[0]<candid[1]<candid[2]) or (candid[0]>candid[1]>candid[2])):\n#                 return True\n#             # if candidate_count==target:return False\n            \n#         def backtrack(index,rating,candidates,candidate_count):\n#             if isSolution(candidate_count,candidates):\n#                 self.count+=1\n#                 return \n#             if candidate_count<3:\n#                 for i in range(index,len(rating)):\n#                     candidates.append(rating[i])\n#                     backtrack(i+1,rating,candidates,candidate_count+1)\n#                     candidates.pop()\n        \n#         directions=[1,-1]\n#         #-1 means decreasing order\n#         # for direction in directions:\n#         backtrack(0,rating,[],0)\n#         # return len(self.lst)\n#         return self.count\n        \n        \n        \n        \n        target=3\n        self.count=0\n        self.lst=[]\n        def isSolution(candidate_count):\n            if candidate_count==target:\n                return True\n            \n        def isViable(rating,i,candidates,candidate_count,direction):\n            if i==0 or candidate_count==0:return True\n            \n            if direction==-1 and rating[i]<candidates[-1]:\n                return True\n            if direction==1 and rating[i]>candidates[-1]:\n                return True\n            return False\n            \n            \n        def backtrack(index,rating,candidates,candidate_count,direction):\n            if isSolution(candidate_count):\n                self.count+=1\n                return \n             \n            for i in range(index,len(rating)):\n                if isViable(rating,i,candidates,candidate_count,direction):\n                    candidates.append(rating[i])\n                    backtrack(i+1,rating,candidates,candidate_count+1,direction)\n                    candidates.pop()\n        \n        directions=[1,-1]\n        #-1 means decreasing order\n        for direction in directions:\n            backtrack(0,rating,[],0,direction)\n        # return len(self.lst)\n        return self.count\n                \n                \n                \n            \n", "class Solution:\n    def numTeams(self, a: List[int]) -> int:\n        \n        def rec(a, start, path, res):\n                        \n            for i in range(start, len(a)):\n                \n                if len(path) == 0:\n                    path.append(a[i])\n                    rec(a, i + 1, path, res)\n                    path.pop()\n                    \n                elif len(path) == 1:\n                    if a[i] != path[-1]:\n                        path.append(a[i])\n                        rec(a, i + 1, path, res)\n                        path.pop()\n                                                                                \n                elif len(path) == 2:\n                    if path[0] < path[1] and path[1] < a[i]:\n                        path.append(a[i])\n                        res.append(path[:])\n                        path.pop()\n                        \n                    elif path[0] > path[1] and path[1] > a[i]:\n                        path.append(a[i])\n                        res.append(path[:])\n                        path.pop()\n                        \n            return\n        \n        res = []\n        rec(a, 0, [], res)\n        \n        return len(res)\n                        \n                    \n                \n                    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        # return sum(1 for i in range(len(rating)) for j in range(i) for k in range(j) if rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k])\n\n        max_rating = 100000\n\n        def strictly_monotonic(\n            rating: List[int],\n            previous_rating: int,\n            remaining_ratings: int,\n            ascending: bool,\n        ) -> int:\n            if not remaining_ratings:\n                return 1\n            if not rating:\n                return 0\n            if ascending ^ (rating[0] < previous_rating):\n                return strictly_monotonic(\n                    rating[1:], rating[0], remaining_ratings - 1, ascending\n                ) + strictly_monotonic(\n                    rating[1:], previous_rating, remaining_ratings, ascending\n                )\n            else:\n                return strictly_monotonic(\n                    rating[1:], previous_rating, remaining_ratings, ascending\n                )\n\n        return strictly_monotonic(rating, 0, 3, ascending=True) + strictly_monotonic(\n            rating, max_rating + 1, 3, ascending=False\n        )\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        target=3\n        self.count=0\n        self.lst=[]\n        def isSolution(candidate_count):\n            if candidate_count==target:\n                return True\n            \n        def isViable(rating,i,candidates,direction):\n            if i==0 or len(candidates)==0:return True\n            \n            if direction==-1 and rating[i]<candidates[-1]:\n                return True\n            if direction==1 and rating[i]>candidates[-1]:\n                return True\n            return False\n            \n            \n        def backtrack(index,rating,candidates,candidate_count,direction):\n            if isSolution(candidate_count):\n                # self.lst.append(list(candidates))\n                self.count+=1\n                return \n             #-1 means decreasing order\n            # for direction in directions:\n            for i in range(index,len(rating)):\n                if isViable(rating,i,candidates,direction):\n                    candidates.append(rating[i])\n                    backtrack(i+1,rating,candidates,candidate_count+1,direction)\n                    candidates.pop()\n        \n        directions=[1,-1]\n        for direction in directions:\n            backtrack(0,rating,[],0,direction)\n        # return len(self.lst)\n        return self.count\n                \n                \n                \n            \n", "class Solution:\n    def numTeams(self, rating):\n        self.count = 0\n        def dfs(soldier, idx):\n            if len(soldier) == 3:\n                if soldier[0] < soldier[1] < soldier[2] or soldier[0] > soldier[1] > soldier[2]:\n                    self.count += 1\n                return\n                # 5 % 3\n            for i in range(idx, len(rating)):\n                soldier.append(rating[i])\n                dfs(soldier, i + 1)\n                soldier.pop()\n        dfs([],0)\n        return self.count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) < 3:\n            return 0\n        \n        teams = []\n        self.select([], 3, True, rating, teams)\n        self.select([], 3, False, rating, teams)\n        \n        return len(teams)\n        \n        \n        \n    def select(self, choice, length, increasing, ratings, teams):\n        if len(choice) == length:\n            teams.append(list(choice))\n            return True\n        \n        for i, team in enumerate(ratings):\n            selected = None\n            if len(choice) == 0:\n                selected = team\n            elif increasing and choice[-1] < team:\n                selected = team\n            elif not increasing and choice[-1] > team:\n                selected = team\n                \n            if selected == None:\n                continue\n                \n            choice.append(selected)\n            res = self.select(choice, length, increasing, ratings[i + 1:], teams)\n            choice.pop()\n                \n        return False", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for a, soldier_1 in enumerate(rating):\n            for b, soldier_2 in enumerate(rating):\n                for c, soldier_3 in enumerate(rating):\n                    if a < b < c:\n                        if soldier_1 < soldier_2 < soldier_3:\n                            count += 1\n                        if soldier_1 > soldier_2 > soldier_3:\n                            count += 1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans = 0\n        for i in range(len(rating) - 2) :\n            head = rating[i]\n            #print(head)\n            for j in range(len(rating) - i - 2) :\n                sec = rating[i + j + 1]\n                #print(head,sec)\n                if sec > head :\n                    for k in rating[i + j + 2::] :\n                        if k > sec : ans = ans + 1\n                elif sec < head : \n                    for k in rating[i + j + 2::] :\n                        if k < sec : ans = ans + 1\n        return ans", "from itertools import combinations\nclass Solution:\n    \n    def numTeams(self, rating: List[int]) -> int:\n        ### Store the index in dictionary \n        from itertools import combinations\n\n        dic = {}\n        for i, x in enumerate(rating):\n            dic[x] = i\n        rating.sort()\n        cnt = 0\n        combinations = list(combinations(rating,3))\n\n        for comb in combinations:\n            if (dic[comb[0]] < dic[comb[1]] and dic[comb[1]] < dic[comb[2]]) or (dic[comb[0]] > dic[comb[1]] and dic[comb[1]] > dic[comb[2]]):\n                \n                cnt += 1\n        return cnt\n    \n    \n    \n        # for i in range(len(rating)-2):\n        #     print(dic[rating[i]], dic[rating[i + 1]], dic[rating[i + 2]])\n        #     if dic[rating[i]] < dic[rating[i + 1]] and dic[rating[i + 1]] < dic[rating[i+ 2]]:\n        #         print(dic[rating[i]], dic[rating[i + 1]], dic[rating[i + 2]])\n        #         print((rating[i], rating[i+1], rating[i+2]))\n        #         cnt += 1\n        \n        \n        \n                \n                \n        \n        # for i in range(len(rating)-1, 1, -1):\n        #     if dic[rating[i]] > dic[rating[i -1]] and dic[rating[i - 1]] > dic[rating[i -2]]:\n        #         print(dic[rating[i]], dic[rating[i - 1]], dic[rating[i - 2]])\n        #         print((rating[i], rating[i-1], rating[i-2]))\n        #         cnt += 1\n                \n        return cnt                                                            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0;\n        for i in range(0,len(rating)):\n            for j in range(1,len(rating)):\n                for k in range(2,len(rating)):\n                    if (j>i and k>j) and ((rating[i]<rating[j] and rating[j]<rating[k]) or (rating[i]>rating[j] and rating[i]>rating[k] and rating[j]>rating[k])):\n                        count=count+1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        target=3\n        self.count=0\n        self.lst=[]\n        def isSolution(candidate_count):\n            if candidate_count==target:\n                return True\n            \n        def isViable(rating,i,candidates,direction):\n            if i==0 or len(candidates)==0:return True\n            \n            if direction==-1 and rating[i]<candidates[-1]:\n                return True\n            if direction==1 and rating[i]>candidates[-1]:\n                return True\n            return False\n            \n            \n        def backtrack(index,rating,candidates,candidate_count,direction):\n            if isSolution(candidate_count):\n                # self.lst.append(list(candidates))\n                self.count+=1\n                return \n             #-1 means decreasing order\n            # for direction in directions:\n            for i in range(index,len(rating)):\n                if isViable(rating,i,candidates,direction):\n                    candidates.append(rating[i])\n                    backtrack(i+1,rating,candidates,candidate_count+1,direction)\n                    candidates.pop()\n        \n        directions=[1,-1]\n        for direction in directions:\n            backtrack(0,rating,[],0,direction)\n        # return len(self.lst)\n        return self.count\n        print((self.count))\n                \n                \n                \n            \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        l = len(rating)\n        for i in range(0, l - 2):\n            right = [x for x in rating[i+1:] if x > rating[i]]\n            res += sum([len([x for x in right[i+1:] if x > right[i]]) for i in range(len(right))])\n            right = [x for x in rating[i+1:] if x < rating[i]]\n            res += sum([len([x for x in right[i+1:] if x < right[i]]) for i in range(len(right))])\n\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        m_counter = 0\n        for index, v1 in enumerate(rating):\n            for jdex, v2 in enumerate(rating[index+1:]):\n                if v1 < v2:\n                    m_counter = m_counter + len([k for k in rating[jdex+index+1:] if k > v2])\n                else:\n                    m_counter = m_counter + len([k for k in rating[jdex+index+1:] if k < v2])\n                print(m_counter)\n        return m_counter", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(len(rating)):\n                #Never need to check last 2\n                #check for 3rd value\n                if j <= i:\n                    continue\n                if rating[i] > rating[j]:\n                    for k in rating[j:]:\n                        if rating[j] > k:\n                            count += 1\n                elif rating[i] < rating[j]:\n                    for k in rating[j:]:\n                        if rating[j] < k:\n                            count += 1\n                           \n        return count\n                        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(len(rating) - 2):\n            for j in range(i + 1, len(rating) - 1):\n                if rating[j] > rating[i]:\n                    cnt += len([k for k in rating[j+1:len(rating)] if k > rating[j]])\n                if rating[j] < rating[i]:\n                    cnt += len([k for k in rating[j+1:len(rating)] if k < rating[j]])\n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i, r_i in enumerate(rating):\n            for j, r_j in enumerate(rating[i+1:], i+1):\n                if r_i == r_j:\n                    continue\n\n                if r_i < r_j:\n                    count += sum(r_j < r_k for r_k in rating[j+1:])\n                else:\n                    count += sum(r_j > r_k for r_k in rating[j+1:])\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if not(rating): return 0\n        \n        asc, desc = 0, 0\n        for index in range(len(rating)):\n            lrd, lri, rld, rli = 0, 0, 0, 0\n            for num in rating[:index]:\n                if num<rating[index]:\n                    lri += 1\n                elif num>rating[index]:\n                    lrd += 1\n            \n            for num in rating[index:]:\n                if num>rating[index]:\n                    rli += 1\n                elif num<rating[index]:\n                    rld += 1            \n            \n            asc += lri * rli\n            desc += lrd * rld\n        \n        return asc+desc\n", "def increasecheck(List,second_index):\n    count=0\n    for i in List[second_index+1:]:\n        if i>List[second_index]:\n            \n            count+=1\n    return count\ndef decreasecheck(List,second_index):\n    count=0\n    for i in List[second_index+1:]:\n        if i<List[second_index]:\n            \n            count+=1\n    return count\nclass Solution:\n    \n    def numTeams(self, rating: List[int]) -> int:\n        n=len(rating)\n        output=0\n        for i in range(n):\n            for j in range(n-1):\n                if i>j:\n                    continue\n                if rating[i]>rating[j]:\n                    \n                    output+=decreasecheck(rating,j)\n                    \n                if rating[i]<rating[j]:\n                    output+=increasecheck(rating,j)\n        return output\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:        \n        num_teams = 0\n        for i, first_rank in enumerate(rating):\n            rest = rating[i+1:]\n            for j, second_rank in enumerate(rest):\n                if second_rank > first_rank:\n                    for k, third_rank in enumerate(rest[j+1:]):\n                        if third_rank > second_rank:\n                            num_teams += 1\n                if second_rank < first_rank:\n                    for k, third_rank in enumerate(rest[j+1:]):\n                        if third_rank < second_rank:\n                            num_teams += 1\n        \n        return num_teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        l = len(rating)\n        res = 0\n        for i,r1 in enumerate(rating[:l-2]):\n            for j in range(i+1,l-1):\n                r2 = rating[j]\n                if r1 > r2:\n                    for k in range(j+1,l):\n                        r3 = rating[k]\n                        if r2 > r3:\n                            res += 1\n                if r1 < r2:\n                    for k in range(j+1,l):\n                        r3 = rating[k]\n                        if r2 < r3:\n                            res += 1\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i,x in enumerate(rating):\n            for j,y in enumerate(rating[i+1:],i+1):\n                if y > x: \n                    #print(f'i: {i}, j: {j}')\n                    cnt += sum(z>y for z in rating[j+1:])           \n                if y < x:\n                    #print(f'i: {i}, j: {j}')\n                    cnt += sum(z<y for z in rating[j+1:])    \n        return cnt\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res = 0\n        for i in range(len(rating)):\n            for j in range(i + 1, len(rating) - 1):\n                if rating[i] < rating[j]:\n                    for k in range (j , len(rating)):\n                        if rating[j] < rating[k]:\n                            res += 1\n                else:\n                    for k in range(j, len(rating)):\n                        if rating[j] > rating[k]:\n                            res +=1\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i + 1, len(rating)):\n                if rating[i] < rating[j]:\n                    for k in range(j + 1, len(rating)):\n                        if rating[j] < rating[k]:\n                            count += 1\n                if rating[i] > rating[j]:\n                    for k in range(j + 1, len(rating)):\n                        if rating[j] > rating[k]:\n                            count += 1\n        return count\n\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i,x in enumerate(rating):\n            for j,y in enumerate(rating[i+1:],i+1):\n                if y > x: \n                    cnt += sum(z>y for z in rating[j+1:])           \n                if y < x:\n                    cnt += sum(z<y for z in rating[j+1:])    \n        return cnt\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        res=0\n        record = []\n        #rec = []\n        for i in range(0,len(rating)):\n            record.append([])\n            for j in range(i+1):\n                record[i].append(None)\n            for j in range(i+1,len(rating)):\n                if rating[j] > rating[i]:\n                    record[i].append(True)\n                elif rating[j] < rating[i]:\n                    record[i].append(False)\n        for i in range(0,len(rating)-1):\n\n            for j in range(i+1,len(rating)):\n                if record[i][j]:\n                    for k in range(j+1,len(rating)):\n                        if record[j][k]:\n                            res += 1\n                            #rec.append((rating[i],rating[j],rating[k]))\n                elif record[i][j]==False:\n                    for k in range(j+1,len(rating)):\n                        if record[j][k]==False:\n                            res += 1\n                            #rec.append((rating[i], rating[j], rating[k]))\n        \n        return res\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = 0\n        a = 0\n        \n        while a < len(rating) - 2:\n            b = a+1\n            while b < len(rating) - 1:\n                c = b+1\n                if rating[a] < rating[b]:\n                    for c in range(c, len(rating)):\n                        if rating[b] < rating[c]:\n                            teams+=1\n                if rating[a] > rating[b]:\n                    for c in range(c, len(rating)):\n                        if rating[b] > rating[c]:\n                            teams+=1\n                b+=1\n            a+=1\n            \n        return teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        up = [0]*n\n        down = [0]*n\n        teams = 0\n        for i in range(n-1, -1, -1):\n            for j in range(i+1, n):\n                if rating[i]<rating[j]:\n                    up[i] +=1\n                    teams += up[j]\n                else:\n                    down[i] +=1\n                    teams+= down[j]\n        return teams", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i in range(len(rating)):\n            for j in range(i+1, len(rating)):\n                if rating[j] > rating[i]:\n                    for k in range(j+1, len(rating)):\n                        if rating[k] > rating[j]:\n                            count += 1\n                elif rating[i] > rating[j]:\n                    for k in range(j+1, len(rating)):\n                        if rating[k] < rating[j]:\n                            count += 1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        # Brute force O(n**3)\n        res = 0\n        for i in range(len(rating) - 2):\n            for j in range(i+1,len(rating) - 1):\n                if rating[j] < rating[i]:\n                    # Check for increasing\n                    for k in range(j+1,len(rating)):\n                        if rating[k] < rating[j]:\n                            res += 1\n                else:\n                    for k in range(j+1,len(rating)):\n                        if rating[k] > rating[j]:\n                            res += 1\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) > 2:\n            count = 0\n            D = {}\n            for i in range(len(rating)):\n                for j in range(i + 1, len(rating)):\n                    if rating[i] < rating[j]:\n                        for k in range(j + 1, len(rating)):\n                            if rating[j] < rating[k]:\n                                count += 1\n                    elif rating[i] > rating[j]:\n                        for k in range(j + 1, len(rating)):\n                            if rating[j] > rating[k]:\n                                count += 1\n            return count\n        return 0", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        # brute force\n        length = len(rating)\n        ans = 0\n\n        for i in range(length-2):\n            temp = rating[i]\n\n            for j in range(i+1, length):\n                if rating[j] > temp:\n                    for k in range(j+1, length):\n                        if rating[k] > rating[j]:\n                            ans += 1\n\n                if rating[j] < temp:\n                    for k in range(j+1, length):\n                        if rating[k] < rating[j]:\n                            ans += 1\n\n        return ans\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        num_teams = 0\n        for i in range(len(rating) - 2):\n            for j in range(i + 1, len(rating)- 1):\n                if rating[i] < rating[j]:\n                    for k in range(j + 1, len(rating)):\n                        if rating[j] < rating[k]:\n                            num_teams += 1\n                elif rating[i] > rating[j]:\n                    for k in range(j + 1, len(rating)):\n                        if rating[j] > rating[k]:\n                            num_teams += 1\n        return num_teams\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(len(rating)-2):\n            for j in range(i+1, len(rating)-1):\n                if(rating[j]>rating[i]):\n                    for k in range(j+1,len(rating)):\n                        if(rating[k]>rating[j]):\n                            count=count+1\n                else:\n                    for k in range(j+1,len(rating)):\n                        if(rating[k]<rating[j]):\n                            count=count+1\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        result=0\n        if len(rating)<3:\n            result=0\n            return result\n        \n        for i in range(1, len(rating)-1):\n            \n            # from small to large\n            for j in range(0,i):\n                if(rating[j]<rating[i]):\n                    for k in range(i+1, len(rating)):\n                        if rating[i]<rating[k]:\n                            result+=1\n            \n            \n            # from large to small\n            for j in range(0,i):\n                if(rating[j]>rating[i]):\n                    for k in range(i+1, len(rating)):\n                        if rating[i]>rating[k]:\n                            result+=1\n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        res = 0\n        \n        for p in range(n - 2):\n            for q in range(p + 1, n - 1):\n                    if rating[p] > rating[q]:\n                        for o in range(q + 1, n):\n                            if rating[q] > rating[o]:\n                                res += 1\n        \n        for p in range(n - 2):\n            for q in range(p + 1, n - 1):\n                    if rating[p] < rating[q]:\n                        for o in range(q + 1, n):\n                            if rating[q] < rating[o]:\n                                res += 1\n        return res\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        result = 0\n        n = len(rating)\n        for i in range(n):\n            for j in range(i+1, n):\n                if rating[j] > rating[i]:\n                    for k in range(j+1, n):\n                        if rating[k] > rating[j]:\n                            result += 1\n        for i in range(n):\n            for j in range(i+1, n):\n                if rating[j] < rating[i]:\n                    for k in range(j+1, n):\n                        if rating[k] < rating[j]:\n                            result += 1\n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cpt = 0\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                if rating[j]>rating[i]:\n                    for k in range(j+1,len(rating)):\n                        if rating[k]>rating[j]:\n                            cpt += 1\n        rating.reverse()\n        for i in range(len(rating)):\n            for j in range(i+1,len(rating)):\n                if rating[j]>rating[i]:\n                    for k in range(j+1,len(rating)):\n                        if rating[k]>rating[j]:\n                            cpt += 1\n        return cpt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        L = len(rating)\n        result = 0\n        \n        for i in range(1, L - 1):\n            pivot = rating[i]\n            loL, hiL, loR, hiR = 0, 0, 0, 0\n            for j in range(i):\n                if rating[j] > pivot:\n                    hiL +=1\n                else:\n                    loL += 1\n            for j in range(i+1, L):\n                if rating[j] > pivot:\n                    hiR +=1\n                else:\n                    loR += 1\n            result += loL * hiR + loR * hiL\n            \n        return result\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        return self.numTeams_brute_force(rating)\n    \n    # O(n^3) time and O(1) space solution\n    def numTeams_brute_force(self, rating):\n        n = 0\n        for i in range(len(rating)):\n            for j in range(i + 1, len(rating)):\n                if rating[j] < rating[i]:\n                    for k in range(j + 1, len(rating)):\n                        if rating[k] < rating[j]:\n                            n += 1\n                else:\n                    for k in range(j + 1, len(rating)):\n                        if rating[k] > rating[j]:\n                            n += 1\n        return  n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n#         lis = []\n#         for i in range(0, len(rating)):\n#             lis.append(i, rating[i])\n            \n#         lis.sort(key = lambda x : x[1])\n        \n        count = 0\n        \n        for i in range(0, len(rating)):\n            for j in range(i+1, len(rating)):\n                if rating[i] >= rating[j]:\n                    continue\n                for k in range(j+1, len(rating)):\n                    if rating[j] < rating[k]:\n                        count += 1\n        \n        for i in range(0, len(rating)):\n            for j in range(i+1, len(rating)):\n                if rating[i] <= rating[j]:\n                    continue\n                for k in range(j+1, len(rating)):\n                    if rating[j] > rating[k]:\n                        count += 1\n        \n        return count\n        \n        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        y, x, ans = 0, 0, 0\n        for i in rating[:-2]:\n            for j in rating[x+1:-1]:\n                for k in rating[y+2:]:\n                    if i < j < k or i > j > k:                  \n                        ans += 1\n                y += 1\n            x += 1\n            y = x\n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        output = 0\n        \n        for k in range(len(rating)-2):\n            for j in range(k+1 , len(rating)-1):\n                if (rating[j] > rating[k]):\n                    for i in range(j+1 , len(rating)):\n                        if (rating[i] > rating[j]):\n                            output += 1\n                if (rating[j] < rating[k]):\n                    for i in range(j+1 , len(rating)):\n                        if (rating[i] < rating[j]):\n                            output += 1\n                            \n        return output", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        import itertools\n        return len([1 for i,j,k in itertools.combinations(rating,3) if i<j<k or i>j>k])", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n = len(rating)\n        ans = 0\n        for i in range(n):\n            for j in range(i+1,n):\n                if rating[j] > rating[i]:\n                    direction = 1\n                else:\n                    direction = 0\n                for k in range(j+1,n):\n                    if direction and rating[k] > rating[j]: \n                        ans += 1\n                    if not direction and rating[k] < rating[j]: \n                        ans += 1\n        return ans\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        less = {}\n        more = {}\n        count = []\n        n = len(rating)\n        for i in range(n):\n            l = []\n            m = []\n            for j in range(i+1,n):\n                if(rating[i]<rating[j]):\n                    l.append(rating[j])\n                if(rating[i]>rating[j]):\n                    m.append(rating[j])\n            if(len(l)>0):\n                less[rating[i]] = l\n            if(len(m)>0):\n                more[rating[i]] = m\n        print((less,more))\n        for i in list(less.keys()):\n            t = less[i]\n            for j in t:\n                if(j in list(less.keys())):\n                        for k in less[j]:\n                            count.append([i,j,k])\n        for i in list(more.keys()):\n            t = more[i]\n            for j in t:\n                if(j in list(more.keys())):\n                        for k in more[j]:\n                            count.append([i,j,k])\n        return(len(count))\n", "class Solution:\n    def __init__(self):\n        self.num_teams = 0\n        \n    def numTeams(self, rating: List[int]) -> int:      \n        for i in range(len(rating)):\n            first_soldier = rating[i]\n            self.compareFirstSoldier(first_soldier, rating[i:])\n        \n        reversed_rating = rating\n        reversed_rating.reverse()\n        for i in range(len(reversed_rating)):\n            first_soldier = reversed_rating[i]\n            self.compareFirstSoldier(first_soldier, reversed_rating[i:])\n            \n        return self.num_teams\n                \n            \n    def compareFirstSoldier(self, soldier_val: int, rating: List[int]) -> None:\n        for j in range(len(rating)):\n            if soldier_val < rating[j]:\n                second_soldier = rating[j]\n                self.compareSecondSoldier(second_soldier, rating[j:])\n    \n    def compareSecondSoldier(self, soldier_val: int, rating: List[int]) -> None:\n        for k in range(len(rating)):\n            if soldier_val < rating[k]:\n                self.num_teams += 1\n        \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = len(rating)\n        if count < 3:\n            return 0\n        result = 0\n        for i in range(count-2):\n            for j in range(i+1, count-1):\n                increasing = rating[j] > rating[i]\n                for k in range(j+1, count):\n                    if increasing and rating[k] > rating[j]:\n                        result += 1\n                    elif not increasing and rating[k] < rating[j]:\n                        result += 1\n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(1, len(rating) - 1):\n            s_l = 0\n            s_r = 0\n            l_l = 0\n            l_r = 0\n            for j in range(0, i):\n                if rating[j] < rating[i]:\n                    s_l += 1\n                elif rating[j] > rating[i]:\n                    l_l += 1\n            for k in range(i+1, len(rating)):\n                if rating[k] > rating[i]:\n                    s_r += 1\n                elif rating[k] < rating[i]:\n                    l_r += 1\n            cnt += s_l * s_r + l_l * l_r\n        return cnt", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        result = 0\n        for i in range(len(rating)):\n            for j in range(i + 1, len(rating)):\n                if rating[j] == rating[i]:\n                    continue\n                if rating[j] < rating[i]:\n                    result += sum(num < rating[j] for num in rating[j+1:])\n                else:\n                    result += sum(num > rating[j] for num in rating[j+1:])\n        return result", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        answer = 0\n        is_increasing = False\n        for i in range(len(rating)-2):\n            for j in range(i+1, len(rating)-1):\n                if rating[i] == rating[j]:\n                    continue\n\n                is_increasing = rating[i] < rating[j]\n                for k in range(j+1, len(rating)):\n                    if is_increasing and rating[j] < rating[k]:\n                        answer += 1\n                    elif not is_increasing and rating[j] > rating[k]:\n                        answer += 1\n\n        return answer", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        teams = 0\n        for i1 in range(len(rating) - 2):\n            for i2 in range(i1 + 1, len(rating) - 1):\n                if rating[i1] < rating[i2]:\n                    for i3 in range(i2 + 1, len(rating)):\n                        teams += rating[i2] < rating[i3]\n                elif rating[i1] > rating[i2]:\n                    for i3 in range(i2 + 1, len(rating)):\n                        teams += rating[i2] > rating[i3]\n        \n        return teams\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        for i, r_i in enumerate(rating):\n            for j, r_j in enumerate(rating[i+1:], i+1):\n                if r_i == r_j:\n                    continue\n\n                if r_i < r_j:\n                    count += sum(r_j < r_k for r_k in rating[j+1:])\n                else:\n                    count += sum(r_j > r_k for r_k in rating[j+1:])\n\n        return count\n", "from functools import lru_cache\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        a = rating\n        @lru_cache()\n        def helper(curr, left):\n            if left == 0: return 1\n            if curr == len(a): return 0\n            \n            return sum([helper(i, left-1) for i in range(curr+1, len(a)) if a[i] > a[curr]] +[0])\n        \n        res = sum([helper(i, 2) for i in range(len(a))]+[0])\n        helper.cache_clear()\n        a = a[::-1]\n        res += sum([helper(i, 2) for i in range(len(a))]+[0])\n        return res", "from functools import lru_cache\n\n@lru_cache\ndef find_ns( rating, i, n, bigger ):\n    res = []\n    while i < len( rating ):\n        if bigger and rating[i] > n or not bigger and rating[i] < n:\n            res.append( i )\n        i += 1\n    return res\n        \n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        rating = tuple( rating )\n        i = 0\n        num_teams = 0\n        while i < len(rating)-2:\n            for i2 in find_ns( rating, i+1, rating[i], True ):\n                for i3 in find_ns( rating, i2+1, rating[i2], True ):\n                    num_teams += 1\n            for i2 in find_ns( rating, i+1, rating[i], False ):\n                for i3 in find_ns( rating, i2+1, rating[i2], False ):\n                    num_teams += 1\n            i += 1\n        return num_teams\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count=0\n        for i in range(0,len(rating)-2):\n            for j in range(i+1,len(rating)):\n                flag=0\n                if rating[j]>rating[i]:\n                    flag=1\n                else:\n                    flag=0\n                for k in range(j+1,len(rating)):\n                    if flag==1:\n                        if rating[k]>rating[j]:\n                            count=count+1\n                    else:\n                        if rating[k]<rating[j]:\n                            count=count+1\n        return count\n                        \n                    \n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        cnt = 0\n        for i in range(len(rating)-2):\n            for j in range(i+1,len(rating)):\n                if rating[i]<rating[j]:\n                    for k in range(j+1,len(rating)):\n                        if rating[j]<rating[k]:\n                            cnt+=1\n                if rating[i]>rating[j]:\n                    for k in range(j+1,len(rating)):\n                        if rating[j]>rating[k]:\n                            cnt+=1\n        return  cnt", "class Solution:\n    def numTeams(self, rating: list) -> int:\n        left_less, left_greater = 0, 0\n        right_less, right_greater = 0, 0\n        count = 0\n        for j in range(len(rating)):\n            for i in range(j):\n                if rating[i] < rating[j]:\n                    left_less += 1\n                elif rating[i] > rating[j]:\n                    left_greater += 1\n            for k in range(j+1, len(rating)):\n                if rating[j] < rating[k]:\n                    right_greater += 1\n                elif rating[j] > rating[k]:\n                    right_less += 1\n            count += left_less * right_greater + left_greater * right_less\n            left_less = left_greater = right_less = right_greater = 0\n        return count", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        ans = 0\n        for i, j, k in combinations(rating, 3):\n            if i < j < k or i > j > k:\n                ans += 1\n        return ans", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        import itertools\n        return len([1 for i,j,k in itertools.combinations(rating,3) if i<j<k or i>j>k])                ", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        n=len(rating)\n        teams=0\n        for i in range(n):\n            for k in range(i+2,n):\n                if rating[i]<rating[k]:\n                    teams+=len([r for r in rating[i+1:k] if rating[i]<r<rating[k]])\n                else:\n                    teams+=len([r for r in rating[i+1:k] if rating[i]>r>rating[k]])\n        \n        # for i in range(n):\n        #     for j in range(i+1,n):\n        #         for k in range(j+1,n):\n        #             if rating[i]<rating[j]<rating[k] or rating[i] > rating[j] > rating[k]:\n        #                 teams+=1\n        return teams\n", "class Solution:\n    def numTeams(self, ratings: List[int]) -> int:\n        ans = 0\n        for i in range(len(ratings)-2):\n            for j in range(i,len(ratings)-1):\n                if ratings[j] > ratings[i]:\n                    for k in range(j,len(ratings)):\n                        if ratings[k] > ratings[j]:\n                            ans += 1\n                if ratings[j] < ratings[i]:\n                    for k in range(j,len(ratings)):\n                        if ratings[k] < ratings[j]:\n                            ans += 1\n        return ans\n", "def look_for_ascending(t_list, sub_array): \n    good_lists = 0\n    for idx in range(0, len(sub_array)):\n        if sub_array[idx] > t_list[-1]:\n            if len(t_list) ==2:\n                good_lists+=1\n            else:\n                good_lists+= look_for_ascending(t_list+[sub_array[idx]], sub_array[idx+1:])\n    \n    return good_lists\n\ndef look_for_descending(t_list, sub_array): \n    good_lists = 0\n    for idx in range(0, len(sub_array)):\n        if sub_array[idx] < t_list[-1]:\n            if len(t_list) ==2:\n                good_lists+=1\n            else:\n                good_lists+= look_for_descending(t_list+[sub_array[idx]], sub_array[idx+1:])\n    \n    return good_lists\n\n\n\nclass Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        good=0\n        for idx in range(len(rating)):\n            good+=look_for_ascending([rating[idx]], rating[idx+1:]) + look_for_descending([rating[idx]], rating[idx+1:])\n        return good", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        count = 0\n        size = len(rating)\n        for idx, val in enumerate(rating):\n            for next in range(idx+1,size):\n                if rating[next] > val:\n                    new = next + 1\n                    while new < size:\n                        if rating[new] > rating[next]:\n                            count += 1\n                        new += 1\n                elif rating[next] < val:\n                    new = next + 1\n                    while new < size:\n                        if rating[new] < rating[next]:\n                            count += 1\n                        new += 1\n        return count\n", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        if len(rating) <= 2:\n            return 0\n        res =0\n        for i in range(len(rating)-2):\n            incs = []\n            decs = []\n            for j in range(i+1,len(rating)-1):\n                if rating[j] > rating[i]:\n                    incs.append(j)\n                elif rating[j] < rating[i]:\n                    decs.append(j)\n            for ind in incs:\n                for j in range(ind+1,len(rating)):\n                    if rating[j] > rating[ind]:\n                        res += 1\n            for ind in decs:\n                for j in range(ind+1,len(rating)):\n                    if rating[j] <rating[ind]:\n                        res += 1\n        return res", "class Solution:\n    def numTeams(self, rating: List[int]) -> int:\n        \n        count = 0\n        n = len(rating)\n        \n        for i in range(n-2):\n            for j in range(i+1, n-1):\n                if rating[i] < rating[j]:\n                    for k in range(j+1, n):\n                        if rating[k] > rating[j]:\n                            count += 1\n                elif rating[i] > rating[j]:\n                    for k in range(j+1, n):\n                        if rating[k] < rating[j]:\n                            count += 1\n        return count"]