#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<ctime>
#include<math.h>
#include<fstream>
#include<stdio.h>
#include<stack>
#include<limits.h>
#include<iomanip>  // cout << setprecision(...) <<  fixed;
using namespace std;

#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define pii pair<int, int>
#define pllll pair<ll, ll>
#define psll pair<string, ll>
#define qi queue<int>
#define qll queue<ll>
#define si set<int>
#define sll set<ll>
#define mulsi multiset<int>
#define mulsll multiset<ll>
#define mulss multiset<string>
#define msi map<string, int>
#define mii map<int, int>
#define ci const int
#define cll const ll
#define cld const long double
#define msii multiset<int> :: iterator
#define mslli multiset<ll> :: iterator
#define sii set<int> :: iterator
#define slli set<ll> :: iterator
#define fi first
#define se second
#define pb push_back

ll gcd(ll a, ll b) {
	while (a != b) {
		if (b > a) {

			b -= a;
		}
		else {
			a -= b;
		}
	}
	return a;
}

ll n = 0, m = 0, t = 0, k = 0, otv = 0, e = 0, tests = 0, r = 0;
const ll maxn = 1 * 1e5 + 3;
multiset<ll> st;
int flag = 0;
ll a[maxn];

int main() {
 	//freopen("network.in", "r", stdin);
 	//freopen("network.out", "w", stdout);
 	ios_base::sync_with_stdio(0);
 	cin.tie(0);
	cout.tie(0);
	string s;
	cin >> t;
	ll y = 0;
	for (int i = 0; i < t; i++) {
		cin >> n;
		y = 0; r = 0;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		if (n == 1) {
			cout << "Yes";
		}
		else if (n == 2) {
			if (a[0] != 0 || a[1] != 0) {
				cout << "Yes";
			}
			else {
				cout << "No";
			}
		}
		else {
			if (n % 2 == 0) {
				k = (n) / 2;
			}
			else {
				k = (n - 1) / 2;
			}
			for (int i = 0; i <= k; i++) {
				if(a[i] >= i){
					y++;
				}
			}
			for (int i = k + 1; i < n; i++) {
				if (a[i] >= (n - i - 1)) {
					y++;
				}
			}
			for (int i = 0; i < k; i++) {
				if (a[i] >= i + 1) {
					r++;
				}
			}
			for (int i = k; i < n; i++) {
				if (a[i] >= (n - i - 1)) {
					r++;
				}
			}
			if (r == n || y == n) {
				cout << "Yes";
			}
			else {
				cout << "No";
			}
		}
		cout << endl;
	}

//	system("pause");
}


#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<math.h>
#include<cstring>
#define INF 1e10
#define MAX 1000006;
using namespace std;
typedef pair<int, int> pii;

int arr[3 * 100000 +5];
int main() {
	int T;
	cin >> T;
	while (T--) {//15000
		int n;
		cin >> n;
		int c = 0;
		int t;
		int d = 0;
		for (int i = 0; i < n; i++) {//300000
			cin >> arr[i];
			if (c == 0 && arr[i] < i) {
				t = arr[i - 1];
				t--;
				c = 1;
			}
			if (d == 1)
				continue;
			if (c == 1) {
				if (t < 0){
					d = 1;
					cout << "NO";
				}
				if (arr[i] < t) {
					t = arr[i];
				}
				t--;
			}
		}
		if (d == 0)
			cout << "YES";
		cout << '\n';
	}
}


#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<math.h>
#include<cstring>
#define INF 1e10
#define MAX 1000006;
using namespace std;
typedef pair<int, int> pii;

int arr[3 * 100000 +5];
int main() {
	int T;
	cin >> T;
	while (T--) {//15000
		int n;
		cin >> n;
		int c = 0;
		int t;
		for (int i = 0; i < n; i++) {//300000
			cin >> arr[i];
			if (c==0&&arr[i] < i) {
				t = arr[i - 1];
				t--;
				c = 1;
			}

			if (c == 1) {
				if (t < 0)
				{
					cout << "NO";
					break;
				}
				if (arr[i] < t) {
					cout << "NO";
					break;
				}
				t--;
			}
			if (i == n - 1)
				cout << "YES";
		}
		cout << '\n';
	}
}


#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<math.h>
#include<cstring>
#define INF 1e10
#define MAX 1000006;
using namespace std;
typedef pair<int, int> pii;

int arr[3 * 100000 +5];
int main() {
	int T;
	cin >> T;
	while (T--) {//15000
		int n;
		cin >> n;
		int c = 0;
		int t;
		for (int i = 0; i < n; i++) {//300000
			cin >> arr[i];
			if (c == 0 && arr[i] < i) {
				t = arr[i - 1];
				t--;
				c = 1;
			}
			if (c == 1) {
				if (t < 0)
				{
					cout << "NO";
					break;
				}
				if (arr[i] < t) {
					t = arr[i];
				}
				t--;
			}
			if (i == n - 1)
				cout << "YES";
		}
		cout << '\n';
	}
}


#include<iostream>
#include<algorithm>
using namespace std;
int a[300010];
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		bool can = 1;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < n; i++) {
			if (a[i] < min(i, n - i - 1)) can = 0;
		}
		if ((n & 1) == 0) {
			if (a[n / 2] == n / 2 - 1 && a[n / 2 - 1] == n / 2 - 1) can = 0;
		}
		if (can) cout << "Yes" << endl;
		else cout << "No" << endl;
	}
}


#include <iostream>
#include<cstring>
#include<vector>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int> arr(n,0);
        for(int i = 0 ;i<n ; i++){
            cin>>arr[i];
        }

        int k = 0;
        int i = 0;
        while(i<n){
            if(arr[i] >= k){
                k++;
            }
            else{
                break;
            }
            i++;
        }

        int j = n-1;
        int p = 0;
        while(j>=0){
            if(arr[j] >= p){
                p++;
            }
            else{
                break;
            }
            j--;
        }

        if(j<=i){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }

    }
    return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n];
        for(int i = 0 ;i<n ; i++){
            cin>>arr[i];
        }

        int k = 0;

        int *dp1 = new int[n];
        int *dp2 = new int[n];
        for(int f = 0 ;f<n ;f++){
            dp1[f] = -1;
            dp2[f] = -1;
        }

        int i = 0;
        while(i<n){
            if(arr[i] >= k){
                dp1[i] = k;
                k++;
            }
            else{
                break;
            }
            i++;
        }

        int p = 0;
        int j = n-1;
        while(j>=0){
            if(arr[j] >= p){
                dp2[j] = p;
                p++;
            }
            else{
                break;
            }
        }
        // cout<<k<<endl;
        if(k  == n || p == n){
            cout<<"yes"<<endl;
            continue;
        }
        else if(k + p + 2 != n){
            cout<<"NO"<<endl;
            continue;
        }

        i = 0;
        j = n-1;
        while(i<=k && j>=p){
            if(dp1[i] > dp2[j]){
                cout<<"yes"<<endl;
                break;
            }
            i++;
            j--;
        }

        // if(i<n){
        //   k = min(k,arr[i]);
        //   i++;
        // }

        // while( i < n && k != 0){
        //     if(arr[i] == 0){
        //         i++;
        //         break;
        //     }
        //     i++;
        // }

        // if(i == n){
        //     cout<<"Yes"<<endl;
        // }
        // else{
        //     cout<<"NO"<<endl;
        // }
    }
    return 0;
}


#include <iostream>
#include<cstring>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n] ={0};
        for(int i = 0 ;i<n ; i++){
            cin>>arr[i];
        }

        int k = 0;

        int dp1[n] = {0};
        int dp2[n] = {0};

        int i = 0;
        while(i<n){
            if(arr[i] >= k){
                dp1[i] = k;
                k++;
            }
            else{
                break;
            }
            i++;
        }

        int p = 0;
        int j = n-1;
        while(j>=0){
            if(arr[j] >= p){
                dp2[j] = p;
                p++;
            }
            else{
                break;
            }
        }
        if(k  == n || p == n){
            cout<<"yes"<<endl;
            continue;
        }
        else if(k + p + 2 != n){
            cout<<"NO"<<endl;
            continue;
        }

        i = 0;
        j = n -1;
        while(i<=k && j>=p){
            if(dp1[i] > dp2[j]){
                cout<<"yes"<<endl;
                break;
            }
            i++;
            j--;
        }

    }
    return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n];
        for(int i = 0 ;i<n ; i++){
            arr[i] = 0;
        }
        for(int i = 0 ;i<n ; i++){
            cin>>arr[i];
        }

        int k = 0;

        int *dp1 = new int[n];
        int *dp2 = new int[n];
        for(int f = 0 ;f<n ;f++){
            dp1[f] = 0;
            dp2[f] = 0;
        }

        int i = 0;
        while(i<n){
            if(arr[i] >= k){
                dp1[i] = k;
                k++;
            }
            else{
                break;
            }
            i++;
        }

        int p = 0;
        int j = n-1;
        while(j>=0){
            if(arr[j] >= p){
                dp2[j] = p;
                p++;
            }
            else{
                break;
            }
        }
        // cout<<k<<endl;
        if(k  == n || p == n){
            cout<<"yes"<<endl;
            continue;
        }
        else if(k + p + 2 != n){
            cout<<"NO"<<endl;
            continue;
        }

        i = 0;
        j = n-1;
        while(i<=k && j>=p){
            if(dp1[i] > dp2[j]){
                cout<<"yes"<<endl;
                break;
            }
            i++;
            j--;
        }

        // if(i<n){
        //   k = min(k,arr[i]);
        //   i++;
        // }

        // while( i < n && k != 0){
        //     if(arr[i] == 0){
        //         i++;
        //         break;
        //     }
        //     i++;
        // }

        // if(i == n){
        //     cout<<"Yes"<<endl;
        // }
        // else{
        //     cout<<"NO"<<endl;
        // }
    }
    return 0;
}


#include <iostream>
#include<string>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n];
        for(int i = 0 ;i<n ; i++){
            cin>>arr[i];
        }

        int k = 0;

        int dp1[n];
        int dp2[n];
        for(int t = 0 ;t<n ;t++){
            dp1[t] = -1;
            dp2[t] = -1;
        }

        int i = 0;
        while(i<n){
            if(arr[i] >= k){
                dp1[i] = k;
                k++;
            }
            else{
                break;
            }
            i++;
        }

        int p = 0;
        int j = n-1;
        while(j>=0){
            if(arr[j] >= p){
                dp2[j] = p;
                p++;
            }
            else{
                break;
            }
        }
        // cout<<k<<endl;
        if(k  == n || p == n){
            cout<<"yes"<<endl;
            continue;
        }
        else if(k + p + 2 != n){
            cout<<"NO"<<endl;
            continue;
        }

        i = 0;
        j = n-1;
        while(i<=k && j>=p){
            if(dp1[i] > dp2[j]){
                cout<<"yes"<<endl;
                break;
            }
            i++;
            j--;
        }

        // if(i<n){
        //   k = min(k,arr[i]);
        //   i++;
        // }

        // while( i < n && k != 0){
        //     if(arr[i] == 0){
        //         i++;
        //         break;
        //     }
        //     i++;
        // }

        // if(i == n){
        //     cout<<"Yes"<<endl;
        // }
        // else{
        //     cout<<"NO"<<endl;
        // }
    }
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0);

    int nbTests; cin >> nbTests;
    while (nbTests--) {
	    int nbElem; cin >> nbElem;
	    vector<int> tab(nbElem);

	    for (int i = 0; i < nbElem; ++i)
		    cin >> tab[i];

	    int prefixEnd = -1, suffixEnd = nbElem;

	    for (int i = 0; i < nbElem; ++i) {
		    if (tab[i] < i) break;
		    prefixEnd = i;
	    }
	    for (int i = nbElem-1; i >= 0; --i) {
		    if (tab[i] < (nbElem-1)-i) break;
		    suffixEnd = i;
	    }

	    if (suffixEnd <= prefixEnd) // Non-empty intersection
		    cout << "Yes\n";
	    else
		    cout << "No\n";
    }
}


#include <iostream>
#include<cstring>
#include<vector>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
       vector<int> arr(n,0);
        for(int i = 0 ;i<n ; i++){
            cin>>arr[i];
        }

        int k = 0;

        vector<int> dp1(n,0);
        vector<int> dp2(n,0);

        int i = 0;
        while(i<n){
            if(arr[i] >= k){
                dp1[i] = k;
                k++;
            }
            else{
                break;
            }
            i++;
        }

        int p = 0;
        int j = n-1;
        while(j>=0){
            if(arr[j] >= p){
                dp2[j] = p;
                p++;
            }
            else{
                break;
            }
        }
        if(k  == n || p == n){
            cout<<"yes"<<endl;
            continue;
        }
        else if(k + p + 2 != n){
            cout<<"NO"<<endl;
            continue;
        }

        i = 0;
        j = n -1;
        while(i<=k && j>=p){
            if(dp1[i] > dp2[j]){
                cout<<"yes"<<endl;
                break;
            }
            i++;
            j--;
        }

    }
    return 0;
}


int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n];
        for(int i = 0 ;i<n ; i++){
            arr[i] = 0;
        }
        for(int i = 0 ;i<n ; i++){
            cin>>arr[i];
        }

        int k = 0;

        int dp1[n];
        int dp2[n];
        for(int f = 0 ;f<n ;f++){
            dp1[f] = 0;
            dp2[f] = 0;
        }

        int i = 0;
        while(i<n){
            if(arr[i] >= k){
                dp1[i] = k;
                k++;
            }
            else{
                break;
            }
            i++;
        }

        int p = 0;
        int j = n-1;
        while(j>=0){
            if(arr[j] >= p){
                dp2[j] = p;
                p++;
            }
            else{
                break;
            }
        }
        // cout<<k<<endl;
        if(k  == n || p == n){
            cout<<"yes"<<endl;
            continue;
        }
        else if(k + p + 2 != n){
            cout<<"NO"<<endl;
            continue;
        }

        i = 0;
        j = n-1;
        while(i<=k && j>=p){
            if(dp1[i] > dp2[j]){
                cout<<"yes"<<endl;
                break;
            }
            i++;
            j--;
        }

        // if(i<n){
        //   k = min(k,arr[i]);
        //   i++;
        // }

        // while( i < n && k != 0){
        //     if(arr[i] == 0){
        //         i++;
        //         break;
        //     }
        //     i++;
        // }

        // if(i == n){
        //     cout<<"Yes"<<endl;
        // }
        // else{
        //     cout<<"NO"<<endl;
        // }
    }
    return 0;
}


#include <iostream>
#include<cstring>
#include<vector>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
       vector<int> arr(n,0);
        for(int i = 0 ;i<n ; i++){
            cin>>arr[i];
        }

        int k = 0;

        vector<int> dp1(n,0);
        vector<int> dp2(n,0);

        int i = 0;
        while(i<n){
            if(arr[i] >= k){
                dp1[i] = k;
                k++;
            }
            else{
                break;
            }
            i++;
        }

        int p = 0;
        int j = n-1;
        while(j>=0){
            if(arr[j] >= p){
                dp2[j] = p;
                p++;
            }
            else{
                break;
            }
        }
        if(k  == n || p == n){
            cout<<"YES"<<endl;
            continue;
        }
        else if(k + p + 2 != n){
            cout<<"NO"<<endl;
            continue;
        }

        i = 0;
        j = n -1;
        while(i<=k && j>=p){
            if(dp1[i] > dp2[j]){
                cout<<"YES"<<endl;
                break;
            }
            i++;
            j--;
        }

    }
    return 0;
}


#include <iostream>
#include<cstring>
#include<vector>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
       vector<int> arr(n,0);
        for(int i = 0 ;i<n ; i++){
            cin>>arr[i];
        }

        int k = 0;

        vector<int> dp1(n,0);
        vector<int> dp2(n,0);

        int i = 0;
        while(i<n){
            if(arr[i] >= k){
                dp1[i] = k;
                k++;
            }
            else{
                break;
            }
            i++;
        }

        int p = 0;
        int j = n-1;
        while(j>=0){
            if(arr[j] >= p){
                dp2[j] = p;
                p++;
            }
            else{
                break;
            }
        }
        if(k  == n || p == n){
            cout<<"YES"<<endl;
            continue;
        }
        else if(k + p + 2 != n){
            cout<<"NO"<<endl;
            continue;
        }

        i = 0;
        j = n -1;
        bool falg = true;
        while(i<=k && j>=p){

            if(dp1[i] > dp2[j]){
                cout<<"YES"<<endl;
                falg = false;
                break;
            }
            i++;
            j--;
        }
         if(falg){
             cout<<"NO"<<endl;
         }

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0);

    int nbTests; cin >> nbTests;
    while (nbTests--) {
	    int nbElem; cin >> nbElem;
	    vector<int> tab(nbElem);

	    for (int i = 0; i < nbElem; ++i)
		    cin >> tab[i];

	    int prefixEnd = -1, suffixEnd = nbElem;

	    for (int i = 0; i < nbElem; ++i) {
		    if (tab[i] < i) break;
		    prefixEnd = i;
	    }
	    for (int i = nbElem-1; i >= 0; --i) {
		    if (tab[i] < (nbElem-1)-i) break;
		    suffixEnd = i;
	    }

	    if (suffixEnd <= prefixEnd) // Non-empty intersection
		    cout << "Yes\n";
	    else
		    cout << "No\n";
    }
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <map>
#include <algorithm>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long LL;
const int maxn= 3e5 + 10;
const int inf = 0x3f3f3f3f;
const LL INF = 9223372036854775807;
const double efs = 1e-9;
const int mod = 1000000007;
int arr[maxn];
int main()
{
    int t;
    scanf("%d", &t);
    int tt = t;
    while(t--) {
        int n;
        scanf("%d", &n);
        int maxx = -inf;
        int k;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &arr[i]);
            if(maxx < arr[i]) {
                maxx = arr[i];
                k = i;
            }
            else if(maxx == arr[i]){
                if(abs(i - (n + 1) / 2) < abs(k - (n + 1) / 2)){
                    k = i;
                }
            }
        }
        int flag = 0;
        int tmp = maxx;
        for(int i = k; i > 0; i--) {
            if(tmp < (i - 1)){
                flag = 1;
                break;
            }
            else{
                if(i - 1 > 0) {
                    if(tmp > arr[i - 1]) {
                        tmp = arr[i - 1];
                    }
                    else{
                        if(tmp > 0)
                        tmp = tmp - 1;
                    }
                }
            }
        }
        tmp = maxx;
        if(!flag)
        for(int i = k; i <= n; i++) {
            if(tmp < (n - i)){
                flag = 1;
                break;
            }
            else{
                if(i + 1 <= n) {
                    if(tmp > arr[i + 1]) {
                        tmp = arr[i + 1];
                    }
                    else{
                        if(tmp > 0)
                        tmp = tmp - 1;
                    }
                }
            }
        }
        if(!flag) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

// We could start with the same idea as E1, but we will quickly realize
// that it does not work.
//
// The conditions we have before was U-L = 0 or 1.  But the problem now is
// that there can be multiple values of m, so it is harder to make sure
// that you choose two end points enclosing at least 1 m, and if there are
// multiple m's in the range then the conditions need to be modified.
//
// But we can modify the idea slightly and see what we get.  If instead
// of just our definitions of U and L being number of elements > and < m,
// We also have M = number of elements == m in an interval.
//
// Then M is the median in this range of L+M+U elements iff
//      L+M >= U        (median is at most m)
// and  U+M > L         (median is at least m)
//
// or   0 < U-L+M <= 2*M
//
// It turns out that it is too hard to keep track of the inequality
// when both sides have variables, so we will only look at U-L+M > 0.
// This will tell us only the number of intervals whose median is at
// least m.  Call this f(m).  If we do this again for f(m+1), the
// answer we want is f(m) - f(m+1).
//
// For f(m) we use a diff array that keeps a partial sum of U-L+M values.
//
// For each diff[i], we want to know how many j < i such that
// 0 < diff[i]-diff[j] (and count 1 more if 0 < diff[i]).
//
// When we see diff[i], we want to know how many j < i such that
// diff[j] < diff[i].  Note that -n <= diff[i] <= n, so we can easily
// use an array freq indexed by the values of diff to see how often each
// value occurs.  If we compute the partial sums of freq, we can easily
// determine the number of values of diff less than a certain value.
// Because we will be updating diff and freq and partial sums as we scan
// from left to right, we will use a fenwick tree to represent freq
// its partial sums.

/*
 * Fenwick Tree
 *
 * Author: Howard Cheng
 * Reference:
 *
 *   Fenwick, P.M. "A New Data Structure for Cumulative Frequency Tables."
 *   Software---Practice and Experience, 24(3), 327-336 (March 1994).
 *
 * This code has been tested on UVa 11525 and 11610.
 *
 * Fenwick trees are data structures that allows the maintainence of
 * cumulative sum tables dynamically.  The following operations
 * are supported:
 *
 * - Initialize the tree from a list of N integers:                 O(N log N)
 *
 * - Read the cumulative sum at index 0 <= k < N:                   O(log k)
 *
 * - Read the entry at index 0 <= k < N:                            O(log N)
 *
 * - Increment/decrement an entry at index 0 <= k < N in the list:  O(log N)
 *
 * - Given a value, find an index such that the cumulative sum at
 *   that position is the value:                                    O(log N)
 *
 * The space usage is at most 2*N for N input entries.
 *
 * NOTE: it is assumed that all entries are non-negative (even after a
 *       decrement operation).
 *
 */

#include <vector>
#include <cassert>

using namespace std;

class FenwickTree
{
public:
  FenwickTree(int n = 0)
    : N(n), tree(n)
  {
    iBM = 1;
    while (iBM < N) {
      iBM *= 2;
    }
    tree.resize(iBM+1);
    fill(tree.begin(), tree.end(), 0);
  }

  // initialize the tree with the given array of values
  FenwickTree(int val[], int n)
    : N(n)
  {
    iBM = 1;
    while (iBM < N) {
      iBM *= 2;
    }

    tree.resize(iBM+1);
    fill(tree.begin(), tree.end(), 0);
    for (int i = 0; i < n; i++) {
      assert(val[i] >= 0);
      incEntry(i, val[i]);
    }
  }

  // increment the entry at position idx by val (use negative val for
  // decrement).  All affected cumulative sums are updated.
  void incEntry(int idx, int val)
  {
    assert(0 <= idx && idx < N);
    if (idx == 0) {
      tree[idx] += val;
    } else {
      do {
	tree[idx] += val;
	idx += idx & (-idx);
      } while (idx < (int)tree.size());
    }
  }

  // return the cumulative sum val[0] + val[1] + ... + val[idx]
  int cumulativeSum(int idx) const
  {
    assert(0 <= idx && idx < (int)tree.size());
    int sum = tree[0];
    while (idx > 0) {
      sum += tree[idx];
      idx &= idx-1;
    }
    return sum;
  }

  // return the entry indexed by idx
  int getEntry(int idx) const
  {
    assert(0 <= idx && idx < N);
    int val, parent;
    val = tree[idx];
    if (idx > 0) {
      parent = idx & (idx-1);
      idx--;
      while (parent != idx) {
	val -= tree[idx];
	idx &= idx-1;
      }
    }
    return val;
  }

  // return the largest index such that the cumulative frequency is
  // what is given, or -1 if it is not found
  //
  int getIndex(int sum) const
  {
    int orig = sum;
    if (sum < tree[0]) return -1;
    sum -= tree[0];

    int idx = 0;
    int bitmask = iBM;

    while (bitmask != 0 && idx < (int)tree.size()-1) {
      int tIdx = idx + bitmask;
      if (sum >= tree[tIdx]) {
	idx = tIdx;
	sum -= tree[tIdx];
      }
      bitmask >>= 1;
    }

    if (sum != 0) {
      return -1;
    }

    idx = min(N-1, idx);
    return (cumulativeSum(idx) == orig) ? idx : -1;
  }

private:
  int N, iBM;
  vector<int> tree;
};

int n;
int a[200000];
int f(int m)
{
  int diff[200000];
  FenwickTree ft(2*n+1);
  long long ans = 0;

  for (int i = 0; i < n; i++) {
    int last = (i == 0) ? 0 : diff[i-1];
    diff[i] = last;
    if (a[i] >= m) diff[i]++;
    else diff[i]--;

    if (diff[i] > 0) {
      // special case: [0,i]
      ans++;
    }
    if (diff[i] > -n) {
      ans += ft.cumulativeSum(diff[i]-1+n);
    }
    ft.incEntry(diff[i]+n, 1);
  }
  return ans;
}

int main()
{
  int m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  cout << f(m) - f(m+1) << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define F(i, a) for (ll i = 0; i < (a); i++)
#define Fd(i, a) for (ll i = (a)-1; i >= 0; i--)
#define Fs(i, a, j) for (ll i = (j); i < (a); i++)
#define deb(x) cout << #x << " " << x << endl;
#define debs(x) cout << #x << " " << x << " ";
#define deba(x) for (auto _i:x) cout << _i << " "; cout << endl;
#define ms(a) memset(a,0,sizeof(a));
#define ll long long
#define mp make_pair
#define eb emplace_back
ll gcd(ll a,ll b){return (b==0)?a:gcd(b,a%b);}
ll power(ll x,ll p){if(p==0)return 1;if(p==1)return x;if(p%2)return x*power(x,p-1);ll y=power(x,p/2);return y*y;}
// long long LCM(long long a, long long b){return (a*b)/gcd(a,b);}

// vector <int > g[200010];

ll arr[200010],n,m;
ll solve2(ll med) {
    vector < ll > g(2*n + 1,0);
    ll sum = n,res=0,cnt=0;
    g[sum]++;
    for (int i=0; i<n; i++) {
        if (arr[i] < med) {
            sum--,cnt -= g[sum];
        }else {
            cnt += g[sum],sum++;
        }
        res += cnt;
        g[sum] ++;
    }
    return res;
}

void solve() {
    cin >> n >> m;
    for (int i=0; i<n; i++) cin >> arr[i];
    cout << solve2(m) - solve2(m+1) << endl;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t; t=1;
    while(t--) solve();
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <math.h>
#include <vector>
#include <algorithm>
#include <stack>
#include <set>
#include <ctime>
#include <queue>
#include <deque>
#include <unordered_map>
#include <map>
#include <random>
#include <cstdio>
#include <string>
#include <cctype>
#define int long long
#define pb push_back
#define eb emplace_back
#define rs resize
#define ld long double
#define vi vector<int>
#define vpii vector<pair<int, int>>
#define ull unsigned long long

using namespace std;

const int VAL = 1e9 + 7;
/*
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("enroll-loops")
#pragma GCC optimize ("fast-math")
#pragma GCC optimize ("unroll-all-loops")
*/

vi a;
int n;

int get(int m) {
    vi s(2 * n + 1);
    int res = 0, sum = n, add = 0;
    s[n] = 1;
    for (int i = 0; i < n; i++) {
        if (a[i] < m) {
            sum--;
            add -= s[sum];
        }
        else {
            add += s[sum];
            sum++;
        }
        s[sum]++;
        res += add;
    }
    return res;
}

void solve() {
    int m;
    cin >> n >> m;
    a.rs(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    cout << get(m) - get(m + 1);
}

signed main() {
    //freopen("olympiad.in", "r", stdin);
    //freopen("olympiad.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(30);
    int t = 1;
    //cin >> t;
    while (t--)
        solve();
    //   system("pause");
}


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ordered_set tree<pair<int,int>, null_type,less<>, rb_tree_tag,tree_order_statistics_node_update>
int n,arr[int(2e5+5)];
long long count(int m){
    ordered_set s;
    int sum=0,id=1;
    long long ans=0;
    s.insert({0,0});
    for (int i = 0; i < n; ++i) {
        sum+=arr[i]>=m;
        sum-=arr[i]<m;
        s.insert({sum,id++});
        ans+=s.order_of_key({sum,0});
    }
    return ans;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int m;
    cin>>n>>m;
    int l=0;
    for (int i = 0; i < n; ++i) {
        cin>>arr[i];
        l+=arr[i]==m;
    }
    cout<<count(m)-count(m+1);
    return 0;
}


/***************************************** START OF TEMPLATE *********************************************/
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include<bits/stdc++.h>

using namespace std;

#define  SPEED          ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(10);
#define  randomINIT     mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define  foo(i,a,n)     for(ll i = (a); i <= n; i++)
#define  frr(i,a,n)     for(ll i = (a); i >= n; i--)
#define  fo(i,n)        for(ll i = 0; i < n; i++)
#define  newl           cout<<"\n";
#define  f              first
#define  s              second
#define  pb             push_back
#define  mp             make_pair
#define  sym(s)         s="#"+s+"#";
#define  sqr(v)         v*v;
#define  all(x)         (x).begin(),(x).end()
#define  alll(x, n)     x+1, x+n+1
#define  mset(x,val)    memset(x,val,sizeof(x))
#define  mset1d(a,n,v)  foo(i,0,n+5)a[i]=v;
#define  mset2d(a,n,m,v)foo(i,0,n+5)foo(j,0,m+5)a[i][j]=v;
#define  INP(a,n)       foo(i,1,n) cin>>a[i];
#define  deb1d(a,n)     foo(i,1,n)cout<<a[i]<<" ";newl;
#define  deb2d(a,n,m)   foo(i,1,n){foo(j,1,m){cout<<a[i][j]<<" ";}newl;}
#define  debm(a)        for(auto it: a)cout<<"{"<<it.f<<","<<it.s<<"}, ";newl;
#define  debp(a,n)      foo(i,1,n)cout<<"{"<<a[i].f<<","<<a[i].s<<"}, ";newl;
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<" ";newl;
#define  deb2(a,b)      cout<<a<<" "<<b;newl;
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c;newl;
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d;newl;
#define  ll             long long
#define  ld             long double
#define  pll            pair<ll,ll>
#define  vll            vector<ll>
#define  vpll           vector<pll>

const ld   PI    =  3.14159265358979323846;
const ll   MOD   =  1e+9+7;
const ll   INF   =  LLONG_MAX;
const int  INFi  =  INT_MAX;
const ll   N     =  1e+6+8;

vll adj[N]; ll vis[N];
ll dx4[]={0,1,0,-1}, dy4[]={1,0,-1,0};

ll test_cases = 1; randomINIT;

/*********************************************************************************************************/
/***************************************** VARIABLE DECLARATIONS *****************************************/
ll n, m, a[N], b[N];

ll solve(ll m) {
    map<ll, ll> p; p[0] = 1;
    ll sum = 0, accum_sum = 0, res = 0;
    foo(i,1,n){
        if (a[i]<m){
            sum--; accum_sum-=p[sum];
        }
        else {
            accum_sum+=p[sum]; sum++;
        }

        res+=accum_sum;
        p[sum]++;
    }

    return res;
}

/***************************************** START OF MAIN FUNCTION ****************************************/
void MAIN(ll tc){
    cin>>n>>m;
    foo(i,1,n) cin>>a[i];
    cout<<solve(m)-solve(m+1);
}

int main(){
    SPEED;

    //cin>>test_cases;
    foo(i,1,test_cases){
    MAIN(i);
    }
}


#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ar array

const int N = 3e5;
int n, m;
int arr[N];
ll bit[2*N];

void read(){
    cin>>n>>m;
    for(int i = 0; i < n; i++) cin>>arr[i];
}

void upd(int i){
    for(; i < 2*N; i += i&-i) bit[i]++;
}

ll sum(int i){
    ll ret = 0;
    for(; i; i -= i&-i) ret += bit[i];
    return ret;
}

ll f(int m){
    mem(bit, 0*1LL);
    int x = 0;
    upd(N);
    ll ans = 0;
    for(int i = 0; i < n; i++){
        if(arr[i] < m) x--;
        else x++;
        ans += sum(x+N-1);
        upd(x+N);
    }
    return ans;
}

void solve(){
    cout << f(m) - f(m+1) << endl;
}

int main()
{
    int i = 0, j = 0, cs = 0, in;
    read();
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=200000+5;
int a[maxn], cnt[maxn*2], n, m;
ll solve(int m) {
    int s = n;
    memset(cnt, 0 , sizeof(cnt));
    cnt[s] = 1;
    ll add = 0, ans = 0;
    for (int i = 1; i <= n; i++) {
        if(a[i] >= m) add += cnt[s], s++;
        else s--, add -= cnt[s];
        cnt[s]++;
        ans += add;
    }
    return ans;
}
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    printf("%lld\n", solve(m) - solve(m+1));
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=100000+5;
int a[maxn], cnt[maxn*2], n, m;
ll solve(int m) {
    int s = n;
    memset(cnt, 0 , sizeof(cnt));
    cnt[s] = 1;
    ll add = 0, ans = 0;
    for (int i = 1; i <= n; i++) {
        if(a[i] >= m) add += cnt[s], s++;
        else s--, add -= cnt[s];
        cnt[s]++;
        ans += add;
    }
    return ans;
}
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    printf("%lld\n", solve(m) - solve(m+1));
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <map>

using namespace std;

#define MAXN 200005

int n, m;

int an[MAXN];

map<int, long long > ml;
map<int, long long > mr;

int main(int argc, char const *argv[]) {

	scanf("%d%d", &n, &m);

	int t = 0;
	for (int i = 0; i < n; ++i) {
		scanf("%d", an + i);
		if (an[i] >= m) {
			t--;
		} else {
			t++;
		}
		mr[t]++;
		// printf("%d\n", t);
	}

	ml[0]++;

	long long ans = 0;
	int tl = 0, tr = 0;
	int c = 0;
	for (int x = 0; x < n; ++x) {

		// if (an[x] == m) {
		// 	tr += 2;
		// 	mr[tr]++;
		// }

		// printf("%d %d\n", tl, tr);
		// for (map<int, long long>::iterator i = ml.begin(); i != ml.end(); ++i) {
		// 	printf("(%d, %lld) ", i->first, i->second);
		// }
		// printf("\n");

		// for (map<int, long long>::iterator i = mr.begin(); i != mr.end(); ++i) {
		// 	printf("(%d, %lld) ", i->first, i->second);
		// }
		// printf("\n");

		if (an[x] == m) {
			c += 2;
			map<int, long long>::iterator j;
			for (map<int, long long>::iterator i = ml.begin(); i != ml.end(); ++i) {
				if ((j = mr.find((i->first) - c)) != mr.end()) {
					ans += ((i->second) * (j->second));
					// printf("find %lld\n", ans);

				}

				if ((j = mr.find((i->first) + 1 - c)) != mr.end()) {
					ans += ((i->second) * (j->second));
					// printf("find %lld\n", ans);
				}
			}

		}
		// printf("find %d\n", ans);

		if (an[x] > m) {
			tl--;
		} else {
			tl++;
		}
		ml[tl]++;

		if (an[x] >= m) {
			tr--;
		} else {
			tr++;
		}
		mr[tr]--;

	}

	printf("%lld\n", ans);

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <map>

using namespace std;

#define MAXN 200005

int n, m;

int an[MAXN];

map<int, long long > ml;
map<int, long long > mr;

int main(int argc, char const *argv[]) {

	scanf("%d%d", &n, &m);

	int t = 0;
	for (int i = 0; i < n; ++i) {
		scanf("%d", an + i);
		if (an[i] >= m) {
			t--;
		} else {
			t++;
		}
		mr[t]++;
		// printf("%d\n", t);
	}

	ml[0]++;

	long long ans = 0;
	int tl = 0, tr = 0;
	int c = 0;
	for (int x = 0; x < n; ++x) {

		// if (an[x] == m) {
		// 	tr += 2;
		// 	mr[tr]++;
		// }

		printf("%d %d\n", tl, tr);
		for (map<int, long long>::iterator i = ml.begin(); i != ml.end(); ++i) {
			printf("(%d, %lld) ", i->first, i->second);
		}
		printf("\n");

		for (map<int, long long>::iterator i = mr.begin(); i != mr.end(); ++i) {
			printf("(%d, %lld) ", i->first, i->second);
		}
		printf("\n");

		if (an[x] == m) {
			c += 2;
			map<int, long long>::iterator j;
			for (map<int, long long>::iterator i = ml.begin(); i != ml.end(); ++i) {
				if ((j = mr.find((i->first) - c)) != mr.end()) {
					ans += ((i->second) * (j->second));
					printf("find %lld\n", ans);

				}

				if ((j = mr.find((i->first) + 1 - c)) != mr.end()) {
					ans += ((i->second) * (j->second));
					printf("find %lld\n", ans);
				}
			}

		}
		// printf("find %d\n", ans);

		if (an[x] > m) {
			tl--;
		} else {
			tl++;
		}
		ml[tl]++;

		if (an[x] >= m) {
			tr--;
		} else {
			tr++;
		}
		mr[tr]--;

	}

	printf("%lld\n", ans);

	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>

using namespace std;
#define MAXN 200005

/**
* 为求得中位数为m的所有组合，可以将问题转化为“求<中位数为大于等于m的数>的所有组合数量 - <中位数为大于等于m+1的数>的所有组合数量”
*
*
* 为求<中位数为大于等于m的数>的所有组合数量，组合共有n^2种，
* 从左到右遍历输入数据，若遇到大于等于m的则+1，否则-1
* 若某个区间[a,b]中+1 -1的和最终大于0(即大于等于m的数多于小于等于m的数)，则[a,b]区间是符合条件的区间，统计这些区间的数目，就是答案
* 但是遍历区间复杂度是n^2，要计算每一个子区间的和值，可以采用类似于前缀数组的思想，
* 但是这样整体的复杂度至少是n^2，数据量n最多是20w，可能会出现TL，
*
*
* 优化：
* 要统计[a,b]大于0出现的次数，也就是统计“[0,b]的值 > [0,a-1]的值”这种情况出现的次数，
*
* 可以采用一次遍历输入数据，假设访问到第c个数，[0,k](k=0,1,2...c)可采用迭代计算，
* 同时用数组或map来保存先前[0,k] (k=0,1,2...c-1)的值出现的次数，
* 将小于[0,c]的值出现的次数相加，所有的c都这样操作，加起来的就是答案
* 为将小于[0,c]的值出现的次数相加，可以采用类似于莫队算法的办法，一次移动一格
*/

int an[MAXN];
int appear[MAXN * 2];//数组开两倍，从中间开始用以满足加减
int n;

long long solve(int m) {
	int count = n;// 使用数组从中间开始
	appear[count]++;
	long long mo = 0;
	long long ans = 0;
	for (int i = 0; i < n; ++i) {
		if (an[i] >= m) {// +1
			mo += appear[count];
			count++;
			appear[count]++;
		} else {// -1
			count--;
			mo -= appear[count];
			appear[count]++;
		}
		ans += mo;
	}

	return ans;
}

int main(int argc, char const *argv[]) {

	int m;

	scanf("%d%d", &n, &m);

	for (int i = 0; i < n; ++i) {
		scanf("%d", an + i);
	}

	long long ans = solve(m);
	memset(appear, 0, sizeof(appear));
	ans -= solve(m + 1);

	printf("%lld\n", ans);

	return 0;
}


// Problem : F. Vicky's Delivery Service
// Contest : Codeforces - Codeforces Round #561 (Div. 2)
// URL : https://codeforces.com/problemset/problem/1166/F
// Memory Limit : 256 MB
// Time Limit : 4000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

/*
 A Submission by $%U%$
 at time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define fef(i,a,b) for(ll i=a;i<=b;i++)
#define rer(i,a,b) for(ll i=b;i>=a;i--)
#define wew while(true)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define nfio cin.tie(0);cout.tie(0)
#define max(x,y) (((x)>(y))?(x):(y))
#define min(x,y) (((x)<(y))?(x):(y))
#define ord(a,b,c) ((a>=b)and(b>=c))
#define MOD (ll(1000000007))
#define MAX 300001
#define mag 320
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define pow2(x) (ll(1)<<x)
#define pii pair<int,int>
#define piii pair<int,pii>
#define For(i,__,___) for(int i=__;i<=___;i++)
#define Rep(i,__,___) for(int i=__;i>=___;i--)
#define ordered_set tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update>
#define endl "\n"
#define bi BigInt
typedef long long ll;
//---------END-------//

int lim, offset;
map<int, int> fa[400 * 1000 + 10];

void add(int x, int y) {
  for (int i = x; i <= lim; i += (i & -i))
    for (int j = y; j <= lim; j += (j & -j)) ++fa[i][j];
}

int query(int x, int y) {
  int sum = 0;
  for (int i = x; i > 0; i -= (i & -i))
    for (int j = y; j > 0; j -= (j & -j)) sum += fa[i][j];
  return sum;
}

int main() {
    fio;

  int n, m;
  cin >> n >> m;
  vector<int> a(n + 1, 0), b(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    a[i] = (x <= m) + a[i - 1];
    b[i] = (x >= m) + b[i - 1];
  }

  offset = n + 3;
  lim = offset + n + 3;

  ll res = 0;
  for (int i = 0; i <= n; ++i) {
    int x = offset + 2 * a[i] - i - 1;
    int y = offset + 2 * b[i] - i - 1;
    res += query(x, y - 1);
    add(x, y);
  }

  cout << res;
}


#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <array>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>

#define ll long long
#define ld long double
#define eps 1e-8
#define MOD 1000000007

#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f

// change if necessary
#define MAXN 200010

using namespace std;

int n, m;
int nums[MAXN];
int med[MAXN];
int medpone[MAXN];

struct bit {
    int n;
    vector<int> b;
    bit(int n) : n(n + 10) {
        b = vector<int>(n + 10);
    }
    void upd(int x, int v) {
        x++;
        while (x && x < n) {
            b[x] += v;
            x += x & -x;
        }
    }
    int qry(int x) {
        int res = 0;
        x++;
        while (x) {
            res += b[x];
            x -= x & -x;
        }
        return res;
    }
};

void calc(int mid, int* arr) {
    vector<int> cnt(n + 1);
    vector<pair<int, int>> pt = {{0, 0}};
    bit b(n + 1);
    for (int i = 0; i < n; i++) {
        cnt[i + 1] += cnt[i];
        if (nums[i] < mid) cnt[i + 1]++;
        pt.push_back({2 * cnt[i + 1] - i - 1, i + 1});
    }
    sort(pt.begin(), pt.end());
    for (auto p : pt) {
        arr[p.second] = b.qry(p.second);
        b.upd(p.second, 1);
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> nums[i];
    }

    calc(m, med);
    calc(m + 1, medpone);

    ll ans = 0;
    for (int i = 0; i <= n; i++) {
        ans += medpone[i] - med[i];
    }
    cout << ans << '\n';

    return 0;
}


/**
 🍪 the_hyp0cr1t3
 🍪 17.09.2020 00:02:49
**/
#ifdef W
#include "k_II.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define aylmao cin.tie(nullptr)->sync_with_stdio(false);
#endif
#define pb emplace_back
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };

const int64_t DESPACITO = 2e18;
const int INF = 2e9, MOD = 1e9+7;
const int N = 2e5 + 5;
int a[N];

int32_t main() {
    aylmao
    int i, n, m, pos, sum, sum3, remove = 0; int64_t ans = 0;
    cin >> n >> m;
    for(i = 1; i <= n; i++) {
        int x; cin >> x;
        a[i] = x == m? pos = i, 0 : x > m? -1 : 1;
    }
    map<int, int> pref;

    for(i = pos, sum = 0; i; i--) {
        sum += a[i];
        pref[sum]++;
    }

    i = pos; sum3 = 0;
    while(i > 0) {
        for(int j = i, sum2 = 0; j <= n; j++) {
            sum2 -= a[j];
            ans += pref[sum2 + remove];
            ans += pref[sum2 - 1 + remove];
            if(j > i and !a[j]) break;
        }
        i--; pref[sum3]--;
        while(i > 0 and a[i]) {
            sum3 += a[i];
            pref[sum3]--;
            remove += a[i--];
        }
    }

    out:
    cout << ans << '\n';
}


/**
 🍪 the_hyp0cr1t3
 🍪 17.09.2020 01:34:15
**/
#ifdef W
#include "k_II.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define aylmao cin.tie(nullptr)->sync_with_stdio(false);
#endif
#define pb emplace_back
#define sz(x) int(x.size())
#define all(x) x.begin(), x.end()
auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class key, class value = null_type, class cmp = std::less<key>>
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define ook order_of_key    // count of elements strictly smaller than k
#define fbo find_by_order   // iterator to kth element starting from 0

const int64_t DESPACITO = 2e18;
const int INF = 2e9, MOD = 1e9+7;
const int N = 2e5 + 5;
int a[N];

int32_t main() {
    aylmao
    int i, n, m;
    cin >> n >> m;
    for(i = 0; i < n; i++)
        cin >> a[i];

    auto get = [&](int x) {
        ordered_set<pair<int, int>> os;
        os.insert({0, INF}); int sum = 0; int64_t ans = 0;
        for(i = 0; i < n; i++) {
            sum += a[i] < x? -1 : 1;
            ans += os.ook({sum, -i});
            os.insert({sum, -i});
        } return ans;
    };

    cout << get(m) - get(m+1) << '\n';
}


#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include<bits/stdc++.h>

using namespace std;

#define  fastio         ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              first
#define  s              second
#define  endl           "\n"
#define  pb             push_back
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";cout<<endl;
#define  deb1(a)        cout<<a<<endl;
#define  deb2(a,b)      cout<<a<<" "<<b<<endl;
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<endl;
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;

typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll,ll>           pll;
typedef  vector<ll>            vll;
typedef  unordered_map<ll,ll>  um;
typedef  vector<pll>           vpll;

const ll   N     =  2e5+5;
const ll   mod   =  998244353;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;

ll test = 1, n, m, a[N];
map <ll,ll> ct;

void solve(){
	int ind = -1;
    cin>>n>>m;
    for(int i = 0; i < n; i++){
    	cin>>a[i];
    }
    ll ans = 0, cnt = 0, sum = 0;
    ct[0] = 1;
    for(int i = 0; i < n; i++){
    	if(a[i] >= m) sum += ct[cnt], cnt++;
    	else cnt--, sum -= ct[cnt];
    	ans += sum;
    	ct[cnt]++;
    }
    ct.clear();
    cnt = 0, sum = 0;
    ct[0] = 1;
    for(int i = 0; i < n; i++){
    	if(a[i] > m) sum += ct[cnt], cnt++;
    	else cnt--, sum -= ct[cnt];
    	ans -= sum;
    	ct[cnt]++;
    }
    deb1(ans)
}

int main(){
    fastio;
    //cin>>test;
    for(int i = 1; i <= test; i++){
        solve();
    }
    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
    #endif
}


#include <iostream>
#include <cstring>
using namespace std;
int a[200005],tree[500005];
inline int lowbit(int x)
{
	return x&(-x);
}
inline void add(int x,int y)
{
	if(x>500000) return ;
	tree[x]+=y,add(x+lowbit(x),y);
}
inline int ask(int x)
{
	if(!x) return x;
	return tree[x]+ask(x-lowbit(x));
}
const int qwqaq=3e5;
int main(int argc, char** argv) {
	int n,m;
	cin >> n >> m;
	for(int i=1;i<=n;i++)
	{
		cin >> a[i];
		if(a[i]==m) a[i]=0;
		else if(a[i]<m) a[i]=-1;
		else a[i]=1;
	}
	long long qwq=0,qaq=0;
	int now=qwqaq;
	add(qwqaq,1);
	for(int i=1;i<=n;i++)
	{
		if(a[i]==-1) --now;
		else ++now;
		qwq+=ask(now-1);
		add(now,1);
	}
	memset(tree,0,sizeof tree);
	now=qwqaq,add(qwqaq,1);
	for(int i=1;i<=n;i++)
	{
		if(a[i]==1) --now;
		else ++now;
		qaq+=ask(now);
		add(now,1);
	}
	cout << qwq+qaq-(long long)n*(n+1)/2;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
//constexpr long long mod = 1000000007LL;
constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

class BIT {
private:
	ll N;
	vector<ll> node;
public:
	BIT(ll M) {
		node = vector<ll>(M + 1, 0);
		N = M;
	}

	ll sum(ll i) {
		ll s = 0;
		while (i > 0) {
			s += node[i];
			i -= i & -i;
		}
		return s;
	}

	ll sum(ll i, ll j) {
		ll l = sum(i - 1);
		ll r = sum(j);
		return r - l;
	}

	void add(ll i, ll x) {
		while (i <= N) {
			node[i] += x;
			i += i & -i;
		}
	}
};

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	int n, m; scanf("%d %d", &n, &m);
	vector<int> a(n); for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	int offset = n + 3;
	ll res = 0;
	{
		BIT bit(n * 2 + 10);
		bit.add(offset, 1);
		int now = offset;
		for (int i = 0; i < n; i++) {
			now += (a[i] >= m ? 1 : -1);
			res += bit.sum(1, now - 1);
			bit.add(now, 1);
		}
	}

	{
		BIT bit(n * 2 + 10);
		bit.add(offset, 1);
		int now = offset;
		for (int i = 0; i < n; i++) {
			now += (a[i] >= m + 1 ? 1 : -1);
			res -= bit.sum(1, now - 1);
			bit.add(now, 1);
		}
	}
	cout << res << endl;
	return 0;

}


#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 62;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos(-1);

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

struct BIT {
	int N;
	vector<ll> node;
	BIT(int M) {
		node = vector<ll>(M + 1, 0);
		N = M;
	}

	ll sum1(int i) {
		ll s = 0;
		while (i > 0) {
			s += node[i];
			i -= i & -i;
		}
		return s;
	}

	ll sum1(int i, int j) {
		ll l = sum1(i - 1);
		ll r = sum1(j);
		return r - l;
	}

	void add1(int i, ll x) {
		while (i <= N) {
			node[i] += x;
			i += i & -i;
		}
	}

	//0-indexed
	//[left, right)
	ll sum(int left, int right) {
		return sum1(left + 1, right);
	}

	void add(int i, ll x) {
		add1(i + 1, x);
	}

	int lower_bound(ll x) {
		int ret = 0;
		int k = 1;
		while (2 * k <= N) k <<= 1;
		for (; k > 0; k >>= 1) {
			if (ret + k <= N and node[ret + k] < x) {
				x -= node[ret + k];
				ret += k;
			}
		}
		return ret;
	}
};

ll suzuken(int m, vector<int> a) {
	int n = a.size();
	for (int i = 0; i < n; i++) if (a[i] >= m) a[i] = 1; else a[i] = -1;
	BIT bit(n * 2 + 1);
	ll res = 0;
	ll cur = n;
	bit.add(cur, 1);
	for (int i = 0; i < n; i++) {
		cur += a[i];
		res += bit.sum(0, cur);
		bit.add(cur, 1);
	}
	return res;
}

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	int n, m; scanf("%d %d", &n, &m);
	vector<int> a(n); for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	cout << suzuken(m, a) - suzuken(m + 1, a) << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353;
constexpr int MAX = 5000000;

template<typename T>
struct BIT {
	int n;
	vector<T> bit;
	BIT() :n(0) {}
	BIT(int _n) :n(_n) { bit = vector<T>(n + 1); }
	void add1(int idx, T val) {
		for (int i = idx; i <= n; i += i & -i) bit[i] += val;
	}
	T sum1(int idx) {
		T res = 0;
		for (int i = idx; i > 0; i -= i & -i) res += bit[i];
		return res;
	}
	//0-indexed
	void add(int idx, T val) { add1(idx + 1, val); }
	//0-indexed [left, right)
	T sum(int left, int right) { return sum1(right) - sum1(left); }

	int lower_bound(T x) {
		int res = 0;
		int k = 1;
		while (2 * k <= n) k <<= 1;
		for (; k > 0; k >>= 1) {
			if (res + k <= n and bit[res + k] < x) {
				x -= bit[res + k];
				res += k;
			}
		}
		return res;
	}
};

ll solve(ll mid, vector<ll> a) {
	int n = a.size();
	for (int i = 0; i < n; i++) if (a[i] >= mid) a[i] = 1; else a[i] = -1;
	ll res = 0;
	BIT<ll> bit(n * 2 + 1);
	ll cur = n;
	bit.add(cur, 1);
	for (int i = 0; i < n; i++) {

		cur += a[i];
		res += bit.sum(0, cur);
		bit.add(cur, 1);
	}
	return res;
}
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int n, m; cin >> n >> m;
	vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i];
	cout << solve(m, a) - solve(m + 1, a) << endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

class BIT {
private:
	int N;
	vector<ll> node;
public:
	BIT(int M) {
		node = vector<ll>(M + 1, 0);
		N = M;
	}

	ll sum1(int i) {
		ll s = 0;
		while (i > 0) {
			s += node[i];
			i -= i & -i;
		}
		return s;
	}

	ll sum1(int i, int j) {
		ll l = sum1(i - 1);
		ll r = sum1(j);
		return r - l;
	}

	void add1(int i, ll x) {
		while (i <= N) {
			node[i] += x;
			i += i & -i;
		}
	}

	//0-indexed
	//[left, right)
	ll sum(int left, int right) {
		return sum1(left + 1 , right);
	}

	void add(int i, ll x) {
		add1(i + 1, x);
	}
};

ll f(ll m, vector<ll>& a) {
	ll sum = 0;
	ll n = a.size();
	ll offset = n + 3;
	BIT bit(n + 5 + offset);
	bit.add(offset, 1);
	ll now = offset;
	ll res = 0;
	for (int i = 0; i < n; i++) {
		now += (a[i] >= m) ? 1 : -1;
		res += bit.sum(0, now);
		bit.add(now, 1);
	}
	return res;
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	ll n, m; scanf("%lld %lld", &n, &m);
	vector<ll> a(n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
	cout << f(m, a) - f(m + 1, a) << "\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
#define endl '\n'
#define maxn 1005
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;

int n;
vector<int> a;
ll solve(int m)
{
  vector<int> v(2 * n + 1);
  int sum = n;
  ll ans = 0;
  v[sum] = 1;
  ll res = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < m) {
      sum--;
      res -= v[sum];
    }
    else {
      res += v[sum];
      sum++;
    }
    ans += res;
    v[sum]++;
  }
  return ans;
}

int main()
{
  FASTIO
  ///*
  //double start_time = clock();
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
//*/
  int T;
  //cin >> T;
  T = 1;
  for (int cs = 1; cs <= T; cs++) {
    int m;
    cin >> n >> m;
    a = vector<int> (n);
    for (int i = 0; i < n; i++) cin >> a[i];
    cout << solve(m) - solve(m + 1) << endl;
  }

  //double end_time = clock();
  //printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
#define endl '\n'
#define maxn 1005
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;

int n;
vector<int> a;
ll solve(int m)
{
  vector<int> v(2 * n + 1);
  int sum = n;
  ll ans = 0;
  v[sum] = 1;
  ll res = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < m) {
      sum--;
      res -= v[sum];
    }
    else {
      res += v[sum];
      sum++;
    }
    ans += res;
    v[sum]++;
  }
  return ans;
}

int main()
{
  FASTIO
  ///*
  //double start_time = clock();
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
//*/
  int T;
  //cin >> T;
  T = 1;
  for (int cs = 1; cs <= T; cs++) {
    int m;
    cin >> n >> m;
    a = vector<int> (n);
    for (int i = 0; i < n; i++) cin >> a[i];
    cout << solve(m) - solve(m + 1) << endl;
    cout << greaterCount(m) - greaterCount(m + 1) << endl;
  }

  //double end_time = clock();
  //printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
#define endl '\n'
#define maxn 1005
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;

int n;
vector<int> a;
ll solve(int m)
{
  vector<int> v(2 * n + 1);
  ll sum = n;
  v[sum] = 1;
  ll res = 0;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < m) {
      sum--;
      res -= v[sum];
    }
    else {
      res += v[sum];
      sum++;
      ans += res;
      v[sum]++;
    }
  }
  return ans;
}

int main()
{
  FASTIO
  ///*
  //double start_time = clock();
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
//*/
  int T;
  //cin >> T;
  T = 1;
  for (int cs = 1; cs <= T; cs++) {
    int m;
    cin >> n >> m;
    a = vector<int> (n);
    for (int i = 0; i < n; i++) cin >> a[i];
    cout << solve(m) - solve(m + 1) << endl;
  }

  //double end_time = clock();
  //printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
#define ll long long
#define pb push_back
#define mp make_pair
#define f(i,a,n) for(int i=a ; i<n ; i++)
#define rf(i,n,a) for(int i=n ; i>=a ; i--)
#define F first
#define S second
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;

const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;

struct BIT {
   int n;
   vector<int> bit;

   BIT(int n) {
      this->n = n;
      bit.assign(n + 1 , 0);
   }

   void add(int idx, int delta) {
      for (; idx < n; idx = idx | (idx + 1))
         bit[idx] += delta;
   }

   int sum(int r) {
      int ret = 0;
      for (; r >= 0; r = (r & (r + 1)) - 1)
         ret += bit[r];
      return ret;
   }

   int sum(int l, int r) {
      return sum(r) - sum(l - 1);
   }
};

void solve() {
   int n, m;
   cin >> n >> m;

   vector<int> v(n);

   for (int &x : v)   cin >> x;

   auto get = [&](int mid) {
      BIT bt(2 * n + 5);

      int sum = n;
      bt.add(n, 1);

      int ans = 0;
      for (int x : v) {
         if (x < mid) {
            sum--;
         }
         else {
            sum++;
         }
         ans += bt.sum(sum - 1);
         bt.add(sum, 1);
      }

      return ans;
   };

   cout << get(m) - get(m + 1) << "\n";
}

int32_t main() {
   fast;
   int t = 1;
   // cin >> t;
   while (t--)  solve();
}


#include <bits/stdc++.h>
#define MAXN 200001
using namespace std;

int n;
int a[MAXN];

long long cntFrom(int m)
{
    long long res=0, sum=0, was=0;
    map <int, int> occ;
    occ[sum] = 1;

    for(int i=0; i<n; ++i)
    {
        if(a[i] >= m)
            was+=occ[sum], sum++;
        else
            sum--, was-=occ[sum];

        res+=was;
        occ[sum]++;
    }
    return res;
}

signed main()
{
    int m;
    cin >> n >> m;

    for(int i=0; i<n; ++i)
        cin >> a[i];

    cout << cntFrom(m) - cntFrom(m+1) << endl;
    return 0;
}


#include <bits/stdc++.h>

#define int long long

using namespace std;

int N, M;

vector<int> vect;

int f(int mid)
{
	int mp[N*2+1];

	for(int i = 0 ; i < N*2+1 ; i++)
	{
		mp[i] = 0;
	}

	int res = 0;

	int acum = N;

	mp[0+N] = 1;

	int sum = 0;

	for(auto x : vect)
	{
		if(x >= mid)
		{
			sum += mp[acum];
			acum++;
		}
		else
		{
			sum -= mp[acum-1];
			acum--;
		}

		res += sum;

		mp[acum]++;
	}

	return res;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N >> M;

    for(int i = 0 ; i < N ; i++)
    {
    	int n;
    	cin >> n;
    	vect.push_back(n);
    }

    cout << f(M)-f(M+1) << '\n';

    return 0;
}


#include <bits/stdc++.h>
#define endl '\n'
#define MAXN 200003
using namespace std;
typedef long long ll;
int n, m;
int arr[MAXN];
void read()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
}
class bit
{
public:
    unordered_map <int, unordered_map <int, int> > tr;
    void add(int a, int b)
    {
        a += 2*n;
        b += 2*n;
        for (int i = a; i < 3*MAXN; i += i & (-i))
            for (int j = b; j < 3*MAXN; j += j & (-j))
                tr[i][j]++;
    }
    int get_prefix(int a, int b)
    {
        a += 2*n;
        b += 2*n;
        int ans = 0;
        for (int i = a; i > 0; i -= i & (-i))
                if (tr.count(i))
                for (int j = b; j > 0; j -= j & (-j))
                        if (tr[i].count(j))
                        ans += tr[i][j];
        return ans;
    }
    int get(int a, int b)
    {
        return get_prefix(a, 3*MAXN-1) - get_prefix(a, b-1);
    }
};
bit even, odd;
void solve()
{
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] < m)
            arr[i] = 0;
        else if (arr[i] == m)
            arr[i] = 1;
        else
            arr[i] = 2;
    }
    int a = 0, b = 0, c = 0;
    even.add(0, 0);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] == 0)
            a++;
        else if (arr[i] == 1)
            b++;
        else
            c++;
        if (i % 2 == 0)
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += even.get(first, second);
            first--;
            ans += odd.get(first, second);
            even.add(a+b-c, a-b-c);
        }
        else
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += odd.get(first, second);
            first--;
            ans += even.get(first, second);
            odd.add(a+b-c, a-b-c);
        }
    }
    cout << ans << endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    read();
    solve();
}


#include <bits/stdc++.h>
#define endl '\n'
#define MAXN 200003
using namespace std;
typedef long long ll;
int n, m;
int arr[MAXN];
void read()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
}
class bit
{
public:
    unordered_map <int, unordered_map <int, int> > tr;
    void add(int a, int b)
    {
        a += n+2;
        b += n+2;
        for (int i = a; i < 3*MAXN; i += i & (-i))
            for (int j = b; j < 3*MAXN; j += j & (-j))
                tr[i][j]++;
    }
    int get_prefix(int a, int b)
    {
        a += n+2;
        b += n+2;
        int ans = 0;
        for (int i = a; i > 0; i -= i & (-i))
                if (tr.count(i))
                for (int j = b; j > 0; j -= j & (-j))
                        if (tr[i].count(j))
                        ans += tr[i][j];
        return ans;
    }
    int get(int a, int b)
    {
        return get_prefix(a, 3*MAXN) - get_prefix(a, b-1);
    }
};
bit even, odd;
void solve()
{
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] < m)
            arr[i] = 0;
        else if (arr[i] == m)
            arr[i] = 1;
        else
            arr[i] = 2;
    }
    int a = 0, b = 0, c = 0;
    even.add(0, 0);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] == 0)
            a++;
        else if (arr[i] == 1)
            b++;
        else
            c++;
        if (i % 2 == 0)
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += even.get(first, second);
            first--;
            ans += odd.get(first, second);
            even.add(a+b-c, a-b-c);
        }
        else
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += odd.get(first, second);
            first--;
            ans += even.get(first, second);
            odd.add(a+b-c, a-b-c);
        }
    }
    cout << ans << endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    read();
    solve();
}


#include <bits/stdc++.h>
#define endl '\n'
#define MAXN 200003
using namespace std;
typedef long long ll;
int n, m;
int arr[MAXN];
void read()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
}
class bit
{
public:
    unordered_map <int, unordered_map <int, int> > tr;
    void add(int a, int b)
    {
        a += n+2;
        b += n+2;
        for (int i = a; i < 2*MAXN+5; i += i & (-i))
            for (int j = b; j < 2*MAXN+5; j += j & (-j))
                tr[i][j]++;
    }
    int get_prefix(int a, int b)
    {
        a += n+2;
        b += n+2;
        int ans = 0;
        for (int i = a; i > 0; i -= i & (-i))
                if (tr.count(i))
                for (int j = b; j > 0; j -= j & (-j))
                        if (tr[i].count(j))
                        ans += tr[i][j];
        return ans;
    }
    int get(int a, int b)
    {
        return get_prefix(a, 2*MAXN+5-1) - get_prefix(a, b-1);
    }
};
bit even, odd;
void solve()
{
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] < m)
            arr[i] = 0;
        else if (arr[i] == m)
            arr[i] = 1;
        else
            arr[i] = 2;
    }
    int a = 0, b = 0, c = 0;
    even.add(0, 0);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] == 0)
            a++;
        else if (arr[i] == 1)
            b++;
        else
            c++;
        if (i % 2 == 0)
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += even.get(first, second);
            first--;
            ans += odd.get(first, second);
            even.add(a+b-c, a-b-c);
        }
        else
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += odd.get(first, second);
            first--;
            ans += even.get(first, second);
            odd.add(a+b-c, a-b-c);
        }
    }
    cout << ans << endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    read();
    solve();
}


#include <bits/stdc++.h>
#define endl '\n'
#define MAXN 200003
using namespace std;
typedef long long ll;
int n, m;
int arr[MAXN];
void read()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
}
class bit
{
public:
    map <int, map <int, int> > tr;
    void add(int a, int b)
    {
        a += MAXN;
        b += MAXN;
        for (int i = a; i < 2*MAXN; i += i & (-i))
            for (int j = b; j < 2*MAXN; j += j & (-j))
                tr[i][j]++;
    }
    int get_prefix(int a, int b)
    {
        a += MAXN;
        b += MAXN;
        int ans = 0;
        for (int i = a; i > 0; i -= i & (-i))
                if (tr.count(i))
                for (int j = b; j > 0; j -= j & (-j))
                        if (tr[i].count(j))
                        ans += tr[i][j];
        return ans;
    }
    int get(int a, int b)
    {
        return get_prefix(a, 2*MAXN-1) - get_prefix(a, b-1);
    }
};
bit even, odd;
void solve()
{
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] < m)
            arr[i] = 0;
        else if (arr[i] == m)
            arr[i] = 1;
        else
            arr[i] = 2;
    }
    int a = 0, b = 0, c = 0;
    even.add(0, 0);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] == 0)
            a++;
        else if (arr[i] == 1)
            b++;
        else
            c++;
        if (i % 2 == 0)
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += even.get(first, second);
            first--;
            ans += odd.get(first, second);
            even.add(a+b-c, a-b-c);
        }
        else
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += odd.get(first, second);
            first--;
            ans += even.get(first, second);
            odd.add(a+b-c, a-b-c);
        }
    }
    cout << ans << endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    read();
    solve();
}


#include <bits/stdc++.h>
#define endl '\n'
#define MAXN 200003
using namespace std;
typedef long long ll;
int n, m;
int arr[MAXN];
void read()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
}
class bit
{
public:
    map <int, map <int, int> > tr;
    void add(int a, int b)
    {
        a += 2*n;
        b += 2*n;
        for (int i = a; i < 3*MAXN; i += i & (-i))
            for (int j = b; j < 3*MAXN; j += j & (-j))
                tr[i][j]++;
    }
    int get_prefix(int a, int b)
    {
        a += 2*n;
        b += 2*n;
        int ans = 0;
        for (int i = a; i > 0; i -= i & (-i))
                if (tr.count(i))
                for (int j = b; j > 0; j -= j & (-j))
                        if (tr[i].count(j))
                        ans += tr[i][j];
        return ans;
    }
    int get(int a, int b)
    {
        return get_prefix(a, 3*MAXN-1) - get_prefix(a, b-1);
    }
};
bit even, odd;
void solve()
{
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] < m)
            arr[i] = 0;
        else if (arr[i] == m)
            arr[i] = 1;
        else
            arr[i] = 2;
    }
    int a = 0, b = 0, c = 0;
    even.add(0, 0);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] == 0)
            a++;
        else if (arr[i] == 1)
            b++;
        else
            c++;
        if (i % 2 == 0)
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += even.get(first, second);
            first--;
            ans += odd.get(first, second);
            even.add(a+b-c, a-b-c);
        }
        else
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += odd.get(first, second);
            first--;
            ans += even.get(first, second);
            odd.add(a+b-c, a-b-c);
        }
    }
    cout << ans << endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    read();
    solve();
}


#include <bits/stdc++.h>
#define endl '\n'
#define MAXN 200003
using namespace std;
typedef long long ll;
int n, m;
int arr[MAXN];
void read()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
}
class bit
{
public:
    map <int, map <int, int> > tr;
    void add(int a, int b)
    {
        a += n+1;
        b += n+1;
        for (int i = a; i < 2*MAXN; i += i & (-i))
            for (int j = b; j < 2*MAXN; j += j & (-j))
                tr[i][j]++;
    }
    int get_prefix(int a, int b)
    {
        a += n+1;
        b += n+1;
        int ans = 0;
        for (int i = a; i > 0; i -= i & (-i))
                if (tr.count(i))
                for (int j = b; j > 0; j -= j & (-j))
                        if (tr[i].count(j))
                        ans += tr[i][j];
        return ans;
    }
    int get(int a, int b)
    {
        return get_prefix(a, 2*MAXN-1) - get_prefix(a, b-1);
    }
};
bit even, odd;
void solve()
{
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] < m)
            arr[i] = 0;
        else if (arr[i] == m)
            arr[i] = 1;
        else
            arr[i] = 2;
    }
    int a = 0, b = 0, c = 0;
    even.add(0, 0);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] == 0)
            a++;
        else if (arr[i] == 1)
            b++;
        else
            c++;
        if (i % 2 == 0)
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += even.get(first, second);
            first--;
            ans += odd.get(first, second);
            even.add(a+b-c, a-b-c);
        }
        else
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += odd.get(first, second);
            first--;
            ans += even.get(first, second);
            odd.add(a+b-c, a-b-c);
        }
    }
    cout << ans << endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    read();
    solve();
}


#include <bits/stdc++.h>
#define endl '\n'
#define MAXN 200003
using namespace std;
typedef long long ll;
int n, m;
int arr[MAXN];
void read()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
}
class bit
{
public:
    unordered_map <int, unordered_map <int, int> > tr;
    void add(int a, int b)
    {
        a += n+2;
        b += n+2;
        for (int i = a; i < 2*MAXN; i += i & (-i))
            for (int j = b; j < 2*MAXN; j += j & (-j))
                tr[i][j]++;
    }
    int get_prefix(int a, int b)
    {
        a += n+2;
        b += n+2;
        int ans = 0;
        for (int i = a; i > 0; i -= i & (-i))
                if (tr.count(i))
                for (int j = b; j > 0; j -= j & (-j))
                        if (tr[i].count(j))
                        ans += tr[i][j];
        return ans;
    }
    int get(int a, int b)
    {
        return get_prefix(a, 2*MAXN-1) - get_prefix(a, b-1);
    }
};
bit even, odd;
void solve()
{
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] < m)
            arr[i] = 0;
        else if (arr[i] == m)
            arr[i] = 1;
        else
            arr[i] = 2;
    }
    int a = 0, b = 0, c = 0;
    even.add(0, 0);
    long long ans = 0;
    for (int i = 1; i <= n; i++)
    {
        if (arr[i] == 0)
            a++;
        else if (arr[i] == 1)
            b++;
        else
            c++;
        if (i % 2 == 0)
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += even.get(first, second);
            first--;
            ans += odd.get(first, second);
            even.add(a+b-c, a-b-c);
        }
        else
        {
            int first = a + b - c;
            int second = a - b - c + 1;
            ans += odd.get(first, second);
            first--;
            ans += even.get(first, second);
            odd.add(a+b-c, a-b-c);
        }
    }
    cout << ans << endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    read();
    solve();
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define mod 1000000007
#define pb emplace_back
#define mp make_pair
#define fi first
#define sec second
typedef long long ll;
typedef long double  ld;
#define pii pair<ll,ll>
#define vi vector< ll >
#define vvi vector< vi >
#define vpi vector< pii >
#define vvpi vector< vpi >
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define test(t) ll t;cin>>t;while(t--)
#define mem(a,b) memset(a,b,sizeof(a))
#define inn freopen("input.txt", "r", stdin)
#define outt freopen("output.txt", "w", stdout)
#define all(arr) arr.begin(),arr.end()
#define fr(i,n) for(ll i=0;i<(n);++i)
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define per(i,a,b) for(ll i=a;i>=b;i--)
#define remin(a,b) (a=min((a),(b)))
#define remax(a,b) (a=max((a),(b)))
#ifndef ONLINE_JUDGE
#define debarr(a,n)cerr<<#a<<":";for(int i=0;i<n;i++)cerr<<a[i]<<" ";cerr<<endl;
#define debmat(mat,row,col)cerr<<#mat<<":\n";for(int i=0;i<row;i++){for(int j=0;j<col;j++)cerr<<mat[i][j]<<" ";cerr<<endl;}
#define pr(...)dbs(#__VA_ARGS__,__VA_ARGS__)
template<class S,class T>ostream &operator<<(ostream &os,const pair<S,T> &p){return os<<"("<<p.first<<","<<p.second<<")";}
template<class T>ostream &operator<<(ostream &os,const vector<T> &p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>ostream &operator<<(ostream &os,const set<T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>ostream &operator<<(ostream &os,const multiset<T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class S,class T>ostream &operator<<(ostream &os,const map<S,T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>void dbs(string str,T t){cerr<<str<<":"<<t<<"\n";}
template<class T,class...S>void dbs(string str,T t,S... s){int idx=str.find(',');cerr<<str.substr(0,idx)<<":"<<t<<",";dbs(str.substr(idx+1),s...);}
#else
#define pr(...){}
#define debarr(a,n){}
#define debmat(mat,row,col){}
#endif
ll power(ll x,ll y){ll res = 1;while(y){if(y&1) res = (res*x)%mod;y=y/2,x=(x*x)%mod;}return res%mod;}
ll BIT[4000001];
ll N;
ll n;
ll arr[200001];
void update(ll x, ll delta)
{
      for(; x <= N; x += x&-x)
        BIT[x] += delta;
}
ll query(ll x)
{
     ll sum = 0;
     for(; x > 0; x -= x&-x)
        sum += BIT[x];
     return sum;
}
ll func(ll m)
{
    ll pref[n+1];
    pref[0]=0;
    fr(i,n){
        if(arr[i]<m)pref[i+1]=-1;
        else pref[i+1]=1;
        pref[i+1]+=pref[i];
    }
    fr(i,N+5)BIT[i]=0;
    update(n+1,1);
    ll ans = 0;
    rep(i,1,n){
        ans+=(query(n+pref[i]));
        update(n+pref[i]+1,1);
    }
    return ans;
}
signed main()
{
    fast;

        ll m;
        cin>>n>>m;
        fr(i,n)cin>>arr[i];
        N = 2*n+5;
        cout<<func(m)-func(m+1)<<"\n";

}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
typedef long long int  ll;
typedef long double  ld;
#define pii pair<ll,ll>
#define pic pair<ll,char>
#define vi vector< ll >
#define vvi vector< vi >
#define vpi vector< pii >
#define vvpi vector< vpi >
#define st  set <ll, greataer <ll> >
#define mst  multiset <ll, greater <ll> >
#define in insert
#define sz size
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mapp  unordered_map <ll, ll>
#define ppii pair< pii, pii >
#define que queue< ll >
#define deq queue< ll >
#define mpit unordered_map<ll, ll> :: iterator
#define mpins(x,y) insert(pair <ll, ll> (x, y))
#define test(t) ll t;cin>>t;while(t--)
#define inarr(n) ll arr[n];for(ll i=0;i<n;i++)cin>>arr[i];
#define ingra(m) vvi arr(100005);for(ll i=0;i<m;i++){ll u,v;cin>>u>>v;arr[u].pb(v);arr[v].pb(u);}
#define mem(a,b) memset(a,b,sizeof(a))
#define inn freopen("input.txt", "r", stdin)
#define outt freopen("output.txt", "w", stdout)
#define all(arr) arr.begin(),arr.end()
vvpi arr(2*100001);
vvi ans(2*100001);
vvi dp(2*100001);
ll level[2*100001];
ll visited[2*100001];
ll parent[2*100001];
ll cnt = 1;
ll oo = 0;
ll n,m,k;
map < ll , ll > lol;
void func(ll u)
{
  if(oo>=cnt)
    return;
  ll i;
  if(u==n+1 and oo<cnt)
  {
    for(i=0;i<m;i++)
    {
      if(lol.find(i)!=lol.end())
        cout<<1;
      else
        cout<<0;
    }
    oo++;
    cout<<"\n";
  }
  for(auto v:ans[u])
  {
    lol[v]++;
    if(oo<cnt){
    func(u+1);
    lol.erase(v);
  }
  else
    return;
  }
}
int main()
{
  fast;

  ll i;
  cin>>n>>m>>k;
  for(i=0;i<m;i++)
  {
    ll u,v;
    cin>>u>>v;
    arr[u].pb({v,i});
    arr[v].pb({u,i});
  }
  queue < ll > q;
  q.push(1);
  visited[1]=1;
  level[1]=0;
  parent[1]=-1;
  while(!q.empty())
  {
    ll u = q.front();
    q.pop();
    for(auto v:arr[u])
    {
      if(visited[v.fi]==1 and parent[u]!=v.fi)
      {
        if(level[v.fi]==level[u]+1)
          ans[v.fi].pb(v.sec);
      }
      if(visited[v.fi]==0)
      {
        level[v.fi]=level[u]+1;
        visited[v.fi]=1;
        ans[v.fi].pb(v.sec);
        q.push(v.fi);
        parent[v.fi]=u;
      }
    }
  }
  // cout<<ans[3].size()<<" ";
  for(i=2;i<=n;i++)
  {
    if(cnt*ans[i].size()>=k)
    {
      cnt = k;
      break;
    }
    else
      cnt = cnt*ans[i].size();

  }
  cout<<cnt<<"\n";
  func(2);

}


#include <bits/stdc++.h>

using namespace std;

#define PI 3.141592653589
#define endl "\n"
#define int long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define fr(i,a,b) for(int i = (a); i < (b); ++i)

#define vi vector<int>
#define vpi vector<pair<int, int>>
const int Nmax = 2e5 + 10;
int a[Nmax];
int z[Nmax];
int cnt[2 * Nmax];
int n;

int greaterCounter(int m){
    fr(i, 0, n){
        z[i] = (a[i] < m ? -1 : +1);
        cnt[2 * i] = 0; cnt[2 * i + 1] = 0;
    }
    int s = 0, sum = n, ans = 0;
    fr(i, 0, n){
        cnt[sum]++;
        if(z[i] == -1) s -= cnt[sum - 1];
        else s += cnt[sum];
        sum += z[i];
        ans += s;
    }
    return ans;
}

void solve(){
    int m;
    cin >> n >> m;
    fr(i, 0, n) cin >> a[i];
    cout << greaterCounter(m) - greaterCounter(m + 1);
}

signed main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0);
    int t;
    t = 1;
    //cin >> t;
    while(t--) {solve();}
    return 0;
}


#include <iostream>
#include <math.h>
#include <algorithm>
//#include <iomanip>
#include <vector>
#include <bits/stdc++.h>

using namespace std;
const int k = 2e5 + 100;
int a[k];
int dp[k][2+2];

int n , m , ans=0;

int main() {

    cin >> n >> m;
    for(int j = 0; j < n; j++) {
        cin >> a[j];
    }

    for(int l = 0; l < n; l++) {
        for(int r = l; r < n; r++) {
            if(l == r)
            {
                if(a[r] == m) {
                    dp[l][1] = 1;
                    ans++;
                }
                else if(a[r] < m) {
                    dp[l][0] = 1;
                }
            }

            else
            {
                if(a[r] == m)
                    dp[l][1]++;

                else if(a[r] < m)
                    dp[l][0]++;

                if(dp[l][0] <= (r-l)/2 && (r-l)/2 < (dp[l][0] + dp[l][1]))
                    ans++;
            }

        }
    }
    cout << ans << endl;

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <functional>
#include <math.h>
#include <map>
#include <set>
#include <string>
using namespace std;
#define fio ios::sync_with_stdio(false); cin.tie(NULL);

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int, int> pii;

const int INT_INF = 1e9;
const ll LL_INF = 1e18;
const int MAXN = 200003;

int n, m;
int A[MAXN];

ll calc(int m) {
    ll ret = 0;

   	map<int, int> dp;

    int sum = 0;
    dp[sum] = 1;
    ll add = 0;
    for(int i=1;i<=n;i++) {
        if (A[i] < m) {
            sum--;

            add -= dp[sum];
        }
        else if(A[i]>=m) {
            add += dp[sum];

            sum++;
        }
        ret += add;
        dp[sum]++;
    }

    return ret;
}

int main() {
	scanf("%d %d", &n, &m);
	// m: median

	for(int i=1,x;i<=n;i++) {
		scanf("%d", &x);
		A[i] = x;
	}

	ll ret1 = calc(m);
	ll ret2 = calc(m+1);
	printf("%lld", ret1-ret2);
	return 0;
}


/*
We will not return to the countless mistakes of the past
I will not relive the failures of the man I once was

Only through dedication will I destroy all preconception

No longer will I let myself be truly satisfied with the standard
We will not return to the countless mistakes of the past

Only through dedication will I destroy all preconception

Let this be
Be the day
Day of my
My declaration
Let this be the day of my declaration

Join me,
Walk with me

Shatter the bonds that tie us
To what we strive to never be

Join me,
Walk with me
*/
#include<bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn=1000001;

ll bit[maxn],a[maxn];
int n,n1,m;

void upd(int idx,int val){
while(idx<=n){
    bit[idx]+=val;
    idx+=(idx&-idx);
   // cout<<1<<endl;
}
}

ll query(int idx){
ll sum=0;
while(idx>0){
    sum+=bit[idx];
    idx-=(idx&-idx);
}
return sum;
}

ll sub_ar(int m){
ll ans=0;
ll sum=n1+1;
memset(bit,0,sizeof(int)*(n+1));
upd(sum,1);

forn(i,n1){
if(a[i]>=m){
    ans+=query(sum);
    //cout<<sum<<endl;
    sum+=1;
    upd(sum,1);
}
else{
    sum-=1;
    ans+=query(sum-1);
    upd(sum,1);
}
}
//forn(i,n) cout<<bit[i]<<endl;
return ans;
}

int main(){

cin>>n1>>m;
n=2*n1+4;
forn(i,n1) cin>>a[i];
cout<<sub_ar(m)-sub_ar(m+1)<<endl;
return 0;

}


/*
We will not return to the countless mistakes of the past
I will not relive the failures of the man I once was

Only through dedication will I destroy all preconception

No longer will I let myself be truly satisfied with the standard
We will not return to the countless mistakes of the past

Only through dedication will I destroy all preconception

Let this be
Be the day
Day of my
My declaration
Let this be the day of my declaration

Join me,
Walk with me

Shatter the bonds that tie us
To what we strive to never be

Join me,
Walk with me
*/
#include<bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn=1000001;

ll bit[maxn],a[maxn];
int n,n1,m;

void upd(int idx,int val){
while(idx<=n){
    bit[idx]+=val;
    idx+=(idx&-idx);
   // cout<<1<<endl;
}
}

ll query(int idx){
ll sum=0;
while(idx>0){
    sum+=bit[idx];
    idx-=(idx&-idx);
}
return sum;
}

ll sub_ar(int m){
ll ans=0;
ll sum=n1+1;
memset(bit,0,sizeof(bit));
upd(sum,1);

forn(i,n1){
if(a[i]>=m){
    ans+=query(sum);
    //cout<<sum<<endl;
    sum+=1;
    upd(sum,1);
}
else{
    sum-=1;
    ans+=query(sum-1);
    upd(sum,1);
}
}
//forn(i,n) cout<<bit[i]<<endl;
return ans;
}

int main(){

cin>>n1>>m;

n=2*n1+4;
forn(i,n1) cin>>a[i];
cout<<sub_ar(m)-sub_ar(m+1)<<endl;

return 0;

}


/*
We will not return to the countless mistakes of the past
I will not relive the failures of the man I once was

Only through dedication will I destroy all preconception

No longer will I let myself be truly satisfied with the standard
We will not return to the countless mistakes of the past

Only through dedication will I destroy all preconception

Let this be
Be the day
Day of my
My declaration
Let this be the day of my declaration

Join me,
Walk with me

Shatter the bonds that tie us
To what we strive to never be

Join me,
Walk with me
*/
#include<bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn=1000001;

ll bit[maxn],a[maxn];
int n,n1,m;

void upd(int idx,int val){
while(idx<=n){
    bit[idx]+=val;
    idx+=(idx&-idx);
   // cout<<1<<endl;
}
}

ll query(int idx){
ll sum=0;
while(idx>0){
    sum+=bit[idx];
    idx-=(idx&-idx);
}
return sum;
}

ll sub_ar(int m){
ll ans=0;
ll sum=n1+1;
memset(bit,0,sizeof(int)*(n+1));
upd(sum,1);

forn(i,n1){
if(a[i]>=m){
    ans+=query(sum);
    //cout<<sum<<endl;
    sum+=1;
    upd(sum,1);
}
else{
    sum-=1;
    ans+=query(sum-1);
    upd(sum,1);
}
}
//forn(i,n) cout<<bit[i]<<endl;
return ans;
}

int main(){

cin>>n1>>m;
bool flag=false;
n=2*n1+4;
forn(i,n1) {cin>>a[i]; if(a[i]==m) flag=true;}
if(!flag) cout<<0<<endl;
else
cout<<sub_ar(m)-sub_ar(m+1)<<endl;
return 0;

}


//this code has been writed by MikeMirzayanov on Codeforces's contest 496 solution
#include <vector>
#include <iostream>
using namespace std;

long long greaterCount(int m,int n, vector<int> a){
    vector<int> s(2*n+1);
    int sum = n;
    long long result = 0;
    s[sum] = 1;
    long long add = 0;
    for(int i = 0; i < n; i++){
        if(a[i] < m){
            sum--, add-=s[sum];
        }
        else{
            add += s[sum], sum++;
        }
        result += add;
        s[sum]++;
    }
    return result;
};

int main(){
    int n,m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    cout << greaterCount(m,n,a) - greaterCount(m+1,n,a) << endl;
    return 0;
}


//this code has been writed by MikeMirzayanov on Codeforces's contest 496 solution
#include <vector>
#include <iostream>
using namespace std;

long long greaterCount(int m){
    vector<int> s(2*n+1);
    int sum = n;
    long long result = 0;
    s[sum] = 1;
    long long add = 0;
    for(int i = 0; i < n; i++){
        if(a[i] < m){
            sum--, add-=s[sum];
        }
        else{
            add += s[sum], sum++;
        }
        result += add;
        s[sum]++;
    }
    return result;
};

int main(){
    int n,m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    cout << greaterCount(m) - greaterCount(m+1) << endl;
    return 0;
}


// Sakhiya07 - Yagnik Sakhiya

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define ld long double

#define pll pair<ll,ll>
#define ff first
#define ss second

#define pb push_back
#define all(x) x.begin(),x.end()

#define MOD  1000000007
const int N = 100005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define os tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>

/*order_of_key (k) : Number of items strictly smaller than k .
find_by_order(k) : K-th element in a set (counting from zero).*/

void solve()
{
    ll n,m;
    cin >> n >> m;
    ll a[n];
    for(ll i=0;i<n;i++)   cin >> a[i];
    auto fun = [&](ll id)
    {
        vector<ll> pre(n+1,0);
        os o;
        for(ll i=1;i<=n;i++)
        {
            ll dmy = 1;
            if(a[i-1]<id)   dmy = -1;
            pre[i] =  pre[i-1] + dmy;
            o.insert({pre[i],i});
        }
        ll ans = 0,temp = 1;
        for(ll i=1;i<=n;i++)
        {
            pll p = {temp , -1};
            ans +=  (o.size() - o.order_of_key(p));
            o.erase(o.find({pre[i],i}));
            temp += pre[i] - pre[i-1];
        }
        return ans;
    };
    ll ans = fun(m) - fun(m+1);
    cout<<ans<<"\n";
}

int main()
{
    FAST;
    int t = 1;
 // cin >> t;
    for(int i=1;i<=t;i++)
    {
        solve();
    }
}