def main(arr, k):
    # max element is k
    # x_harmless_element anchor harmless_element_y
    
    x = 0
    y = 0
    anchor = 0
    ans = 0
    
    n = len(arr)
    
    for i in range(n):
        # 1. THE WALL
        if arr[i] > k:
            if anchor != 0:
                ans += (x + y + 1 + x * y)
            x = 0
            y = 0
            anchor = 0
            
        else:
            # 2. THE ANCHOR
            if arr[i] == k:
                if anchor == 1: # Shift the old anchor
                    ans += (x + y + 1 + x * y)
                    x = x + 1 + y
                    y = 0
                else: # First anchor lock-in
                    anchor = 1
                    
            # 3. THE HARMLESS PADDING
            elif anchor == 0: # <-- Fixed 'else if' to 'elif'
                x += 1
            else:
                y += 1
    
    # 4. FINAL TALLY (Protected!)
    if anchor != 0: # <-- Added protection check
        ans += (x + y + 1 + x * y)
        
    return ans