You are given a permutation $p_1, p_2, \dots, p_n$. A permutation of length $n$ is a sequence such that each integer between $1$ and $n$ occurs exactly once in the sequence.

Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of the median of $p_l, p_{l+1}, \dots, p_r$ is exactly the given number $m$.

The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.

For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence.

Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of the median of $p_l, p_{l+1}, \dots, p_r$ is exactly the given number $m$.
The first line contains integers $n$ and $m$ ($1 \le n \le 2\cdot10^5$, $1 \le m \le n$) — the length of the given sequence and the required value of the median.

The second line contains a permutation $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). Each integer between $1$ and $n$ occurs in $p$ exactly once.
5 4
2 4 5 3 1
4
5 5
1 2 3 4 5
1
15 8
1 15 2 14 3 13 4 8 12 5 11 6 10 7 9
48
1 1
1
1
2 1
1 2
2
2 1
2 1
2
2 2
1 2
1
2 2
2 1
1
3 1
1 2 3
2
3 1
1 3 2
2
3 1
2 1 3
3
3 1
2 3 1
2
3 1
3 1 2
3
3 1
3 2 1
2
5 2
2 1 3 4 5
3
6 3
3 6 1 4 2 5
6
7 4
1 7 6 4 2 3 5
10
8 7
2 3 6 8 7 5 4 1
4
9 9
6 4 8 5 1 7 2 9 3
1
10 1
6 9 8 10 4 3 7 1 5 2
3
11 2
9 4 7 5 11 1 3 8 10 2 6
3
12 3
6 4 3 7 8 10 1 2 11 5 12 9
4
14 5
13 14 5 11 3 2 10 8 12 9 6 1 7 4
10
100 1
78 52 95 76 96 49 53 59 77 100 64 11 9 48 15 17 44 46 21 54 39 68 43 4 32 28 73 6 16 62 72 84 65 86 98 75 33 45 25 3 91 82 2 92 63 88 7 50 97 93 14 22 20 42 60 55 80 85 29 34 56 71 83 38 26 47 90 70 51 41 40 31 37 12 35 99 67 94 1 87 57 8 61 19 23 79 36 18 66 74 5 27 81 69 24 58 13 10 89 30
3
100 50
2 4 82 12 47 63 52 91 87 45 53 1 17 25 64 50 9 13 22 54 21 30 43 24 38 33 68 11 41 78 99 23 28 18 58 67 79 10 71 56 49 61 26 29 59 20 90 74 5 75 89 8 39 95 72 42 66 98 44 32 88 35 92 3 97 55 65 51 77 27 81 76 84 69 73 85 19 46 62 100 60 37 7 36 57 6 14 83 40 48 16 70 96 15 31 93 80 86 94 34
182
100 100
70 54 10 72 81 84 56 15 27 19 43 100 49 44 52 33 63 40 95 17 58 2 51 39 22 18 82 1 16 99 32 29 24 94 9 98 5 37 47 14 42 73 41 31 79 64 12 6 53 26 68 67 89 13 90 4 21 93 46 74 75 88 66 57 23 7 25 48 92 62 30 8 50 61 38 87 71 34 97 28 80 11 60 91 3 35 86 96 36 20 59 65 83 45 76 77 78 69 85 55
1
import sys
input = lambda: sys.stdin.readline().rstrip()


from typing import Generic, Iterable, Tuple, TypeVar, Union, List
T = TypeVar("T")


class Node:

  def __init__(self, key, val: int):
    self.key = key
    self.val = val
    self.valsize = val
    self.size = 1
    self.left = None
    self.right = None
    self.balance = 0

  def __str__(self):
    if self.left is None and self.right is None:
      return f'key:{self.key, self.val, self.size, self.valsize}\n'
    return f'key:{self.key, self.val, self.size, self.valsize},\n left:{self.left},\n right:{self.right}\n'


class AVLTreeMultiSet(Generic[T]):

  def __init__(self, a: Iterable[T]=[]) -> None:  
    self.node = None
    if a:
      self._build(a)

  def _rle(self, li: List[T]) -> List[Tuple[T, int]]:
    now = li[0]
    ret = [[now, 1]]
    for i in li[1:]:
      if i == now:
        ret[-1][1] += 1
        continue
      ret.append([i, 1])
      now = i
    return ret
 
  def _build(self, a: Iterable[T]) -> None:
    def sort(l: int, r: int) -> Tuple[Node, int]:
      mid = (l + r) >> 1
      node = Node(a[mid][0], a[mid][1])
      h = 0
      if l != mid:
        left, hl = sort(l, mid)
        node.left = left
        node.size += left.size
        node.valsize += left.valsize
        node.balance = hl
        h = hl
      if mid+1 != r:
        right, hr = sort(mid+1, r)
        node.right = right
        node.size += right.size
        node.valsize += right.valsize
        node.balance -= hr
        if hr > h:
          h = hr
      return node, h+1
    a = self._rle(sorted(a))
    self.node = sort(0, len(a))[0]

  def _rotate_L(self, node: Node) -> Node:
    u = node.left
    u.size = node.size
    u.valsize = node.valsize
    if u.left is None:
      node.size -= 1
      node.valsize -= u.val
    else:
      node.size -= u.left.size + 1
      node.valsize -= u.left.valsize + u.val
    node.left = u.right
    u.right = node
    if u.balance == 1:
      u.balance = 0
      node.balance = 0
    else:
      u.balance = -1
      node.balance = 1
    return u

  def _rotate_R(self, node: Node) -> Node:
    u = node.right
    u.size = node.size
    u.valsize = node.valsize
    if u.right is None:
      node.size -= 1
      node.valsize -= u.val
    else:
      node.size -= u.right.size + 1
      node.valsize -= u.right.valsize + u.val
    node.right = u.left
    u.left = node
    if u.balance == -1:
      u.balance = 0
      node.balance = 0
    else:
      u.balance = 1
      node.balance = -1
    return u

  def _update_balance(self, node: Node) -> None:
    if node.balance == 1:
      node.right.balance = -1
      node.left.balance = 0
    elif node.balance == -1:
      node.right.balance = 0
      node.left.balance = 1
    else:
      node.right.balance = 0
      node.left.balance = 0
    node.balance = 0

  def _rotate_LR(self, node: Node) -> Node:
    B = node.left
    E = B.right
    E.size = node.size
    E.valsize = node.valsize
    if E.right is None:
      node.size -= B.size
      node.valsize -= B.valsize
      B.size -= 1
      B.valsize -= E.val
    else:
      node.size -= B.size - E.right.size
      node.valsize -= B.valsize - E.right.valsize
      B.size -= E.right.size + 1
      B.valsize -= E.right.valsize + E.val
    B.right = E.left
    E.left = B
    node.left = E.right
    E.right = node
    self._update_balance(E)
    return E

  def _rotate_RL(self, node: Node) -> Node:
    C = node.right
    D = C.left
    D.size = node.size
    D.valsize = node.valsize
    if D.left is None:
      node.size -= C.size
      node.valsize -= C.valsize
      C.size -= 1
      C.valsize -= D.val
    else:
      node.size -= C.size - D.left.size
      node.valsize -= C.valsize - D.left.valsize
      C.size -= D.left.size + 1
      C.valsize -= D.left.valsize + D.val
    C.left = D.right
    D.right = C
    node.right = D.left
    D.left = node
    self._update_balance(D)
    return D

  def _kth_elm(self, k: int) -> Tuple[T, int]:
    if k < 0: k += self.__len__()
    node = self.node
    while True:
      t = node.val if node.left is None else node.val + node.left.valsize
      if t-node.val <= k < t:
        return node.key, node.val
      elif t > k:
        node = node.left
      else:
        node = node.right
        k -= t

  def _kth_elm_tree(self, k: int) -> Tuple[T, int]:
    if k < 0:
      k += self.len_elm()
    assert 0 <= k < self.len_elm()
    node = self.node
    while True:
      t = 0 if node.left is None else node.left.size
      if t == k:
        return node.key, node.val
      elif t > k:
        node = node.left
      else:
        node = node.right
        k -= t + 1

  def _discard(self, node: Node, path: List[Node], di: int) -> bool:
    fdi = 0
    if node.left is not None and node.right is not None:
      path.append(node)
      di <<= 1
      di |= 1
      lmax = node.left
      while lmax.right is not None:
        path.append(lmax)
        di <<= 1
        fdi <<= 1
        fdi |= 1
        lmax = lmax.right
      lmax_val = lmax.val
      node.key = lmax.key
      node.val = lmax_val
      node = lmax

    cnode = node.right if node.left is None else node.left
    if path:
      if di & 1:
        path[-1].left = cnode
      else:
        path[-1].right = cnode
    else:
      self.node = cnode
      return True

    while path:
      new_node = None
      pnode = path.pop()
      pnode.balance -= 1 if di & 1 else -1
      pnode.size -= 1
      pnode.valsize -= lmax_val if fdi & 1 else 1
      di >>= 1
      fdi >>= 1

      if pnode.balance == 2:
        new_node = self._rotate_LR(pnode) if pnode.left.balance < 0 else self._rotate_L(pnode)
      elif pnode.balance == -2:
        new_node = self._rotate_RL(pnode) if pnode.right.balance> 0 else self._rotate_R(pnode)
      elif pnode.balance != 0:
        break

      if new_node is not None:
        if not path:
          self.node = new_node
          return    
        if di & 1:
          path[-1].left = new_node
        else:
          path[-1].right = new_node
        if new_node.balance != 0:
          break

    while path:
      pnode = path.pop()
      pnode.size -= 1
      pnode.valsize -= lmax_val if fdi & 1 else 1
      fdi >>= 1

    return True

  def discard(self, key: T, val: int=1) -> bool:
    path = []
    di = 0
    node = self.node
    while node is not None:
      if key == node.key:
        break
      elif key < node.key:
        path.append(node)
        di <<= 1
        di |= 1
        node = node.left
      else:
        path.append(node)
        di <<= 1
        node = node.right
    else:
      return False

    if val > node.val:
      val = node.val - 1
      node.val -= val
      node.valsize -= val
      for p in path:
        p.valsize -= val
    if node.val == 1:
      self._discard(node, path, di)
    else:
      node.val -= val
      node.valsize -= val
      for p in path:
        p.valsize -= val
    return True

  def discard_all(self, key: T) -> None:
    self.discard(key, self.count(key))
    return

  def add(self, key: T, val: int=1) -> None:
    if self.node is None:
      self.node = Node(key, val)
      return
    pnode = self.node
    di = 0
    path = []
    while pnode is not None:
      if key == pnode.key:
        pnode.val += val
        pnode.valsize += val
        for p in path:
          p.valsize += val
        return
      elif key < pnode.key:
        path.append(pnode)
        di <<= 1
        di |= 1
        pnode = pnode.left
      else:
        path.append(pnode)
        di <<= 1
        pnode = pnode.right
    if di & 1:
      path[-1].left = Node(key, val)
    else:
      path[-1].right = Node(key, val)
    new_node = None
    while path:
      pnode = path.pop()
      pnode.size += 1
      pnode.valsize += val
      pnode.balance += 1 if di & 1 else -1
      di >>= 1
      if pnode.balance == 0:
        break
      if pnode.balance == 2:
        new_node = self._rotate_LR(pnode) if pnode.left.balance < 0 else self._rotate_L(pnode)
        break
      elif pnode.balance == -2:
        new_node = self._rotate_RL(pnode) if pnode.right.balance> 0 else self._rotate_R(pnode)
        break
    if new_node is not None:
      if path:
        if di & 1:
          path[-1].left = new_node
        else:
          path[-1].right = new_node
      else:
        self.node = new_node
    for p in path:
      p.size += 1
      p.valsize += val
    return

  def count(self, key: T) -> int:
    node = self.node
    while node is not None:
      if node.key == key:
        return node.val
      elif key < node.key:
        node = node.left
      else:
        node = node.right
    return 0

  '''Find the largest element <= key, or None if it doesn't exist. / O(logN)'''
  def le(self, key: T) -> Union[T, None]:
    res = None
    node = self.node
    while node is not None:
      if key == node.key:
        res = key
        break
      elif key < node.key:
        node = node.left
      else:
        res = node.key
        node = node.right
    return res

  '''Find the largest element < key, or None if it doesn't exist. / O(logN)'''
  def lt(self, key: T) -> Union[T, None]:
    res = None
    node = self.node
    while node is not None:
      if key <= node.key:
        node = node.left
      else:
        res = node.key
        node = node.right
    return res

  '''Find the smallest element >= key, or None if it doesn't exist. / O(logN)'''
  def ge(self, key: T) -> Union[T, None]:
    res = None
    node = self.node
    while node is not None:
      if key == node.key:
        res = key
        break
      elif key < node.key:
        res = node.key
        node = node.left
      else:
        node = node.right
    return res

  '''Find the smallest element > key, or None if it doesn't exist. / O(logN)'''
  def gt(self, key: T) -> Union[T, None]:
    res = None
    node = self.node
    while node is not None:
      if key < node.key:
        res = node.key
        node = node.left
      else:
        node = node.right
    return res

  '''Count the number of elements < key. / O(logN)'''
  def index(self, key: T) -> int:
    k = 0
    node = self.node
    while node is not None:
      if key == node.key:
        if node.left is not None:
          k += node.left.valsize
        break
      elif key < node.key:
        node = node.left
      else:
        k += node.val if node.left is None else node.left.valsize + node.val
        node = node.right
    return k

  '''Count the number of elements <= key. / O(logN)'''
  def index_right(self, key: T) -> int:
    k = 0
    node = self.node
    while node is not None:
      if key == node.key:
        k += node.val if node.left is None else node.left.valsize + node.val
        break
      elif key < node.key:
        node = node.left
      else:
        k += node.val if node.left is None else node.left.valsize + node.val
        node = node.right
    return k

  '''Count the number of keys < key. / O(logN)'''
  def index_keys(self, key: T) -> int:
    k = 0
    node = self.node
    while node:
      if key == node.key:
        if node.left is not None:
          k += node.left.size
        break
      elif key < node.key:
        node = node.left
      else:
        k += node.val if node.left is None else node.left.size + node.val
        node = node.right
    return k

  '''Count the number of keys <= key. / O(logN)'''
  def index_right_keys(self, key: T) -> int:
    k = 0
    node = self.node
    while node:
      if key == node.key:
        k += node.val if node.left is None else node.left.size + node.val
        break
      elif key < node.key:
        node = node.left
      else:
        k += node.val if node.left is None else node.left.size + node.val
        node = node.right
    return k

  def pop(self, k: int=-1) -> T:
    if k < 0: k += self.node.valsize
    node = self.node
    path = []
    if k == self.node.valsize-1:
      while node.right is not None:
        path.append(node)
        node = node.right
      x = node.key
      if node.val == 1:
        self._discard(node, path, 0)
      else:
        node.val -= 1
        node.valsize -= 1
        for p in path:
          p.valsize -= 1
      return x
    di = 0
    while True:
      t = node.val if node.left is None else node.val + node.left.valsize
      if t-node.val <= k < t:
        x = node.key
        break
      elif t > k:
        path.append(node)
        di <<= 1
        di |= 1
        node = node.left
      else:
        path.append(node)
        di <<= 1
        node = node.right
        k -= t
    if node.val == 1:
      self._discard(node, path, di)
    else:
      node.val -= 1
      node.valsize -= 1
      for p in path:
        p.valsize -= 1
    return x

  def popleft(self) -> T:
    node = self.node
    path = []
    while node.left is not None:
      path.append(node)
      node = node.left
    x = node.key
    if node.val == 1:
      self._discard(node, path, (1<<len(path))-1)
    else:
      node.val -= 1
      node.valsize -= 1
      for p in path:
        p.valsize -= 1
    return x

  def items(self):
    for i in range(self.len_elm()):
      yield self._kth_elm_tree(i)

  def keys(self):
    for i in range(self.len_elm()):
      yield self._kth_elm_tree(i)[0]

  def values(self):
    for i in range(self.len_elm()):
      yield self._kth_elm_tree(i)[1]

  def len_elm(self) -> int:
    return 0 if self.node is None else self.node.size

  def show(self) -> None:
    print('{' + ', '.join(map(lambda x: f'{x[0]}: {x[1]}', self.tolist_items())) + '}')

  def get_elm(self, k: int) -> T:
    return self._kth_elm_tree(k)[0]

  def tolist(self) -> List[T]:
    a = []
    if self.node is None:
      return a
    def rec(node):
      if node.left is not None:
        rec(node.left)
      a.extend([node.key]*node.val)
      if node.right is not None:
        rec(node.right)
    rec(self.node)
    return a

  def tolist_items(self) -> List[Tuple[T, int]]:
    a = []
    if self.node is None:
      return a
    def rec(node):
      if node.left is not None:
        rec(node.left)
      a.append((node.key, node.val))
      if node.right is not None:
        rec(node.right)
    rec(self.node)
    return a

  def __getitem__(self, k: int):
    return self._kth_elm(k)[0]

  def __contains__(self, key: T):
    node = self.node
    while node:
      if node.key == key:
        return True
      elif key < node.key:
        node = node.left
      else:
        node = node.right
    return False

  def __iter__(self):
    self.__iter = 0
    return self

  def __next__(self):
    if self.__iter == self.__len__():
      raise StopIteration
    res = self._kth_elm(self.__iter)
    self.__iter += 1
    return res

  def __reversed__(self):
    for i in range(self.__len__()):
      yield self._kth_elm(-i-1)

  def __len__(self):
    return 0 if self.node is None else self.node.valsize

  def __bool__(self):
    return self.node is not None

  def __str__(self):
    return '{' + ', '.join(map(str, self.tolist())) + '}'

  def __repr__(self):
    return 'AVLTreeMultiSet(' + str(self) + ')'


#  -----------------------  #

n, m = map(int, input().split())
P = list(map(int, input().split()))

keep = AVLTreeMultiSet()
mi = P.index(m)

ans = 0
cnt = AVLTreeMultiSet()
Q = P[:mi+1][::-1]
for i, q in enumerate(Q):
  keep.add(q)
  cnt.add(keep.index(m)-(len(keep)-keep.index_right(m)))
keep = AVLTreeMultiSet()
for i in range(mi, n):
  keep.add(P[i])
  a = keep.index(m)
  b = len(keep)-keep.index_right(m)
  ans += cnt.count(b-a)
  ans += cnt.count(b-a-1)
print(ans)
