#include <bits/stdc++.h>
using namespace std;
void input(vector<long long int> &arr) {
  for (int i = 0; i < arr.size(); i++) cin >> arr[i];
}
void output(vector<long long int> &arr) {
  for (int i = 0; i < arr.size(); i++) cout << arr[i] << " ";
  cout << "\n";
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int power(long long int a, long long int b) {
  if (b == 0) return 1;
  if (b == 1) return a;
  long long int smallans = power(a, b / 2);
  long long int myans = (smallans * smallans) % 1000000007;
  if ((b & 1)) myans = (myans * a) % 1000000007;
  return myans;
}
long long int multiply(long long int a, long long int b) {
  long long int ans = ((a % 1000000007) * (b % 1000000007)) % 1000000007;
  return ans;
}
long long int divide(long long int a, long long int b) {
  return multiply(a, power(b, 1000000007 - 2));
}
void manageLazy(vector<long long int> &tree, vector<long long int> &lazy,
                long long int idx) {
  tree[2 * idx + 1] += lazy[idx];
  lazy[2 * idx + 1] += lazy[idx];
  tree[2 * idx + 2] += lazy[idx];
  lazy[2 * idx + 2] += lazy[idx];
  lazy[idx] = 0;
}
void build(vector<long long int> &tree, vector<long long int> &arr,
           long long int left, long long int right, long long int idx) {
  if (left == right) {
    tree[idx] = arr[left];
    return;
  }
  long long int mid = (left + right) / 2;
  build(tree, arr, left, mid, 2 * idx + 1);
  build(tree, arr, mid + 1, right, 2 * idx + 2);
  tree[idx] = min(tree[2 * idx + 1], tree[2 * idx + 2]);
}
void update(vector<long long int> &tree, vector<long long int> &lazy,
            long long int tl, long long int tr, long long int l,
            long long int r, long long int idx, long long int val) {
  if (l > r) return;
  if (l == tl && tr == r) {
    tree[idx] += val;
    lazy[idx] += val;
    return;
  }
  manageLazy(tree, lazy, idx);
  long long int mid = (tl + tr) / 2;
  update(tree, lazy, tl, mid, l, min(r, mid), 2 * idx + 1, val);
  update(tree, lazy, mid + 1, tr, max(l, mid + 1), r, 2 * idx + 2, val);
  tree[idx] = min(tree[2 * idx + 1], tree[2 * idx + 2]);
}
long long int query(vector<long long int> &tree, vector<long long int> &lazy,
                    long long int tl, long long int tr, long long int l,
                    long long int r, long long int idx) {
  if (l > r) return 1e18;
  if (l <= tl && tr <= r) return tree[idx];
  manageLazy(tree, lazy, idx);
  long long int mid = (tl + tr) / 2;
  long long int a = query(tree, lazy, tl, mid, l, min(r, mid), 2 * idx + 1);
  long long int b =
      query(tree, lazy, mid + 1, tr, max(l, mid + 1), r, 2 * idx + 2);
  return min(a, b);
}
void update(vector<long long int> &tree, long long int index,
            long long int val) {
  index++;
  while (index < tree.size()) {
    tree[index] = (tree[index] + val) % 1000000007;
    index += index & (-index);
  }
}
long long int calculate(vector<long long int> &tree, long long int index) {
  long long int sum = 0;
  index++;
  while (index > 0) {
    sum = (sum + tree[index]) % 1000000007;
    index -= index & (-index);
  }
  return sum;
}
long long int findpar(long long int p, vector<long long int> &parent) {
  if (parent[p] == p) return p;
  parent[p] = findpar(parent[p], parent);
  return parent[p];
}
void merge(long long int a, long long int b, vector<long long int> &parent,
           vector<long long int> &size_) {
  a = findpar(a, parent);
  b = findpar(b, parent);
  if (a != b) {
    if (size_[a] < size_[b]) swap(a, b);
    parent[b] = a;
    size_[a] += size_[b];
  }
}
void sieve(vector<bool> &primes, vector<long long int> &count) {
  for (int i = 2; i < primes.size(); i++) {
    if (primes[i]) {
      count.push_back(i);
      for (int j = i * i; j < primes.size(); j += i) primes[j] = false;
    }
  }
}
void solve() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> arr(n, 0);
  input(arr);
  map<long long int, long long int> mp1;
  map<long long int, long long int> mp2;
  long long int idx = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] == m) {
      idx = i;
      break;
    }
  }
  long long int l = 0, r = 0;
  for (int i = idx - 1; i >= 0; i--) {
    if (arr[i] > m)
      r += 1;
    else
      l += 1;
    mp1[l - r] += 1;
  }
  l = 0, r = 0;
  for (int i = idx + 1; i < n; i++) {
    if (arr[i] > m)
      r += 1;
    else
      l += 1;
    mp2[l - r] += 1;
  }
  long long int ans = 0;
  mp1[0] += 1;
  mp2[0] += 1;
  for (auto keys : mp1) {
    long long int num = keys.first;
    long long int val = keys.second;
    ans += val * mp2[-1 * num];
    ans += val * mp2[-1 * num - 1];
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}

### Prompt

Please create a solution in Cpp to the following problem:

You are given a permutation p_1, p_2, ..., 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 ≤ l ≤ r ≤ n) such that the value of the median of p_l, p_{l+1}, ..., 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 ≤ l ≤ r ≤ n) such that the value of the median of p_l, p_{l+1}, ..., p_r is exactly the given number m.

Input

The first line contains integers n and m (1 ≤ n ≤ 2⋅10^5, 1 ≤ m ≤ n) — the length of the given sequence and the required value of the median.

The second line contains a permutation p_1, p_2, ..., p_n (1 ≤ p_i ≤ n). Each integer between 1 and n occurs in p exactly once.

Output

Print the required number.

Examples

Input

5 4
2 4 5 3 1


Output

4


Input

5 5
1 2 3 4 5


Output

1


Input

15 8
1 15 2 14 3 13 4 8 12 5 11 6 10 7 9


Output

48

Note

In the first example, the suitable pairs of indices are: (1, 3), (2, 2), (2, 3) and (2, 4).

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
void input(vector<long long int> &arr) {
  for (int i = 0; i < arr.size(); i++) cin >> arr[i];
}
void output(vector<long long int> &arr) {
  for (int i = 0; i < arr.size(); i++) cout << arr[i] << " ";
  cout << "\n";
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int power(long long int a, long long int b) {
  if (b == 0) return 1;
  if (b == 1) return a;
  long long int smallans = power(a, b / 2);
  long long int myans = (smallans * smallans) % 1000000007;
  if ((b & 1)) myans = (myans * a) % 1000000007;
  return myans;
}
long long int multiply(long long int a, long long int b) {
  long long int ans = ((a % 1000000007) * (b % 1000000007)) % 1000000007;
  return ans;
}
long long int divide(long long int a, long long int b) {
  return multiply(a, power(b, 1000000007 - 2));
}
void manageLazy(vector<long long int> &tree, vector<long long int> &lazy,
                long long int idx) {
  tree[2 * idx + 1] += lazy[idx];
  lazy[2 * idx + 1] += lazy[idx];
  tree[2 * idx + 2] += lazy[idx];
  lazy[2 * idx + 2] += lazy[idx];
  lazy[idx] = 0;
}
void build(vector<long long int> &tree, vector<long long int> &arr,
           long long int left, long long int right, long long int idx) {
  if (left == right) {
    tree[idx] = arr[left];
    return;
  }
  long long int mid = (left + right) / 2;
  build(tree, arr, left, mid, 2 * idx + 1);
  build(tree, arr, mid + 1, right, 2 * idx + 2);
  tree[idx] = min(tree[2 * idx + 1], tree[2 * idx + 2]);
}
void update(vector<long long int> &tree, vector<long long int> &lazy,
            long long int tl, long long int tr, long long int l,
            long long int r, long long int idx, long long int val) {
  if (l > r) return;
  if (l == tl && tr == r) {
    tree[idx] += val;
    lazy[idx] += val;
    return;
  }
  manageLazy(tree, lazy, idx);
  long long int mid = (tl + tr) / 2;
  update(tree, lazy, tl, mid, l, min(r, mid), 2 * idx + 1, val);
  update(tree, lazy, mid + 1, tr, max(l, mid + 1), r, 2 * idx + 2, val);
  tree[idx] = min(tree[2 * idx + 1], tree[2 * idx + 2]);
}
long long int query(vector<long long int> &tree, vector<long long int> &lazy,
                    long long int tl, long long int tr, long long int l,
                    long long int r, long long int idx) {
  if (l > r) return 1e18;
  if (l <= tl && tr <= r) return tree[idx];
  manageLazy(tree, lazy, idx);
  long long int mid = (tl + tr) / 2;
  long long int a = query(tree, lazy, tl, mid, l, min(r, mid), 2 * idx + 1);
  long long int b =
      query(tree, lazy, mid + 1, tr, max(l, mid + 1), r, 2 * idx + 2);
  return min(a, b);
}
void update(vector<long long int> &tree, long long int index,
            long long int val) {
  index++;
  while (index < tree.size()) {
    tree[index] = (tree[index] + val) % 1000000007;
    index += index & (-index);
  }
}
long long int calculate(vector<long long int> &tree, long long int index) {
  long long int sum = 0;
  index++;
  while (index > 0) {
    sum = (sum + tree[index]) % 1000000007;
    index -= index & (-index);
  }
  return sum;
}
long long int findpar(long long int p, vector<long long int> &parent) {
  if (parent[p] == p) return p;
  parent[p] = findpar(parent[p], parent);
  return parent[p];
}
void merge(long long int a, long long int b, vector<long long int> &parent,
           vector<long long int> &size_) {
  a = findpar(a, parent);
  b = findpar(b, parent);
  if (a != b) {
    if (size_[a] < size_[b]) swap(a, b);
    parent[b] = a;
    size_[a] += size_[b];
  }
}
void sieve(vector<bool> &primes, vector<long long int> &count) {
  for (int i = 2; i < primes.size(); i++) {
    if (primes[i]) {
      count.push_back(i);
      for (int j = i * i; j < primes.size(); j += i) primes[j] = false;
    }
  }
}
void solve() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> arr(n, 0);
  input(arr);
  map<long long int, long long int> mp1;
  map<long long int, long long int> mp2;
  long long int idx = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] == m) {
      idx = i;
      break;
    }
  }
  long long int l = 0, r = 0;
  for (int i = idx - 1; i >= 0; i--) {
    if (arr[i] > m)
      r += 1;
    else
      l += 1;
    mp1[l - r] += 1;
  }
  l = 0, r = 0;
  for (int i = idx + 1; i < n; i++) {
    if (arr[i] > m)
      r += 1;
    else
      l += 1;
    mp2[l - r] += 1;
  }
  long long int ans = 0;
  mp1[0] += 1;
  mp2[0] += 1;
  for (auto keys : mp1) {
    long long int num = keys.first;
    long long int val = keys.second;
    ans += val * mp2[-1 * num];
    ans += val * mp2[-1 * num - 1];
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}

```