#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#define rint register int
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
typedef long long LL;
typedef vector<int>::iterator vit;
const int maxn=100003;
inline void read(int &x) {
	x=0;int f=1;char ch=getchar();
	while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)) x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	x*=f;
}
struct qj {int l,r;} b[maxn];
int n,k,a[maxn],sum[maxn<<2|1];
bool tag[maxn<<2|1];
vector<int> l[maxn],r[maxn];
inline void update(int rt) {
	sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
inline void color(int rt,int l,int r) {
	tag[rt]^=1;
	sum[rt]=r-l+1-sum[rt];
}
inline void push_down(int rt,int l,int r) {
	if(tag[rt]) {
		int mid=l+r>>1;
		color(lson);
		color(rson);
		tag[rt]=0;
	}
}
void modify(int rt,int l,int r,int ml,int mr) {
	if(l>=ml&&r<=mr) {
		color(rt,l,r);
		return;
	}
	push_down(rt,l,r);
	int mid=l+r>>1;
	if(ml<=mid) modify(lson,ml,mr);
	if(mr>mid) modify(rson,ml,mr);
	update(rt);
}
int query(int rt,int l,int r,int ql,int qr) {
	if(l>=ql&&r<=qr) return sum[rt];
	push_down(rt,l,r);
	int mid=l+r>>1;
	if(qr<=mid) return query(lson,ql,qr);
	if(ql>mid) return query(rson,ql,qr);
	return query(lson,ql,qr)+query(rson,ql,qr);
}
int main() {
	read(n),read(k);
	for(rint i=1;i<=n;++i) read(a[i]);
	sort(a+1,a+n+1);
	for(rint i=1;i<=k;++i) {
		read(b[i].l),read(b[i].r);
		b[i].l=lower_bound(a+1,a+n+1,b[i].l)-a;
		b[i].r=upper_bound(a+1,a+n+1,b[i].r)-a-1;
		l[b[i].l].push_back(i),r[b[i].r].push_back(i);
	}
	LL ans=(LL)n*(n-1)*(n-2)/6;
	for(rint i=1;i<=n;++i) {
		int du=0;
		if(!l[i].empty()) for(vit it=l[i].begin();it!=l[i].end();++it) modify(1,1,n,b[*it].l,b[*it].r);
		if(!r[i-1].empty()) for(vit it=r[i-1].begin();it!=r[i-1].end();++it) modify(1,1,n,b[*it].l,b[*it].r);
		if(i>1) du+=i-1-query(1,1,n,1,i-1);
		if(i<n) du+=query(1,1,n,i+1,n);
		ans-=(LL)du*(du-1)/2;
	}
	cout<<ans;
	return 0;
}


#include <cstdio>
#include <algorithm>

const int MAXN = 1e4 + 7;

typedef long long LL;

void Qread(int &x) {
    x = 0; register char ch = getchar(); register bool k = false;
    while (ch < '0' || ch > '9') { if (ch == '-') k = true; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    if (k) x = -x;
}

int n, k;
int a[MAXN];
int e[MAXN][MAXN];
LL ans;

int main() {
    // freopen("fight.in", "r", stdin);
    // freopen("fight.out", "w", s  tdout);

    Qread(n), Qread(k);
    for (int i = 1; i <= n; i++) {
        Qread(a[i]);
    }

    std::sort(a+1, a+n+1);
    for (int i = n; i >= 1; i--)
        for (int j = i - 1; j >= 1; j--) {
            e[i][j] = 1;
        }

    int l, r;
    for (int i = 1; i <= k; i++) {
        Qread(l), Qread(r);
        for (int i = 1; i <= n; i++)
            for (int j = i + 1; j <= n; j++) {
                if (a[i] >= l && a[i] <= r && a[j] >= l && a[j] <= r) e[i][j] ^= 1, e[j][i] ^= 1;
            }
    }

    for (int i = 1; i <= n; i++)
        for (int j = i + 1; j <= n; j++)
            for (int m = j + 1; m <= n; m++) {
                if (e[i][j] && e[j][m] && e[m][i]) ans++;
            }

    printf("%lld\n", ans);
    return 0;
}

/*
3 2
1 2 3
1 2
2 3
*/


#include<iostream>
#include<cstring>
#include<algorithm>
#define N 100010
using namespace std;
struct D{
	long long l,r;
}a[N],b[N];
long long L[N*4],R[N*4],len[N*4],tag[N*4],val[N*4],s[N];
long long n,m;
long long ans;
inline void swap(long long &a,long long &b){
    long long t=a;
    a=b;
    b=t;
}
void build(long long x,long long l,long long r){
    len[x]=r-l+1;
    L[x]=l,R[x]=r;
    if(l>=r)
        return;
    long long mid=(l+r)>>1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
}
void pushdown(long long x){
    if(!tag[x])
        return;
    tag[x<<1]^=1;
    val[x<<1]=len[x<<1]-val[x<<1];
    tag[x<<1|1]^=1;
    val[x<<1|1]=len[x<<1|1]-val[x<<1|1];
    tag[x]=0;
}
void modify(long long x,long long l,long long r){
    if(L[x]>=l&&R[x]<=r){
        val[x]=len[x]-val[x];
        tag[x]^=1;
        return;
    }
    pushdown(x);
    long long mid=(L[x]+R[x])>>1;
    if(l<=mid)
        modify(x<<1,l,r);
    if(r>mid)
        modify(x<<1|1,l,r);
    val[x]=val[x<<1]+val[x<<1|1];
}
long long query(long long x,long long l,long long r){
    if(L[x]>=l&&R[x]<=r)
        return val[x];
    pushdown(x);
    long long res=0;
    long long mid=(L[x]+R[x])>>1;
    if(l<=mid)
        res+=query(x<<1,l,r);
    if(r>mid)
        res+=query(x<<1|1,l,r);
    return res;
}
bool cmp(D x,D y){
	return x.l<y.l;
}
bool cmp2(D x,D y){
	return x.r<y.r;
}
long long main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>m;
    ans=(1LL*n*(n-1)*(n-2))/6;
    build(1,1,n);
    long long t;
    for(long long i=1;i<=n;i++)
    	cin>>s[i];
    sort(s+1,s+n+1);
    for(long long i=1;i<=m;i++){
    	long long x,y;
    	cin>>x>>y;
    	x=lower_bound(s+1,s+n+1,x)-s;
    	y=upper_bound(s+1,s+n+1,y)-s-1;
    //	printf("Debug 1: %d %d\n",x,y);
    	if(x<=y){
	    	a[i].l=b[i].l=x;
	    	a[i].r=b[i].r=y;
    	}
	}
	sort(a+1,a+m+1,cmp);
	sort(b+1,b+m+1,cmp2);
	long long p=0,q=0;
	for(long long i=1;i<=n;i++){
		long long d=0;
	//	printf("I Move to %d\n",i);
		while(p<m&&a[p+1].l<=i){
			p++;
			modify(1,a[p].l,a[p].r);
			//printf("P Move to %d. Modify %d %d\n",p,a[p].l,a[p].r);
		}
		while(q<m&&b[q+1].r<i){
			q++;
			modify(1,b[q].l,b[q].r);
			//printf("Q Move to %d. Modify %d %d\n",q,a[q].l,a[q].r);
		}
		if(i>1)
			d+=(i-1)-query(1,1,i-1);
		if(i<n)
			d+=query(1,i+1,n);
	//	printf("D: %d\n",d);
		ans-=(1LL*d*(d-1))/2;
		//printf("Current Ans: %d\n",ans);
	}
	printf("%lld",ans);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
using namespace std;
const long long maxn=1e5+9;
long long n,k;
int a[maxn];
long long ans[maxn<<2],tag[maxn<<2];
long long ls(long long p){return p<<1;}
long long rs(long long p){return p<<1|1;}
long long push_up(long long p){ans[p]=ans[ls(p)]+ans[rs(p)];}
void f(ll p,ll l,ll r)
{
	tag[p]^=1;
	ans[p]=r-l+1-ans[p];
}
void push_down(ll p,ll l, ll r)
{
	if(tag[p] && l!=r)
	{
		ll mid=l+r>>1;
		f(ls(p),l,mid);
		f(rs(p),mid+1,r);
		tag[p]=0;
	}
}
void update(ll nl,ll nr,ll l,ll r,ll p)
{
	if(nl<=l&&nr>=r){f(p,l,r);return;}
	ll mid=l+r>>1;
	push_down(p,l,r);
	if(nl<=mid) update(nl,nr,l,mid,ls(p));
	if(nr>mid) update(nl,nr,mid+1,r,rs(p));
	push_up(p);
}
ll query(ll nl,ll nr,ll l,ll r,ll p)
{
	ll res=0;
	if(nl<=l&&nr>=r){return ans[p];}
	ll mid=l+r>>1;
	push_down(p,l,r);
	if(nl<=mid)res+=query(nl,nr,l,mid,ls(p));
	if(nr>mid) res+=query(nl,nr,mid+1,r,rs(p));
	return res;
}

int l,r;
vector<long long>vl[maxn],vr[maxn];
int main()
{
	cin>>n>>k;
	long long tot=1l*n*(n-1)*(n-2)/6;
	for(long long i=0;i<n;i++)scanf("%d",&a[i]);// cin>>a[i];
	sort(a,a+n);
	while(k--)
	{
//		cin>>l>>r;
		scanf("%d%d",&l,&r);
		l=lower_bound(a,a+n,l)-a;
		r=upper_bound(a,a+n,r)-a-1;
		if(l>r)continue;
		vl[l].push_back(r);
		vr[r].push_back(l);
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<(int)vl[i].size();j++)update(i,vl[i][j],0,n-1,1);
		long long res=0;
		if(i)res+=i-query(0,i-1,0,n-1,1);
		if(i<n-1)res+=query(i+1,n-1,0,n-1,1);
//		cout<<res<<endl;
		tot-=res*(res-1)/2;
		for(int j=0;j<(int)vr[i].size();j++)update(vr[i][j],i,0,n-1,1);
	}
	cout<<tot<<endl;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#define int long long
using namespace std;
vector<int> zyh1[1000005],zyh2[1000005];
int n,k,s[1000005],len[1000005],sum[1000005],lazy[1000005];
long long ans;
void build(int now,int l,int r){
	len[now]=sum[now]=r-l+1;
	if(l==r) return;
	int mid=(l+r)>>1;
	build(now<<1,l,mid);
	build(now<<1|1,mid+1,r);
}

void fff(int now){
	sum[now]=len[now]-sum[now],lazy[now]^=1;
}

void ffff(int now){
	if(!lazy[now]) return;
	fff(now<<1),fff(now<<1|1);
	lazy[now]=0;
}

void rev(int now,int l,int r,int ql,int qr){
	if(r<ql||l>qr) return;
	if(ql<=l&&r<=qr){
		fff(now);
		return;
	}
	int mid=(l+r)>>1;ffff(now);
	rev(now<<1,l,mid,ql,qr),rev(now<<1|1,mid+1,r,ql,qr);
	sum[now]=sum[now<<1]+sum[now<<1|1];
}

signed main(){
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;i++) scanf("%lld",&s[i]);
	sort(s+1,s+n+1);
	for(int i=1;i<=k;i++){
		int x,y;scanf("%lld%lld",&x,&y);
		x=lower_bound(s+1,s+n+1,x)-s;
		y=upper_bound(s+1,s+n+1,y)-s-1;
		zyh2[x].push_back(y),zyh1[y+1].push_back(x);
	}
	build(1,1,n);
	int zyh=0;ans=1ll*n*(n-1)*(n-2)/6;
	for(int i=1;i<=n;i++){
		rev(1,1,n,i,i);
		for(int j=0;j<zyh1[i].size();j++){
			rev(1,1,n,zyh1[i][j],i-1),zyh^=1;
		}
		for(int j=0;j<zyh2[i].size();j++){
			rev(1,1,n,i,zyh2[i][j]),zyh^=1;
		}
		int tmp=sum[1]-zyh;
		ans-=1ll*tmp*(tmp-1)/2;
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define lp long long
using namespace std;
const lp ml=1e6+10;
struct wxz{
	lp l,r,len;
	lp sum,add;
}t[ml];
lp n,m;
lp a[ml],b[ml];
lp ans;
lp inline read(){
   lp r=0,f=1;
   char c=getchar();
   while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
   while(c>='0'&&c<='9')r=(r<<1)+(r<<3)+(c^48),c=getchar();
   return r*f;
}
vector < lp > a1[ml],a2[ml];
void spread(lp bh){
	if(t[bh].add==1){
		t[bh].add=0;
		t[bh<<1].sum=t[bh<<1].len-t[bh<<1].sum;
		t[bh<<1|1].sum=t[bh<<1|1].len-t[bh<<1|1].sum;
		t[bh<<1].add^=1;
		t[bh<<1|1].add^=1;
	}
}
void build(lp l,lp r,lp bh){
	t[bh].l=l,t[bh].r=r;
	if(l==r){t[bh].len=t[bh].r-t[bh].l+1;return;}
	lp mid=(l+r)>>1;
	build(l,mid,bh<<1);
	build(mid+1,r,bh<<1|1);
}
void change(lp l,lp r,lp bh){
	if(l<=t[bh].l&&r>=t[bh].r){t[bh].add^=1;t[bh].sum=t[bh].len-t[bh].sum;return;}
	lp mid=(t[bh].l+t[bh].r)>>1;
	spread(bh);
	if(l<=mid)change(l,r,bh<<1);
	if(r>mid)change(l,r,bh<<1|1);
	t[bh].sum=t[bh<<1].sum+t[bh<<1|1].sum;
}
lp ask(lp l,lp r,lp bh){
	if(l<=t[bh].l&&r>=t[bh].r)return t[bh].sum;
	lp mid=(t[bh].l+t[bh].r)>>1,res=0;
	spread(bh);
	if(l<=mid)res+=ask(l,r,bh<<1);
	if(r>mid)res+=ask(l,r,bh<<1|1);
	return res;
}
int main()
{
	n=read(),m=read();
	for(lp i=1;i<=n;i++)a[i]=read(),b[i]=a[i];
	sort(b+1,b+1+n);
	for(lp i=1;i<=m;i++){
		lp x=read(),y=read();
		lp k1=lower_bound(b+1,b+1+n,x)-b;
		lp k2=upper_bound(b+1,b+1+n,y)-b-1;
		a1[k1].push_back(k2);
		a2[k2+1].push_back(k1);
	}
	ans=n*(n-1)*(n-2)/6;
	build(1,n,1);
	for(lp i=1;i<=n;i++){
		for(lp j=0;j<a1[i].size();j++)change(i,a1[i][j],1);
		for(lp j=0;j<a2[i].size();j++)change(a2[i][j],i-1,1);
		lp kp=0;
		if(i>1)kp+=i-1-ask(1,i-1,1);
		if(i<n)kp+=ask(i+1,n,1);
		ans-=kp*(kp-1)/2;
	}
	printf("%lld\n",ans);
return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#define N 100010
using namespace std;
struct D{
	int l,r;
}a[N],b[N];
int L[N],R[N],len[N],tag[N],val[N];
int n,m,ans;
char str[N/4];
inline void swap(int &a,int &b){
    int t=a;
    a=b;
    b=t;
}
void build(int x,int l,int r){
    len[x]=r-l+1;
    L[x]=l,R[x]=r;
    if(l>=r)
        return;
    int mid=(l+r)>>1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
}
void pushdown(int x){
    if(!tag[x])
        return;
    tag[x<<1]^=1;
    val[x<<1]=len[x<<1]-val[x<<1];
    tag[x<<1|1]^=1;
    val[x<<1|1]=len[x<<1|1]-val[x<<1|1];
    tag[x]=0;
}
void modify(int x,int l,int r){
    if(L[x]>=l&&R[x]<=r){
        val[x]=len[x]-val[x];
        tag[x]^=1;
        return;
    }
    pushdown(x);
    int mid=(L[x]+R[x])>>1;
    if(l<=mid)
        modify(x<<1,l,r);
    if(r>mid)
        modify(x<<1|1,l,r);
    val[x]=val[x<<1]+val[x<<1|1];
}
int query(int x,int l,int r){
    if(L[x]>=l&&R[x]<=r)
        return val[x];
    pushdown(x);
    int res=0;
    int mid=(L[x]+R[x])>>1;
    if(l<=mid)
        res+=query(x<<1,l,r);
    if(r>mid)
        res+=query(x<<1|1,l,r);
    return res;
}
bool cmp(D x,D y){
	return x.l<y.l;
}
bool cmp2(D x,D y){
	return x.r<y.r;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>m;
    build(1,1,n);
    int t;
    for(int i=1;i<=n;i++)
    	cin>>t;
    for(int i=1;i<=m;i++){
    	int x,y;
    	cin>>x>>y;
    	a[i].l=b[i].l=x;
    	a[i].r=b[i].r=y;
	}
	sort(a+1,a+m+1,cmp);
	sort(b+1,b+m+1,cmp2);
	int p=0,q=0;
	long long ans=(n*(n-1)*(n-2))/6;
	for(int i=1;i<=n;i++){
		int d=0;
		while(p<m&&a[p+1].l<=i){
			modify(1,a[p].l,a[p].r);
			p++;
		}
		while(q<m&&b[q+1].r<i){
			modify(1,b[q].l,b[q].r);
			q++;
		}
		if(i>1)
			d+=(i-1)-query(1,1,i-1);
		if(i<n)
			d+=query(1,i+1,n);
		ans-=(d*(d-1))/2;
	}
	printf("%lld",ans);
    return 0;
}


#include <cstdio>
#include <cctype>
#include <algorithm>
#include <vector>
#define re register
using namespace std;
inline int read(){
	int f=0,r=0;char c=getchar();
	while(!isdigit(c))f^=c=='-',c=getchar();
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}
const int MAXN=1e5+1;
struct T{
	int l,r,dat,len,rev;
}t[MAXN<<2];
#define ls (p<<1)
#define rs (p<<1|1)
void build(int p,int l,int r){
	t[p].l=l,t[p].r=r,t[p].len=t[p].dat=r-l+1;
	if(l==r) return;
	int mid=l+r>>1;
	build(ls,l,mid),build(rs,mid+1,r);
}
inline void Rev(int p){t[p].dat=t[p].len-t[p].dat,t[p].rev^=1;}
inline void spread(int p){if(t[p].rev)Rev(ls),Rev(ls),t[p].rev=0;}
void change(int p,int l,int r){
	if(l<=t[p].l && t[p].r<=r) return Rev(p);
	spread(p);
	int mid=t[p].l+t[p].r>>1;
	if(l<=mid) change(ls,l,r);
	if(r>mid) change(rs,l,r);
	t[p].dat=t[ls].dat+t[rs].dat;
}
int n,m,s[MAXN];
vector<int>a[MAXN],b[MAXN];
int main(){
	n=read(),m=read();
	for(re int i=1;i<=n;i++)s[i]=read();
	sort(s+1,s+n+1);
	while(m--){
		int x=read(),y=read();
		x=lower_bound(s+1,s+n+1,x)-s;
		y=upper_bound(s+1,s+n+1,y)-s-1;
		b[x].push_back(y),a[y+1].push_back(x);
	}
	long long ans=1ll*n*(n-1)*(n-2)/6;
	build(1,1,n);int x=0;
	for(re int i=1;i<=n;i++){
		change(1,i,i);
		for(re int j=0;j<a[i].size();j++)
			change(1,a[i][j],i-1),x^=1;
		for(re int j=0;j<b[i].size();j++)
			change(1,i,b[i][j]),x^=1;
		int tmp=t[1].dat-x;
		ans-=1ll*tmp*(tmp-1)/2;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#define N 3003
#include<bitset>
#include<deque>
#include<cstdlib>
#include<set>
#include<ctime>
#define ll long long
using namespace std;
ll read()
{
	ll x=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return f*x;
}
int n,k,a[N],num,top,b[N];
struct TREE{
	bool re;
	int x;
}tr[N*4];
ll ans;
bitset<N>bs[N];
void pushup(int x)
{
	int l=x<<1,r=x<<1|1;
	tr[x].x=tr[l].x+tr[r].x;
}
void pushdown(int x,int len)
{
	int l=x<<1,r=x<<1|1,lenl=len>>1,lenr=len-(len>>1);
	if(tr[x].re)
	{
		tr[x].re=0;
		tr[l].re^=1;
		tr[r].re^=1;
		tr[l].x=lenl-tr[l].x;
		tr[r].x=lenr-tr[r].x;
	}
}
struct node{
	int id,op,l,r;
}st[N];
bool cmp(node x,node y)
{
	return x.id<y.id;
}
void build(int rt,int l,int r)
{
	tr[rt].re=0;
	if(l==r){
		tr[rt].re=tr[rt].x=0;
		return ;
	}
	int mid=l+r>>1;
	build(rt<<1,l,mid);
	build(rt<<1|1,mid+1,r);
}
void rever(int rt,int l,int r,int first,int end)
{
	if(first<=l&&r<=end)
	{
		tr[rt].re^=1;
		tr[rt].x=r-l+1-tr[rt].x;
		return ;
	}
	pushdown(rt,r-l+1);
	int mid=l+r>>1;
	if(first<=mid) rever(rt<<1,l,mid,first,end);
	if(end>mid) rever(rt<<1|1,mid+1,r,first,end);
	pushup(rt);
}
int query(int rt,int l,int  r,int first,int end)
{
	if(first>end) return 0;
	if(first<=l&&r<=end)
	{
		return tr[rt].x;
	}
	int mid=l+r>>1;
	pushdown(rt,r-l+1);
	int ans=0;
	if(first<=mid) ans+=query(rt<<1,l,mid,first,end);
	if(end>mid) ans+=query(rt<<1|1,mid+1,r,first,end);
	return ans;
}
int erflow(int x)
{
	int l=1,r=n,ans=n+1;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(a[mid]>=x) ans=mid,r=mid-1;
		else l=mid+1;
	 }
	 return x;
}
int erfupp(int x)
{
	int l=1,r=n,ans=n+1;
	while(l<=r)
	{
		int mid=l+r>>1;
		if(a[mid]>x) ans=mid,r=mid-1;
		else l=mid+1;
	 }
	 return x;
}
int main()
{
//	freopen("fight.in","r",stdin);
//	freopen("fight.out","w",stdout);
	n=read(),k=read();
	for(int i=1;i<=n;++i) 	a[i]=read();
	sort(a+1,a+1+n);
	for(int i=1;i<=k;++i)
	{
		int L=read(),R=read();
		int t1=erflow(L),t2=erfupp(R);
		if(t1>t2||t1==n+1) continue;
		st[++top]=(node){t1,1,t1,t2-1};
		st[++top]=(node){t2,-1,t1,t2-1};
	}
	sort(st+1,st+1+top,cmp);
/*	for(int i=1;i<=top;++i)
	{
		if(i==1||st[i].x!=st[i-1].x) num++;
		if(st[i].op==0) a[st[i].id]=num;
		else if(st[i].op==1) l[st[i].id]=num;
		else r[st[i].id]=num;
	}
	for(int i=1;i<=n;++i)
	{
		if(i==1||a[i]!=a[i-1]) num++;
		b[i]=num;
	}*/
	int now=1;
	ans=n*(n-1)*(n-2)/6;
//	cout<<ans<<endl;
	for(int i=1;i<=n;++i)
	{
		while(st[now].id==i&&now<=top)
		{
			rever(1,1,n,st[now].l,st[now].r);
			++now;
		}
		ans-=(i-1-query(1,1,n,1,i-1))*query(1,1,n,i+1,n);
	//	cout<<i<<":"<<ans<<endl;
	}
	cout<<ans<<'\n';
	return 0;
 }


#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#define maxn 200000
#define SZJ int
#define AK main
#define half (l+r)>>1
#define SDOI ()
using namespace std;
#define rep (i,a,b) for (int i=a;i<=b;++i)
#define erpe (i,a) for (int i=head[a];i!=-1;i=e[i].next)
#define int long long
struct hzw
{
	int l,r;
}t[maxn];
struct zmd
{
	int lc,rc,tag,sum;
}tre[maxn];
int n,k,num[maxn];
vector<int>bkt[100006];
inline bool cmp(hzw a,hzw b)
{
	if (a.l==b.l) return a.r<b.r;
	else return a.l<b.l;
}
inline void pushdown(int s,int l,int r)
{
	int mid=half;
	int tmp=tre[s].tag%2;
	if (!tmp) {tre[s].tag=0;return;}
	int lson=tre[s].lc,rson=tre[s].rc;
	tre[lson].sum=(mid-l+1)-tre[lson].sum,tre[lson].tag+=tre[s].tag;
	tre[rson].sum=(r-mid)-tre[rson].sum,tre[rson].tag+=tre[s].tag;
	tre[s].tag=0;
}
int cnt=0;
inline void build(int s,int l,int r)
{
	if (l==r) return;
	int mid=half;
	tre[s].lc=++cnt;
	build(tre[s].lc,l,mid);
	tre[s].rc=++cnt;
	build(tre[s].rc,mid+1,r);
}
inline void update(int s,int l,int r,int cl,int cr)
{
	if (l==cl&&r==cr)
	{
		tre[s].sum=(r-l+1)-tre[s].sum;
		tre[s].tag++;
		return;
	}
	if (tre[s].tag) pushdown(s,l,r);
	int mid = half;
	if (cr<=mid) update(tre[s].lc,l,mid,cl,cr);
	else if (cl>mid) update(tre[s].rc,mid+1,r,cl,cr);
	else
	{
		update(tre[s].lc,l,mid,cl,mid);
		update(tre[s].rc,mid+1,r,mid+1,cr);
	}
	tre[s].sum=tre[tre[s].lc].sum+tre[tre[s].rc].sum;
}
inline int query(int s,int l,int r,int cl,int cr)
{
	if (l==cl&&r==cr) return tre[s].sum;
	if (tre[s].tag) pushdown(s,l,r);
	int mid=half;
	if (cr<=mid) return query(tre[s].lc,l,mid,cl,cr);
	else if (cl>mid) return query(tre[s].rc,mid+1,r,cl,cr);
	else
	{
		return query(tre[s].lc,l,mid,cl,mid)+query(tre[s].rc,mid+1,r,mid+1,cr);
	}
}
#undef int
SZJ AK SDOI
{
//	freopen("data.in","r",stdin);
//	freopen("baoli.out","w",stdout);
	#define int long long
	cnt=1;
	cin>>n>>k;
	for (int i=1;i<=n;++i) scanf("%lld",&num[i])
	sort(num+1,num+1+n);
	build(1,1,n);
	num[n+1]=1e17;
	int fina=n*(n-1)*(n-2)/6;
	for (int i=1,a,b;i<=k;++i)
	{
		scanf("%lld%lld",&a,&b)
		t[i].l=lower_bound(num+1,num+1+n,a)-num;
		t[i].r=upper_bound(num+1,num+1+n+1,b)-num-1;
		if (t[i].l>t[i].r) t[i].l=1e17,t[i].r=1e17;
	}
	sort(t+1,t+1+k,cmp);
	int now=1;
	for (int i=1;i<=n;++i)
	{
		while (t[now].l==i) update(1,1,n,t[now].l,t[now].r),bkt[t[now].r].push_back(t[now].l),now++;
		int tmp=0;
		if (i>1) tmp+=query(1,1,n,1,i-1);
		if (i<n) tmp+=(n-i)-query(1,1,n,i+1,n);
		for (int j=0;j<bkt[i].size();++j) update(1,1,n,bkt[i][j],i);
		fina-=tmp*(tmp-1)/2;
	}
	cout<<fina;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
using namespace std;
const long long maxn=1e5+9;
long long n,k;
int a[maxn];
long long ans[maxn<<2],tag[maxn<<2];
inline long long ls(long long p){return p<<1;}
inline long long rs(long long p){return p<<1|1;}
inline long long push_up(long long p){ans[p]=ans[ls(p)]+ans[rs(p)];}
inline void f(ll p,ll l,ll r)
{
	tag[p]^=1;
	ans[p]=r-l+1-ans[p];
}
inline void push_down(ll p,ll l, ll r)
{
	if(tag[p])
	{
		ll mid=l+r>>1;
		f(ls(p),l,mid);
		f(rs(p),mid+1,r);
		tag[p]=0;
	}
}
void update(ll nl,ll nr,ll l,ll r,ll p)
{
	if(nl<=l&&nr>=r){f(p,l,r);return;}
	ll mid=l+r>>1;
	push_down(p,l,r);
	if(nl<=mid) update(nl,nr,l,mid,ls(p));
	if(nr>mid) update(nl,nr,mid+1,r,rs(p));
	push_up(p);
}
ll query(ll nl,ll nr,ll l,ll r,ll p)
{
	ll res=0;
	if(nl<=l&&nr>=r){return ans[p];}
	ll mid=l+r>>1;
	push_down(p,l,r);
	if(nl<=mid)res+=query(nl,nr,l,mid,ls(p));
	if(nr>mid) res+=query(nl,nr,mid+1,r,rs(p));
	return res;
}

int l,r;
vector<long long>vl[maxn],vr[maxn];
int main()
{
	cin>>n>>k;
	long long tot=1l*n*(n-1)*(n-2)/6;
	for(long long i=0;i<n;i++)scanf("%d",&a[i]);// cin>>a[i];
	sort(a,a+n);
	while(k--)
	{
//		cin>>l>>r;
		scanf("%d%d",&l,&r);
		l=lower_bound(a,a+n,l)-a;
		r=upper_bound(a,a+n,r)-a-1;
		if(l>r)continue;
		vl[l].push_back(r);
		vr[r].push_back(l);
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<(int)vl[i].size();j++)update(i,vl[i][j],0,n-1,1);
		long long res=0;
		if(i)res+=i-query(0,i-1,0,n-1,1);
		if(i<n-1)res+=query(i+1,n-1,0,n-1,1);
//		cout<<res<<endl;
		tot-=res*(res-1)/2;
		for(int j=0;j<(int)vr[i].size();j++)update(vr[i][j],i,0,n-1,1);
	}
	cout<<tot<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define lson rec<<1,l,mid
#define rson rec<<1|1,mid+1,r
#define rint register int
using namespace std;
const int mx=100005;
int n,k,x,y;
ll ans;
int a[mx],d[mx];
int col[mx<<2],ad[mx<<2],sm[mx<<2];
struct node{
	int l,r;
};
node c[mx];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-') f=-1;}
	while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
	return x*f;
}
inline bool cmp1(node a,node b)
{
	if(a.l==b.l) return a.r<b.r;
	return a.l<b.l;
}
inline bool cmp2(node a,node b)
{
	if(a.r==b.r) return a.l>b.l;
	return a.r>b.r;
}
inline void pushdown(int rec,int len)
{
	if(ad[rec]){
		int hf2=len>>1,hf1;hf1=hf2+((len&1)?1:0);
		col[rec<<1]^=1;col[rec<<1|1]^=1;
		ad[rec<<1]^=1;ad[rec<<1|1]^=1;
		sm[rec<<1]=hf1-sm[rec<<1];sm[rec<<1|1]=hf2-sm[rec<<1|1];ad[rec]=0;
	}
}
void modify(int rec,int l,int r)
{
	if(x<=l&&y>=r){
		col[rec]^=1;ad[rec]^=1;sm[rec]=r-l+1-sm[rec];return;
	}
	pushdown(rec,r-l+1);int mid=(l+r)>>1;
	if(x<=mid) modify(lson);
	if(y>mid) modify(rson);
	sm[rec]=sm[rec<<1]+sm[rec<<1|1];
}
int query(int rec,int l,int r)
{
	if(x<=l&&y>=r) return sm[rec];
	pushdown(rec,r-l+1);int mid=(l+r)>>1;
	if(y<=mid) return query(lson);
	if(x>mid) return query(rson);
	return query(lson)+query(rson);
}
int main()
{
	n=read();k=read();for(rint i=1;i<=n;++i) a[i]=read();
	sort(a+1,a+1+n);
	for(rint i=1;i<=k;++i){
		c[i].l=read();c[i].r=read();
		c[i].l=lower_bound(a+1,a+1+n,c[i].l)-a;
		c[i].r=upper_bound(a+1,a+1+n,c[i].r)-a-1;
	}
	int zz=1;sort(c+1,c+1+k,cmp1);
	for(rint i=1;i<=k;++i){
		while(zz<n&&c[i].l>zz){x=zz+1;y=n;
			d[zz]=query(1,1,n);++zz;
		}
		if(c[i].l<=c[i].r){
			x=c[i].l;y=c[i].r;modify(1,1,n);
		}
	}
	while(zz<n) x=zz+1,y=n,d[zz]=query(1,1,n),++zz;
	zz=n;sort(c+1,c+1+k,cmp2);
	memset(col,0,sizeof(col));memset(ad,0,sizeof(ad));memset(sm,0,sizeof(sm));
	for(rint i=1;i<=k;++i){
		while(zz>1&&c[i].r<zz){x=1;y=zz-1;
			d[zz]+=y-x+1-query(1,1,n);--zz;
		}
		if(c[i].l<=c[i].r){
			x=c[i].l;y=c[i].r;modify(1,1,n);
		}
	}
	while(zz>1) x=1,y=zz-1,d[zz]+=y-x+1-query(1,1,n),--zz;
	ans=(ll)n*(n-1)*(n-2)/6;
	for(rint i=1;i<=n;++i) ans-=(ll)d[i]*(d[i]-1)/2;
	printf("%lld",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N=1e5+100;
#define ll o<<1
#define rr o<<1|1
#define mid (l+r)/2
#define pb push_back
typedef __int64 LL;
int cc[N<<2],num[N<<2];
int L,R;
void pushdown(int l,int r,int o){
    if(!cc[o])return ;
    cc[ll]^=cc[o];
    cc[rr]^=cc[o];
    num[ll]=mid-l+1-num[ll];
    num[rr]=r-mid-num[rr];
    cc[o]=0;
}

void update(int l,int r,int o){
    if(L<=l&&r<=R){
        cc[o]^=1;
        num[o]=r-l+1-num[o];
        return ;
    }
    pushdown(l,r,o);
    if(L<=mid)update(l,mid,ll);
    if(R>mid)update(mid+1,r,rr);
    num[o]=num[ll]+num[rr];
}

int query(int l,int r,int o,int v){
    if(L<=l&&r<=R){
        if(v==0)return r-l+1-num[o];
        else return num[o];
    }
    pushdown(l,r,o);
    int sum=0;
    if(L<=mid)sum+=query(l,mid,ll,v);
    if(R>mid) sum+=query(mid+1,r,rr,v);
    return sum;
}
vector<int> vv[N],ww[N];
int aa[N];

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)scanf("%d",&aa[i]);
    sort(aa,aa+n);
    for(int i=0;i<n;i++){
       vv[i].clear();ww[i].clear();
    }
    for(int i=0;i<m;i++){
        int l,r;scanf("%d%d",&l,&r);
        int id1=lower_bound(aa,aa+n,l)-aa;
        int id2=lower_bound(aa,aa+n,r)-aa;
        if(id2==n||aa[id2]>r)id2--;
        l=id1,r=id2;
            //printf("%d %dads\n",l,r);
        if(l>r)continue;
        vv[l].pb(r);
        ww[r].pb(l);
    }
    LL ans=(LL)n*(n-1)*(n-2)/6;
    memset(cc,0,sizeof(cc));
    memset(num,0,sizeof(num));
        //for(int i=0;i<n;i++)printf("%d ",aa[i]);printf("aa\n");
    for(int i=0;i<n;i++){
        for(int j=0;j<vv[i].size();j++){
            L=i,R=vv[i][j];
            update(0,n-1,1);
        }
        int bns=0;
        L=0,R=i-1;
        if(L<=R)bns+=query(0,n-1,1,0);
            //printf("%d ",bns);
        L=i+1,R=n-1;
        if(L<=R)bns+=query(0,n-1,1,1);

        for(int j=0;j<ww[i].size();j++){
            L=ww[i][j],R=i;
            update(0,n-1,1);
        }
            //printf("%d:i\n",bns,i);
        ans-=(LL)bns*(bns-1)/2;
    }
    printf("%I64d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const int maxn=(1e5)+10;
int n,q,d[maxn],tot;
int tr[maxn*4],lazy[maxn*4];
int sz[maxn*4],sum[maxn];
ll ans;
struct node { int x,l,r; } a[maxn*2];
bool cmp(node t1,node t2) { return t1.x<t2.x; }
void build(int l,int r,int root) {
	sz[root]=r-l+1;
	if (l==r) return;
	int mid=(l+r)/2;
	build(l,mid,root*2);
	build(mid+1,r,root*2+1);
}
void pushdown(int root) {
	if (!lazy[root]) return;
	lazy[root*2]^=1;
	lazy[root*2+1]^=1;
	tr[root*2]=sz[root*2]-tr[root*2];
	tr[root*2+1]=sz[root*2+1]-tr[root*2+1];
	lazy[root]=0;
}
void add(int L,int R,int l,int r,int root) {
	if (L<=l&&r<=R) {
		tr[root]=sz[root]-tr[root];
		lazy[root]^=1;
		return;
	}
	pushdown(root);
	int mid=(l+r)/2;
	if (L<=mid) add(L,R,l,mid,root*2);
	if (mid<R) add(L,R,mid+1,r,root*2+1);
	tr[root]=tr[root*2]+tr[root*2+1];
}
int query(int L,int R,int l,int r,int root) {
	if (L>R) return 0;
	if (L<=l&&r<=R) return tr[root];
	pushdown(root);
	int mid=(l+r)/2,res=0;
	if (L<=mid) res+=query(L,R,l,mid,root*2);
	if (mid<R) res+=query(L,R,mid+1,r,root*2+1);
	return res;
}
ll C2(ll x) { return x*(x-1)/2; }
ll C3(ll x) { return x*(x-1)*(x-2)/6; }
int main() {
	//freopen("1.txt","r",stdin);
	read(n); read(q);
	for (int i=1;i<=n;i++) read(d[i]);
	sort(d+1,d+n+1);
	int x,y;
	while (q--) {
		read(x); read(y);
		x=lower_bound(d+1,d+n+1,x)-d;
		y=upper_bound(d+1,d+n+1,y)-d-1;
		if (x>y) continue;
		a[++tot]=(node){x,x,y};
		a[++tot]=(node){y+1,x,y};
		//printf("flip %d %d\n",x,y);
	}
	sort(a+1,a+tot+1,cmp);
	build(1,n,1);
	int pos=0;
	for (int i=1;i<=n;i++) {
		while (pos<tot&&a[pos+1].x<=i) {
			pos++;
			add(a[pos].l,a[pos].r,1,n,1);
		}
		sum[i]=query(1,i-1,1,n,1)+(n-i)-query(i+1,n,1,n,1);
		//printf("%d\n",sum[i]);
	}
	for (int i=1;i<=n;i++) ans+=C2(n-1-sum[i]);
	printf("%lld\n",C3(n)-ans);
	return 0;
}
/*
  REMEMBER:
  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Array size? Integer overflow? Time complexity? Memory usage? Precision error?
*///hellobig_tq


#include<iostream>
#include<vector>
#include<algorithm>
#define f first
#define s second
#define mp make_pair
using namespace std;

struct Stree {
	vector<int> data;
	vector<bool> lazy;
	int size;

	Stree(int nsize) {
		size = 1;
		while(size < nsize) size *= 2;

		data.resize(size*2, 0);
		lazy.resize(size*2, 0);
	}

	void propagate(int pos, int lb, int rb) {
		if(lazy[pos]) {
			lazy[pos] = false;
			data[pos] = rb - lb - data[pos];

			if(pos < size) {
				lazy[pos*2] = !lazy[pos*2];
				lazy[pos*2+1] = !lazy[pos*2+1];
			}
		}
	}

	void set(int l, int r, int pos = 1, int lb = 0, int rb = -1) {
		if(rb == -1) rb = size;

		propagate(pos, lb, rb);
		if(rb <= l || r <= lb)
			return;

		if(l <= lb && rb <= r) {
			lazy[pos] = !lazy[pos];
			propagate(pos, lb, rb);
			return;
		}

		int mid = (lb + rb) / 2;
		set(l, r, pos*2, lb, mid);
		set(l, r, pos*2+1, mid, rb);
		data[pos] = data[pos*2] + data[pos*2+1];
	}

	int get(int l, int r, int pos = 1, int lb = 0, int rb = -1) {
		if(rb == -1) rb = size;

		propagate(pos, lb, rb);
		if(rb <= l || r <= lb)
			return 0;

		if(l <= lb && rb <= r)
			return data[pos];

		int mid = (lb + rb) / 2;
		return 	get(l, r, pos*2, lb, mid) +
				get(l, r, pos*2+1, mid, rb);
	}
};

bool byFirst(pair<int, int> l, pair<int, int> r) {
	return l.f < r.f;
}
bool bySecond(pair<int, int> l, pair<int, int> r) {
	return l.s < r.s;
}

//------------------------------------------------------------
//Main function

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

	int n, k;
	cin >> n >> k;
	vector<int> s(n);
	for(int i=0;i<n;i++)
		cin >> s[i];
	sort(s.begin(), s.end() );

	vector<pair<int, int> > op;
	for(int i=0;i<k;i++) {
		int a, b;
		cin >> a >> b;
		a = lower_bound(s.begin(), s.end(), a) - s.begin();
		b = upper_bound(s.begin(), s.end(), b) - s.begin();
		if(a != b)
			op.push_back(mp(a, b));
	}

	vector<int> wins(n, 0);

	//Now calculate the wins
	for(int side = 0; side < 2; side++) {
		vector<pair<int, int> > toAdd = op, toRemove = op;
		Stree st(n);
		if(side) {
			for(auto &c : toAdd) c = mp(n - c.s, n - c.f);
			for(auto &c : toRemove) c = mp(n - c.s, n - c.f);
		}
		else
			st.set(0, n);

		sort(toAdd.begin(), toAdd.end(), byFirst);
		sort(toRemove.begin(), toRemove.end(), bySecond);

		int ai = 0, ri = 0;

		for(int i=0;i<n;i++) {
			while(ai < (int)toAdd.size() && toAdd[ai].f <= i) {
				st.set(toAdd[ai].f, toAdd[ai].s);
				ai++;
			}
			while(ri < (int)toRemove.size() && toRemove[ri].s <= i) {
				st.set(toRemove[ri].f, toRemove[ri].s);
				ri++;
			}

			if(side)
				wins[n-i-1] += st.get(0, i);
			else
				wins[i] += st.get(0, i);
		}
	}

	long long result = 0;
	for(auto c : wins)
		result += 1LL * c * (c-1) / 2;

	cout << 1LL * n * (n-1) * (n-2) / 6 - result << '\n';

	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#define f first
#define s second
#define mp make_pair
using namespace std;

struct Stree {
	vector<int> data;
	vector<bool> lazy;
	int size;

	Stree(int nsize) {
		size = 1;
		while(size < nsize) size *= 2;

		data.resize(size*2, 0);
		lazy.resize(size*2, 0);
	}

	void propagate(int pos, int lb, int rb) {
		if(lazy[pos]) {
			lazy[pos] = false;
			data[pos] = rb - lb - data[pos];

			if(pos < size) {
				lazy[pos*2] = !lazy[pos*2];
				lazy[pos*2+1] = !lazy[pos*2+1];
			}
		}
	}

	void set(int l, int r, int pos = 1, int lb = 0, int rb = -1) {
		if(rb == -1) rb = size;

		propagate(pos, lb, rb);
		if(rb <= l || r <= lb)
			return;

		if(l <= lb && rb <= r) {
			lazy[pos] = !lazy[pos];
			propagate(pos, lb, rb);
			return;
		}

		int mid = (lb + rb) / 2;
		set(l, r, pos*2, lb, mid);
		set(l, r, pos*2+1, mid, rb);
		data[pos] = data[pos*2] + data[pos*2+1];
	}

	int get(int l, int r, int pos = 1, int lb = 0, int rb = -1) {
		if(rb == -1) rb = size;

		propagate(pos, lb, rb);
		if(rb <= l || r <= lb)
			return 0;

		if(l <= lb && rb <= r)
			return data[pos];

		int mid = (lb + rb) / 2;
		return 	get(l, r, pos*2, lb, mid) +
				get(l, r, pos*2+1, mid, rb);
	}
};

bool byFirst(pair<int, int> l, pair<int, int> r) {
	return l.f < r.f;
}
bool bySecond(pair<int, int> l, pair<int, int> r) {
	return l.s < r.s;
}

//------------------------------------------------------------
//Main function

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

	int n, k;
	cin >> n >> k;
	vector<int> s(n);
	for(int i=0;i<n;i++)
		cin >> s[i];
	sort(s.begin(), s.end() );

	vector<pair<int, int> > op;
	for(int i=0;i<k;i++) {
		int a, b;
		cin >> a >> b;
		a = lower_bound(s.begin(), s.end(), a) - s.begin();
		b = upper_bound(s.begin(), s.end(), b) - s.begin();
		if(a != b)
			op.push_back(mp(a, b));
	}

	vector<int> wins(n, 0);

	//Now calculate the wins
	for(int side = 0; side < 2; side++) {
		vector<pair<int, int> > toAdd = op, toRemove = op;
		Stree st(n);
		if(side) {
			for(auto &c : toAdd) c = mp(n - c.s, n - c.f);
			for(auto &c : toRemove) c = mp(n - c.s, n - c.f);
		}
		else
			st.set(0, n);

		sort(toAdd.begin(), toAdd.end(), byFirst);
		sort(toRemove.begin(), toRemove.end(), bySecond);

		int ai = 0, ri = 0;

		for(int i=0;i<n;i++) {
			while(ai < (int)toAdd.size() && toAdd[ai].f <= i) {
				st.set(toAdd[ai].f, toAdd[ai].s);
				ai++;
			}
			while(ri < (int)toRemove.size() && toRemove[ri].s <= i) {
				st.set(toRemove[ri].f, toRemove[ri].s);
				ri++;
			}

			if(side)
				wins[n-i-1] = st.get(0, i);
			else
				wins[i] += st.get(0, i);
		}
	}

	long long result = 0;
	for(auto c : wins)
		result += 1LL * c * (c-1) / 2;

	cout << 1LL * n * (n-1) * (n-2) / 6 - result << '\n';

	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
	int l,r,cnt,lazy;
	node *lson,*rson;
	inline void upd(){cnt=lson->cnt+rson->cnt;}
	inline void mark(){lazy^=1;cnt=(r-l+1)-cnt;}
	inline void fa_lazy(){if(lazy){lson->mark();rson->mark();lazy=0;}}
}*root,pool[233333];
int top;
node *build(int l,int r)
{
	node *tmp=&pool[top++];
	tmp->l=l;tmp->r=r;tmp->cnt=0;tmp->lazy=0;
	if(l!=r)
	{
		int mid=(l+r)/2;
		tmp->lson=build(l,mid);
		tmp->rson=build(mid+1,r);
	}
	return tmp;
}
void change(node *cur,int l,int r)
{
	if(cur->l==l&&cur->r==r)
	{
		cur->mark();
		return;
	}
	if(cur->lazy)cur->fa_lazy();
	if(cur->lson->r>=r)change(cur->lson,l,r);
	else if(cur->rson->l<=l)change(cur->rson,l,r);
	else change(cur->lson,l,cur->lson->r),change(cur->rson,cur->rson->l,r);
	cur->upd();
}
int query(node *cur,int l,int r)
{
	if(cur->l==l&&cur->r==r)
	{
		return cur->cnt;
	}
	if(cur->lazy)cur->fa_lazy();
	if(cur->lson->r>=r)return query(cur->lson,l,r);
	else if(cur->rson->l<=l)return query(cur->rson,l,r);
	else return query(cur->lson,l,cur->lson->r)+query(cur->rson,cur->rson->l,r);
}
struct que
{
	int t,l,r;
	inline bool operator<(const que &x)const{return t<x.t;}
}q[777777];
int qtop;
int n,k;
int a[233333],ans[233333];
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	int x,y,l,r,L,R,mid;
	for(int i=1;i<=k;i++)
	{
		scanf("%d%d",&x,&y);
		L=1,R=n;
		while(L<R)
		{
			mid=(L+R)/2;
			if(a[mid]<x)L=mid+1;
			else R=mid;
		}
		l=L;
		L=1,R=n;
		while(L<R)
		{
			mid=(L+R+1)/2;
			if(a[mid]>y)R=mid-1;
			else L=mid;
		}
		r=L;
		if(l<=r)
		{
			qtop++;q[qtop].l=l;q[qtop].r=r;q[qtop].t=l;
			qtop++;q[qtop].l=l;q[qtop].r=r;q[qtop].t=r+1;
		}
	}
	qtop++;q[qtop].l=1;q[qtop].r=n;q[qtop].t=1;
	for(int i=1;i<=n;i++)
	{
		qtop++;q[qtop].l=i;q[qtop].r=i;q[qtop].t=i+1;
	}
	sort(q+1,q+qtop+1);
	root=build(1,n);
	int p=1;
	for(int t=1;t<=n;t++)
	{
		while(q[p].t<=t)
		{
//			cout<<q[p].t<<' '<<q[p].l<<' '<<q[p].r<<endl;
			change(root,q[p].l,q[p].r);
			p++;
		}
		if(t>1)ans[t]+=query(root,1,t-1);
		if(t<n)ans[t]+=query(root,t+1,n);
	}
	long long cnt=1ll*n*(n-1)*(n-2)/6;
	for(int i=1;i<=n;i++)
	{
		cnt-=1ll*ans[i]*(ans[i]-1)/2;
	}
	printf("%lld\n",cnt);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
	int l,r,cnt,lazy;
	node *lson,*rson;
	inline void upd(){cnt=lson->cnt+rson->cnt;}
	inline void mark(){lazy^=1;cnt=(r-l+1)-cnt;}
	inline void fa_lazy(){if(lazy){lson->mark();rson->mark();lazy=0;}}
}*root,pool[233333];
int top;
node *build(int l,int r)
{
	node *tmp=&pool[top++];
	tmp->l=l;tmp->r=r;tmp->cnt=0;tmp->lazy=0;
	if(l!=r)
	{
		int mid=(l+r)/2;
		tmp->lson=build(l,mid);
		tmp->rson=build(mid+1,r);
	}
	return tmp;
}
void change(node *cur,int l,int r)
{
	if(cur->l==cur->r)
	{
		cur->mark();
		return;
	}
	if(cur->lazy)cur->fa_lazy();
	if(cur->lson->r>=r)change(cur->lson,l,r);
	else if(cur->rson->l<=l)change(cur->rson,l,r);
	else change(cur->lson,l,cur->lson->r),change(cur->rson,cur->rson->l,r);
	cur->upd();
}
int query(node *cur,int l,int r)
{
	if(cur->l==cur->r)
	{
		return cur->cnt;
	}
	if(cur->lazy)cur->fa_lazy();
	if(cur->lson->r>=r)return query(cur->lson,l,r);
	else if(cur->rson->l<=l)return query(cur->rson,l,r);
	else return query(cur->lson,l,cur->lson->r)+query(cur->rson,cur->rson->l,r);
}
struct que
{
	int t,l,r;
	inline bool operator<(const que &x)const{return t<x.t;}
}q[777777];
int qtop;
int n,k;
int a[233333],ans[233333];
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	int x,y,l,r,L,R,mid;
	for(int i=1;i<=k;i++)
	{
		scanf("%d%d",&x,&y);
		L=1,R=n;
		while(L<R)
		{
			mid=(L+R)/2;
			if(a[mid]<x)L=mid+1;
			else R=mid;
		}
		l=L;
		L=1,R=n;
		while(L<R)
		{
			mid=(L+R+1)/2;
			if(a[mid]>y)R=mid-1;
			else L=mid;
		}
		r=L;
		if(l<=r)
		{
			qtop++;q[qtop].l=l;q[qtop].r=r;q[qtop].t=l;
			qtop++;q[qtop].l=l;q[qtop].r=r;q[qtop].t=r+1;
		}
	}
	qtop++;q[qtop].l=1;q[qtop].r=n;q[qtop].t=1;
	for(int i=1;i<=n;i++)
	{
		qtop++;q[qtop].l=i;q[qtop].r=i;q[qtop].t=i+1;
	}
	sort(q+1,q+qtop+1);
	root=build(1,n);
	int p=1;
	for(int t=1;t<=n;t++)
	{
		while(q[p].t<=t)
		{
//			cout<<q[p].t<<' '<<q[p].l<<' '<<q[p].r<<endl;
			change(root,q[p].l,q[p].r);
			p++;
		}
		if(t>1)ans[t]+=query(root,1,t-1);
		if(t<n)ans[t]+=query(root,t+1,n);
	}
	long long cnt=1ll*n*(n-1)*(n-2)/6;
	for(int i=1;i<=n;i++)
	{
		cnt-=1ll*ans[i]*(ans[i]-1)/2;
	}
	printf("%lld\n",cnt);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node
{
	int l,r,cnt,lazy;
	node *lson,*rson;
	inline void upd(){cnt=lson->cnt+rson->cnt;}
	inline void mark(){lazy^=1;cnt=(r-l+1)-cnt;}
	inline void fa_lazy(){if(lazy){lson->mark();rson->mark();lazy=0;}}
}*root,pool[233333];
int top;
node *build(int l,int r)
{
	node *tmp=&pool[top++];
	tmp->l=l;tmp->r=r;tmp->cnt=0;tmp->lazy=0;
	if(l!=r)
	{
		int mid=(l+r)/2;
		tmp->lson=build(l,mid);
		tmp->rson=build(mid+1,r);
	}
	return tmp;
}
void change(node *cur,int l,int r)
{
	if(cur->l==cur->r)
	{
		cur->mark();
		return;
	}
	if(cur->lazy)cur->fa_lazy();
	if(cur->lson->r>=r)change(cur->lson,l,r);
	else if(cur->rson->l<=l)change(cur->rson,l,r);
	else change(cur->lson,l,cur->lson->r),change(cur->rson,cur->rson->l,r);
	cur->upd();
}
int query(node *cur,int l,int r)
{
	if(cur->l==cur->r)
	{
		return cur->cnt;
	}
	if(cur->lazy)cur->fa_lazy();
	if(cur->lson->r>=r)return query(cur->lson,l,r);
	else if(cur->rson->l<=l)return query(cur->rson,l,r);
	else return query(cur->lson,l,cur->lson->r)+query(cur->rson,cur->rson->l,r);
}
struct que
{
	int t,l,r;
	inline bool operator<(const que &x)const{return t<x.t;}
}q[777777];
int qtop;
int n,k;
int a[233333],ans[233333];
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	int x,y,l,r,L,R,mid;
	for(int i=1;i<=k;i++)
	{
		scanf("%d%d",&x,&y);
		L=1,R=n;
		while(L<R)
		{
			mid=(L+R)/2;
			if(a[mid]<x)L=mid+1;
			else R=mid;
		}
		l=L;
		L=1,R=n;
		while(L<R)
		{
			mid=(L+R+1)/2;
			if(a[mid]>y)R=mid-1;
			else L=mid;
		}
		r=L;
		qtop++;q[qtop].l=l;q[qtop].r=r;q[qtop].t=l;
		qtop++;q[qtop].l=l;q[qtop].r=r;q[qtop].t=r+1;
	}
	qtop++;q[qtop].l=1;q[qtop].r=n;q[qtop].t=1;
	for(int i=1;i<=n;i++)
	{
		qtop++;q[qtop].l=i;q[qtop].r=i;q[qtop].t=i+1;
	}
	sort(q+1,q+qtop+1);
	root=build(1,n);
	int p=1;
	for(int t=1;t<=n;t++)
	{
		while(q[p].t<=t)
		{
//			cout<<q[p].t<<' '<<q[p].l<<' '<<q[p].r<<endl;
			change(root,q[p].l,q[p].r);
			p++;
		}
		if(t>1)ans[t]+=query(root,1,t-1);
		if(t<n)ans[t]+=query(root,t+1,n);
	}
	long long cnt=1ll*n*(n-1)*(n-2)/6;
	for(int i=1;i<=n;i++)
	{
		cnt-=1ll*ans[i]*(ans[i]-1)/2;
	}
	printf("%lld\n",cnt);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+9;
int l,r,n,k,seg[MAX*4],lz[MAX*4],a[MAX];
long long ans,t;
vector<pair<int,int> > o[MAX];
void shift(int s,int e,int m,int id)
{
	if (lz[id])
	{
		lz[id*2+1]^=1,lz[id*2+2]^=1;
		seg[id*2+1]=m-s-seg[id*2+1];
		seg[id*2+2]=e-m-seg[id*2+2];
		lz[id]^=1;
	}
}
void upd(int l,int r,int s=0,int e=n,int id=0)
{
	if (r<=s || e<=l) return ;
	if (l<=s && e<=r){seg[id]=e-s-seg[id],lz[id]^=1;return ;}
	int mid=s+e>>1;
	shift(s,e,mid,id);
	upd(l,r,s,mid,id*2+1),upd(l,r,mid,e,id*2+2);
	seg[id]=seg[id*2+1]+seg[id*2+2];
}
int get(int l,int r,int s=0,int e=n,int id=0)
{
	if (r<=s || e<=l) return 0;
	if (l<=s && e<=r) return seg[id];
	int mid=s+e>>1;
	shift(s,e,mid,id);
	return get(l,r,s,mid,id*2+1)+get(l,r,mid,e,id*2+2);
}
int main()
{
	ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>k,ans=-1LL*n*(n-2)*(n-1)/6;
	for (int i=0;i<n;i++) cin>>a[i];
	sort(a,a+n);
	for (int i=0;i<k;i++)
		cin>>l>>r,l=lower_bound(a,a+n,l)-a,r=upper_bound(a,a+n,r)-a,o[l].push_back({l,r}),o[r].push_back({l,r});
	for (int i=0;i<n;i++)
	{
		for (auto j:o[i]) upd(j.first,j.second);
		if (i) upd(i-1,i);
		t=get(0,n);
		if (get(i,i+1)) t--;
		ans+=t*(n-t-1);
	}
	cout<<ans/2;
	return 0;
}


#include <bits/stdc++.h>

#define int long long
#define F first
#define S second
#define pb push_back
#define pii pair <int,int>

using namespace std;

const int N=4e5+9;
int a[N],seg[N];
vector <pii> vec[N];
bool lazy[N];

void FLUSH(int pos,int l,int r){
    if(!lazy[pos]) return ;
    seg[pos]=r-l+1-seg[pos];
    lazy[pos]=0;
    if(l!=r) lazy[pos*2]^=1,lazy[pos*2+1]^=1;
}

void REV(int pos,int l,int r,int ql,int qr){
    FLUSH(pos,l,r);
    if(l>qr || r<ql) return ;
    if(l>=ql && r<=qr){
        lazy[pos]^=1;
        FLUSH(pos,l,r);
        return ;
    }
    FLUSH(pos,l,r);
    int mid=(l+r)/2;
    REV(pos*2,l,mid,ql,qr); REV(pos*2+1,mid+1,r,ql,qr);
    seg[pos]=seg[pos*2]+seg[pos*2+1];
}

int NUM(int pos,int l,int r,int ql,int qr){
    FLUSH(pos,l,r);
    if(qr<ql) return 0;
    if(l>qr || r<ql) return 0;
    if(l>=ql && r<=qr) return seg[pos];
    int mid=(l+r)/2;
    return NUM(pos*2,l,mid,ql,qr)+NUM(pos*2+1,mid+1,r,ql,qr);
}

int32_t main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    for(int i=0;i<m;i++){
        int l,r;
        cin>>l>>r;
        int il=lower_bound(a,a+n,l)-a,ir=upper_bound(a,a+n,r)-a;
        if(il!=ir && il<n && ir>0) vec[il].pb({il,ir-1}); vec[ir].pb({il,ir-1});
    }
    int ans=(n*(n-1)*(n-2))/6;
    for(int i=0;i<n;i++){
        REV(1,0,n-1,i,i); //khodesh baraye baadi ha
        for(pii p:vec[i]) REV(1,0,n-1,p.F,p.S); // qry ha
        int x=NUM(1,0,n-1,0,i-1)+NUM(1,0,n-1,i+1,n-1);
        ans-=(x*(x-1))/2;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

#define int long long
#define F first
#define S second
#define pb push_back
#define pii pair <int,int>

using namespace std;

const int N=4e5+9;
int a[N],seg[N];
vector <pii> vec[N];
bool lazy[N];

void FLUSH(int pos,int l,int r){
    if(!lazy[pos]) return ;
    seg[pos]=r-l+1-seg[pos];
    lazy[pos]=0;
    if(l!=r) lazy[pos*2]^=1,lazy[pos*2+1]^=1;
}

void REV(int pos,int l,int r,int ql,int qr){
    if(l>qr || r<ql) return ;
    if(l>=ql && r<=qr){
        lazy[pos]^=1;
        FLUSH(pos,l,r);
        return ;
    }
    FLUSH(pos,l,r);
    int mid=(l+r)/2;
    REV(pos*2,l,mid,ql,qr); REV(pos*2+1,mid+1,r,ql,qr);
    seg[pos]=seg[pos*2]+seg[pos*2+1];
}

int NUM(int pos,int l,int r,int ql,int qr){
    if(qr<ql) return 0;
    FLUSH(pos,l,r);
    if(l>qr || r<ql) return 0;
    if(l>=ql && r<=qr) return seg[pos];
    int mid=(l+r)/2;
    return NUM(pos*2,l,mid,ql,qr)+NUM(pos*2+1,mid+1,r,ql,qr);
}

int32_t main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    for(int i=0;i<m;i++){
        int l,r;
        cin>>l>>r;
        int il=lower_bound(a,a+n,l)-a,ir=upper_bound(a,a+n,r)-a;
        if(il!=ir && il<n && ir>0) vec[il].pb({il,ir-1}); vec[ir].pb({il,ir-1});
    }
    int ans=(n*(n-1)*(n-2))/6;
    for(int i=0;i<n;i++){
        REV(1,0,n-1,i,i); //khodesh baraye baadi ha
        for(pii p:vec[i]) REV(1,0,n-1,p.F,p.S); // qry ha
        int x=NUM(1,0,n-1,0,i-1)+NUM(1,0,n-1,i+1,n-1);
        ans-=(x*(x-1))/2;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

#define int long long
#define F first
#define S second
#define pb push_back
#define pii pair <int,int>

using namespace std;

const int N=4e5+9;
int a[N],seg[N];
vector <pii> vec[N];
bool lazy[N];

void FLUSH(int pos,int l,int r){
    if(!lazy[pos]) return ;
    seg[pos]=r-l+1-seg[pos];
    lazy[pos]=0;
    if(l!=r) lazy[pos*2]^=1,lazy[pos*2+1]^=1;
}

void REV(int pos,int l,int r,int ql,int qr){
    if(l>qr || r<ql) return ;
    if(l>=ql && r<=qr){
        lazy[pos]^=1;
        FLUSH(pos,l,r);
        return ;
    }
    FLUSH(pos,l,r);
    int mid=(l+r)/2;
    REV(pos*2,l,mid,ql,qr); REV(pos*2+1,mid+1,r,ql,qr);
    seg[pos]=seg[pos*2]+seg[pos*2+1];
}

int NUM(int pos,int l,int r,int ql,int qr){
    if(qr<ql) return 0;
    FLUSH(pos,l,r);
    if(l>qr || r<ql) return 0;
    if(l>=ql && r<=qr) return seg[pos];
    int mid=(l+r)/2;
    return NUM(pos*2,l,mid,ql,qr)+NUM(pos*2+1,mid+1,r,ql,qr);
}

int32_t main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    for(int i=0;i<m;i++){
        int l,r;
        cin>>l>>r;
        int il=lower_bound(a,a+n,l)-a,ir=upper_bound(a,a+n,r)-a;
        if(il<n && ir>0) vec[il].pb({il,ir-1}); vec[ir].pb({il,ir-1});
    }
    int ans=(n*(n-1)*(n-2))/6;
    for(int i=0;i<n;i++){
        REV(1,0,n-1,i,i); //khodesh baraye baadi ha
        for(pii p:vec[i]) REV(1,0,n-1,p.F,p.S); // qry ha
        int x=NUM(1,0,n-1,0,i-1)+NUM(1,0,n-1,i+1,n-1);
        ans-=(x*(x-1))/2;
    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

#define int long long
#define F first
#define S second
#define pb push_back
#define pii pair <int,int>

using namespace std;

const int N=4e5+9;
int a[N],seg[N];
vector <pii> vec[N];
bool lazy[N];

void FLUSH(int pos,int l,int r){
    if(!lazy[pos]) return ;
    seg[pos]=r-l+1-seg[pos];
    lazy[pos]=0;
    if(l!=r) lazy[pos*2]^=1,lazy[pos*2+1]^=1;
}

void REV(int pos,int l,int r,int ql,int qr){
    if(l>qr || r<ql) return ;
    if(l>=ql && r<=qr){
        lazy[pos]^=1;
        FLUSH(pos,l,r);
        return ;
    }
    FLUSH(pos,l,r);
    int mid=(l+r)/2;
    REV(pos*2,l,mid,ql,qr); REV(pos*2+1,mid+1,r,ql,qr);
    seg[pos]=seg[pos*2]+seg[pos*2+1];
}

int NUM(int pos,int l,int r,int ql,int qr){
    if(qr<ql) return 0;
    FLUSH(pos,l,r);
    if(l>qr || r<ql) return 0;
    if(l>=ql && r<=qr) return seg[pos];
    int mid=(l+r)/2;
    return NUM(pos*2,l,mid,ql,qr)+NUM(pos*2+1,mid+1,r,ql,qr);
}

int32_t main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>a[i];
    sort(a,a+n);
    for(int i=0;i<m;i++){
        int l,r;
        cin>>l>>r;
        int il=lower_bound(a,a+n,l)-a,ir=upper_bound(a,a+n,r)-a;
        vec[il].pb({il,ir-1}); vec[ir].pb({il,ir-1});
    }
    int ans=(n*(n-1)*(n-2))/6;
    for(int i=0;i<n;i++){
        REV(1,0,n-1,i,i); //khodesh baraye baadi ha
        for(pii p:vec[i]) REV(1,0,n-1,p.F,p.S); // qry ha
        int x=NUM(1,0,n-1,0,i-1)+NUM(1,0,n-1,i+1,n-1);
        ans-=(x*(x-1))/2;
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define in inline
#define ll long long
const int N=1e5+10;
in ll read()
{
	ll w=0,r=1;
	char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-')r=-1;
		ch=getchar();
	}
	while(isdigit(ch))
	{
		w=(w<<1)+(w<<3)+(ch^48);
		ch=getchar();
	}
	return w*r;
}

ll n,m;
ll a[N];

struct cha
{
	ll l,r;
}ca[N];

ll num[4*N],lazy[4*N];

void change(int k,int l,int r)
{
	num[k]=(r-l+1)-num[k];
	lazy[k]^=1;
}

void pushdown(int k,int l,int r,int mid)
{
	if(lazy[k]==0)return ;
	change(k<<1,l,mid);
	change(k<<1|1,mid+1,r);
	lazy[k]=0;
}

void modify(int k,int l,int r,int x,int y)
{
	if(x<=l&&r<=y)
	{
		change(k,l,r);
		return ;
	}
	int mid=(l+r)>>1;
	pushdown(k,l,r,mid);
	if(x<=mid)modify(k<<1,l,mid,x,y);
	if(y>mid)modify(k<<1|1,mid+1,r,x,y);
	num[k]=num[k<<1]+num[k<<1|1];
}

ll query(int k,int l,int r,int x,int y)
{
	if(x<=l&&r<=y)
	{
		return num[k];
	}
	int mid=(l+r)>>1;
	pushdown(k,l,r,mid);
	ll ans=0;
	if(x<=mid)ans+=query(k<<1,l,mid,x,y);
	if(y>mid)ans+=query(k<<1|1,mid+1,r,x,y);
	return ans;
}

vector<int> pnt1[N],pnt2[N];

ll ans;

int main()
{
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		a[i]=read();
	}
	sort(a+1,a+1+n);
	for(int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		x=lower_bound(a+1,a+1+n,x)-a;
		y=upper_bound(a+1,a+1+n,y)-a-1;
//		cout<<"!"<<x<<" "<<y<<endl;
		if(x>y)continue;
		ca[i].l=x;
		ca[i].r=y;
		pnt1[ca[i].l].push_back(ca[i].r);
		pnt2[ca[i].r].push_back(ca[i].l);
	}
	ans=n*(n-1ll)*(n-2ll)/6ll;
	for(int i=1;i<=n;i++)
	{
//		cout<<pnt1[i].size()<<endl;
		for(int j=0;j<pnt1[i].size();j++)
		{
//			cout<<i<<" "<<pnt1[i][j]<<endl;
			modify(1,1,n,i,pnt1[i][j]);
		}
		ll knum=0;
		if(i>1)knum+=query(1,1,n,1,i-1);
		if(i<n)knum+=(n-i)-query(1,1,n,i+1,n);
		ans-=knum*(knum-1ll)/2ll;
		for(int j=0;j<pnt2[i].size();j++)
		{
			modify(1,1,n,pnt2[i][j],i);
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define in inline
#define ll long long
const int N=1e5+10;
in int read()
{
	int w=0,r=1;
	char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-')r=-1;
		ch=getchar();
	}
	while(isdigit(ch))
	{
		w=(w<<1)+(w<<3)+(ch^48);
		ch=getchar();
	}
	return w*r;
}

int n,m;
int a[N];

struct cha
{
	int l,r;
}ca[N];

ll num[4*N],lazy[4*N];

void change(int k,int l,int r)
{
	num[k]=(r-l+1)-num[k];
	lazy[k]^=1;
}

void pushdown(int k,int l,int r,int mid)
{
	if(lazy[k]==0)return ;
	change(k<<1,l,mid);
	change(k<<1|1,mid+1,r);
	lazy[k]=0;
}

void modify(int k,int l,int r,int x,int y)
{
	if(x<=l&&r<=y)
	{
		change(k,l,r);
		return ;
	}
	int mid=(l+r)>>1;
	pushdown(k,l,r,mid);
	if(x<=mid)modify(k<<1,l,mid,x,y);
	if(y>mid)modify(k<<1|1,mid+1,r,x,y);
	num[k]=num[k<<1]+num[k<<1|1];
}

ll query(int k,int l,int r,int x,int y)
{
	if(x<=l&&r<=y)
	{
		return num[k];
	}
	int mid=(l+r)>>1;
	pushdown(k,l,r,mid);
	ll ans=0;
	if(x<=mid)ans+=query(k<<1,l,mid,x,y);
	if(y>mid)ans+=query(k<<1|1,mid+1,r,x,y);
	return ans;
}

vector<int> pnt1[N],pnt2[N];

ll ans;

int main()
{
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		a[i]=read();
	}
	sort(a+1,a+1+n);
	for(int i=1;i<=m;i++)
	{
		int x=read(),y=read();
		x=lower_bound(a+1,a+1+n,x)-a;
		y=upper_bound(a+1,a+1+n,y)-a-1;
		ca[i].l=x;
		ca[i].r=y;
		pnt1[ca[i].l].push_back(ca[i].r);
		pnt2[ca[i].r].push_back(ca[i].l);
	}
	ans=n*(n-1ll)*(n-2ll)/6ll;
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<pnt1[i].size();j++)
		{
			modify(1,1,n,i,pnt1[i][j]);
		}
		ll knum=0;
		if(i>1)knum+=query(1,1,n,1,i-1);
		if(i<n)knum+=(n-i)-query(1,1,n,i+1,n);
		ans-=knum*(knum-1ll)/2ll;
		for(int j=0;j<pnt2[i].size();j++)
		{
			modify(1,1,n,pnt2[i][j],i);
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
const int N=2e5+5;
inline int getint()
{
	char c;
	int ans=0;
	while(c=getchar(),c>'9' || c<'0');
	while(ans=ans*10+c-'0',c=getchar(),c>='0' && c<='9');
	return ans;
}
int n,m;
int a[N],temp[N];
vector<int> L[N],R[N];
struct Segment{
	int l,r;
	LL sum;
	bool tag;
}t[N<<3];

inline void build_tree(int p,int left,int right)
{
	t[p].l=left,t[p].r=right;
	t[p].sum=t[p].r-t[p].l+1;
	if(left==right) return ;
	int mid=left+right>>1;
	build_tree(p<<1,left,mid);
	build_tree(p<<1|1,mid+1,right);
}
inline void update(int p)
{
	t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
}
inline void pushdown(int p)
{
	if(t[p].tag){
		t[p<<1].sum=t[p<<1].r-t[p<<1].l+1-t[p<<1].sum;
		t[p<<1|1].sum=t[p<<1|1].r-t[p<<1|1].l+1-t[p<<1|1].sum;
		t[p<<1].tag^=1;
		t[p<<1|1].tag^=1;
		t[p].tag=0;
	}
}
inline void mody(int p,int left,int right)
{
	if(t[p].l>=left && t[p].r<=right){
		t[p].sum=(t[p].r-t[p].l+1)-t[p].sum;
		t[p].tag^=1;
		return ;
	}
	if(t[p].tag) pushdown(p);
	int mid=t[p].l+t[p].r>>1;
	if(left<=mid) mody(p<<1,left,right);
	if(right>mid) mody(p<<1|1,left,right);
	update(p);
}
int main()
{
	n=getint(),m=getint();
	for(register int i=1;i<=n;++i) a[i]=getint();
	sort(a+1,a+1+n);
	a[0]=unique(a+1,a+1+n)-a-1;
	int x,y;
	while(m--){
		x=getint(),y=getint();
		x=lower_bound(a+1,a+1+a[0],x)-a;
		y=upper_bound(a+1,a+1+a[0],y)-a-1;
		L[x].push_back(y);
		R[y+1].push_back(x);
	}
	build_tree(1,1,n);
	LL ans=LL(n)*(n-1)*(n-2)/6;
//	printf("basic ans=%lld\n",ans);
	int tot_=0;
	for(register int i=1;i<=n;++i){
		mody(1,i,i);
		int sx=L[i].size();
		for(register int j=0;j<sx;++j)
			mody(1,i,L[i][j]),tot_^=1;
		sx=R[i].size();
		for(register int j=0;j<sx;++j){
			mody(1,R[i][j],i-1),tot_^=1;
		}
		ans-=((LL)(t[1].sum-tot_))*(t[1].sum-tot_-1)/2ll;
		printf("ans=%lld\n",ans);
	}
	printf("%lld\n",ans);
	return 0;
}


#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
const int N=2e5+5;
inline int getint()
{
	char c;
	int ans=0;
//	while(c=getchar(),c>'9' || c<'0');
//	while(ans=ans*10+c-'0',c=getchar(),c>='0' && c<='9');
	scanf("%d",&ans);
	return ans;
}
int n,m;
int a[N];
vector<int> L[N],R[N];
struct Segment{
	int l,r;
	LL sum;
	bool tag;
}t[N<<3];

inline void build_tree(int p,int left,int right)
{
	t[p].l=left,t[p].r=right;
	t[p].sum=t[p].r-t[p].l+1;
	if(left==right) return ;
	int mid=left+right>>1;
	build_tree(p<<1,left,mid);
	build_tree(p<<1|1,mid+1,right);
}
inline void update(int p)
{
	t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
}
inline void pushdown(int p)
{
	if(t[p].tag){
		t[p<<1].sum=t[p<<1].r-t[p<<1].l+1-t[p<<1].sum;
		t[p<<1|1].sum=t[p<<1|1].r-t[p<<1|1].l+1-t[p<<1|1].sum;
		t[p<<1].tag^=1;
		t[p<<1|1].tag^=1;
		t[p].tag=0;
	}
}
inline void mody(int p,int left,int right)
{
	if(left>right) return ;
	if(t[p].l>=left && t[p].r<=right){
		t[p].sum=(t[p].r-t[p].l+1)-t[p].sum;
		t[p].tag^=1;
		return ;
	}
	if(t[p].tag) pushdown(p);
	int mid=t[p].l+t[p].r>>1;
	if(left<=mid) mody(p<<1,left,right);
	if(right>mid) mody(p<<1|1,left,right);
	update(p);
}
int main()
{
	n=getint(),m=getint();
	for(register int i=1;i<=n;++i) a[i]=getint();
	sort(a+1,a+1+n);
	int x,y;
	while(m--){
		x=getint(),y=getint();
		x=lower_bound(a+1,a+1+n,x)-a;
		y=upper_bound(a+1,a+1+n,y)-a-1;
		L[x].push_back(y);
		R[y+1].push_back(x);
	}
	build_tree(1,1,n);
	LL ans=LL(n)*(n-1)*(n-2)/6;
	int tot_=0;
	for(register int i=1;i<=n;++i){
		mody(1,i,i);
		int sx=R[i].size();
		for(register int j=0;j<sx;++j){
			mody(1,R[i][j],i-1),tot_^=1;
		}
		sx=L[i].size();
		for(register int j=0;j<sx;++j)
			mody(1,i,L[i][j]),tot_^=1;
		ans-=((LL)(t[1].sum-tot_))*(t[1].sum-tot_-1)/2ll;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
const int N=2e5+5;
inline int getint()
{
	char c;
	int ans=0;
	while(c=getchar(),c>'9' || c<'0');
	while(ans=ans*10+c-'0',c=getchar(),c>='0' && c<='9');
	return ans;
}
int n,m;
int a[N],temp[N];
vector<int> L[N],R[N];
struct Segment{
	int l,r;
	LL sum;
	bool tag;
}t[N<<3];

inline void build_tree(int p,int left,int right)
{
	t[p].l=left,t[p].r=right;
	t[p].sum=t[p].r-t[p].l+1;
	if(left==right) return ;
	int mid=left+right>>1;
	build_tree(p<<1,left,mid);
	build_tree(p<<1|1,mid+1,right);
}
inline void update(int p)
{
	t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
}
inline void pushdown(int p)
{
	if(t[p].tag){
		t[p<<1].sum=t[p<<1].r-t[p<<1].l+1-t[p<<1].sum;
		t[p<<1|1].sum=t[p<<1|1].r-t[p<<1|1].l+1-t[p<<1|1].sum;
		t[p<<1].tag^=1;
		t[p<<1|1].tag^=1;
		t[p].tag=0;
	}
}
inline void mody(int p,int left,int right)
{
	if(t[p].l>=left && t[p].r<=right){
		t[p].sum=(t[p].r-t[p].l+1)-t[p].sum;
		t[p].tag^=1;
		return ;
	}
	if(t[p].tag) pushdown(p);
	int mid=t[p].l+t[p].r>>1;
	if(left<=mid) mody(p<<1,left,right);
	if(right>mid) mody(p<<1|1,left,right);
	update(p);
}
int main()
{
	n=getint(),m=getint();
	for(register int i=1;i<=n;++i) a[i]=getint();
	sort(a+1,a+1+n);
	a[0]=unique(a+1,a+1+n)-a-1;
	int x,y;
	while(m--){
		x=getint(),y=getint();
		x=lower_bound(a+1,a+1+a[0],x)-a;
		y=upper_bound(a+1,a+1+a[0],y)-a-1;
		L[x].push_back(y);
		R[y+1].push_back(x);
	}
	build_tree(1,1,n);
	LL ans=LL(n)*(n-1)*(n-2)/6;
//	printf("basic ans=%lld\n",ans);
	int tot_=0;
	for(register int i=1;i<=n;++i){
		mody(1,i,i);
		int sx=L[i].size();
		for(register int j=0;j<sx;++j)
			mody(1,i,L[i][j]),tot_^=1;
		sx=R[i].size();
		for(register int j=0;j<sx;++j){
			mody(1,R[i][j],i-1),tot_^=1;
		}
		ans-=((LL)(t[1].sum-tot_))*(t[1].sum-tot_-1)/2ll;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
const int N=2e5+5;
inline int getint()
{
	char c;
	int ans=0;
	while(c=getchar(),c>'9' || c<'0');
	while(ans=ans*10+c-'0',c=getchar(),c>='0' && c<='9');
	return ans;
}
int n,m;
int a[N],temp[N];
vector<int> L[N],R[N];
struct Segment{
	int l,r;
	LL sum;
	bool tag;
}t[N<<3];

inline void build_tree(int p,int left,int right)
{
	t[p].l=left,t[p].r=right;
	t[p].sum=t[p].r-t[p].l+1;
	if(left==right) return ;
	int mid=left+right>>1;
	build_tree(p<<1,left,mid);
	build_tree(p<<1|1,mid+1,right);
}
inline void update(int p)
{
	t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
}
inline void pushdown(int p)
{
	if(t[p].tag){
		t[p<<1].sum=t[p<<1].r-t[p<<1].l+1-t[p<<1].sum;
		t[p<<1|1].sum=t[p<<1|1].r-t[p<<1|1].l+1-t[p<<1|1].sum;
		t[p<<1].tag^=1;
		t[p<<1|1].tag^=1;
		t[p].tag=0;
	}
}
inline void mody(int p,int left,int right)
{
	if(t[p].l>=left && t[p].r<=right){
		t[p].sum=(t[p].r-t[p].l+1)-t[p].sum;
		t[p].tag^=1;
		return ;
	}
	if(t[p].tag) pushdown(p);
	int mid=t[p].l+t[p].r>>1;
	if(left<=mid) mody(p<<1,left,right);
	if(right>mid) mody(p<<1|1,left,right);
	update(p);
}
int main()
{
	n=getint(),m=getint();
	for(register int i=1;i<=n;++i) a[i]=getint();
	sort(a+1,a+1+n);
	a[0]=unique(a+1,a+1+n)-a-1;
	int x,y;
	while(m--){
		x=getint(),y=getint();
		x=lower_bound(a+1,a+1+a[0],x)-a;
		y=upper_bound(a+1,a+1+a[0],y)-a-1;
		L[x].push_back(y);
		R[y+1].push_back(x);
	}
	build_tree(1,1,n);
	LL ans=LL(n)*(n-1)*(n-2)/6;
//	printf("basic ans=%lld\n",ans);
	int tot_=0;
	for(register int i=1;i<=n;++i){
		mody(1,i,i);
		int sx=L[i].size();
		for(register int j=0;j<sx;++j)
			mody(1,i,L[i][j]),tot_^=1;
		sx=R[i].size();
		for(register int j=0;j<sx;++j){
			mody(1,R[i][j],i-1),tot_^=1;
		}
		ans-=((LL)(t[1].sum-tot_))*(t[1].sum-tot_-1)/2ll;
		printf("%lld\n",ans);
	}
	printf("%lld\n",ans);
	return 0;
}


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

#define maxn 100005

using namespace std;

inline int getint()
{
	int num=0,flag=1;char c;
	while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;
	while(c>='0'&&c<='9')num=num*10+c-48,c=getchar();
	return num*flag;
}

int n,K,s[maxn];
int sum[maxn<<2],len[maxn<<2],rev[maxn<<2],flg;
vector<int>L[maxn],R[maxn];

inline void build(int i,int l,int r)
{
	sum[i]=len[i]=r-l+1;
	if(l==r)return;
	int mid=(l+r)>>1;
	build(i<<1,l,mid),build(i<<1|1,mid+1,r);
}

inline void pushdown(int i)
{
	if(!rev[i])return;
	rev[i<<1]^=1,rev[i<<1|1]^=1;
	sum[i<<1]=len[i<<1]-sum[i<<1],sum[i<<1|1]=len[i<<1|1]-sum[i<<1|1];
	rev[i]=0;
}

inline void update(int i,int l,int r,int ql,int qr)
{
	if(ql>r||l>qr)return;
	if(ql<=l&&r<=qr){sum[i]=len[i]-sum[i],rev[i]^=1;return;}
	int mid=(l+r)>>1;pushdown(i);
	update(i<<1,l,mid,ql,qr),update(i<<1|1,mid+1,r,ql,qr);
	sum[i]=sum[i<<1]+sum[i<<1|1];
}

int main()
{
	n=getint(),K=getint();
	for(int i=1;i<=n;i++)s[i]=getint();
	sort(s+1,s+n+1);
	while(K--)
	{
		int x=getint(),y=getint();
		x=lower_bound(s+1,s+n+1,x)-s;
		y=upper_bound(s+1,s+n+1,y)-s-1;
		R[x].push_back(y);
		L[y+1].push_back(x);
	}
	long long ans=1ll*n*(n-1)*(n-2)/6;
	build(1,1,n);
	for(int i=1;i<=n;i++)
	{
		update(1,1,n,i,i);
		for(int j=0;j<L[i].size();j++)update(1,1,n,L[i][j],i-1),flg^=1;
		for(int j=0;j<R[i].size();j++)update(1,1,n,i,R[i][j]),flg^=1;
		ans-=1ll*(sum[1]-flg)*(sum[1]-flg-1)/2;
	}
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
const int N=100005;
vector<int> in[N],out[N];
int a[N],n,k,tot[N*3];
int sum[N*3],rev[N*3];
long long ans;
void chg(int x){
	rev[x]^=1;
	sum[x]=tot[x]-sum[x];
}
void pushdown(int x){
	if (!rev[x]) return;
	chg(x*2); chg(x*2+1); rev[x]=0;
}
void build(int k,int l,int r){
	tot[k]=r-l+1;
	if (l==r) return;
	int mid=(l+r)/2;
	build(k*2,l,mid);
	build(k*2+1,mid+1,r);
}
void reverse(int k,int l,int r,int x,int y){
	if (l==x&&r==y) return chg(k);
	int mid=(l+r)/2;
	pushdown(k);
	if (y<=mid) reverse(k*2,l,mid,x,y);
	else if (x>mid) reverse(k*2+1,mid+1,r,x,y);
	else reverse(k*2,l,mid,x,mid),reverse(k*2+1,mid+1,r,mid+1,y);
	sum[k]=sum[k*2]+sum[k*2+1];
}
int ask(int k,int l,int r,int x,int y){
	if (l==x&&r==y) return sum[k];
	pushdown(k);
	int mid=(l+r)/2;
	if (y<=mid) return ask(k*2,l,mid,x,y);
	if (x>mid) return ask(k*2+1,mid+1,r,x,y);
	return ask(k*2,l,mid,x,mid)+ask(k*2+1,mid+1,r,mid+1,y);
}
int main(){
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	for (int i=1;i<=k;i++){
		int L,R; scanf("%d%d",&L,&R);
		L=lower_bound(a+1,a+n+1,L)-a;
		R=upper_bound(a+1,a+n+1,R)-a-1;
		if (L>=1&&L<=n&&R>=1&&R<=n&&L<=R)
			in[L].push_back(R),out[R].push_back(L);
	}
	ans=1ll*n*(n-1)*(n-2)/6ll;
	build(1,1,n);
	for (int i=1;i<=n;i++){
		for (int j=0;j<in[i].size();j++)
			reverse(1,1,n,i,in[i][j]);
		int tmp=0;
		if (i>1) tmp+=i-1-ask(1,1,n,1,i-1);
		if (i<n) tmp+=ask(1,1,n,i+1,n);
		ans-=1ll*tmp*(tmp-1)/2;
		for (int j=0;j<out[i].size();j++)
			reverse(1,1,n,out[i][j],i);
	}
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
const int N=100005;
vector<int> in[N],out[N];
int a[N],n,k,tot[N*3];
int sum[N*3],rev[N*3];
long long ans;
void chg(int x){
	rev[x]^=1;
	sum[x]=tot[x]-sum[x];
}
void pushdown(int x){
	if (!rev[x]) return;
	chg(x*2); chg(x*2+1); rev[x]=0;
}
void build(int k,int l,int r){
	tot[k]=r-l+1;
	if (l==r) return;
	int mid=(l+r)/2;
	build(k*2,l,mid);
	build(k*2+1,mid+1,r);
}
void reverse(int k,int l,int r,int x,int y){
	if (l==x&&r==y) return chg(k);
	int mid=(l+r)/2;
	pushdown(k);
	if (y<=mid) reverse(k*2,l,mid,x,y);
	else if (x>mid) reverse(k*2+1,mid+1,r,x,y);
	else reverse(k*2,l,mid,x,mid),reverse(k*2+1,mid+1,r,mid+1,y);
	sum[k]=sum[k*2]+sum[k*2+1];
}
int ask(int k,int l,int r,int x,int y){
	if (l==x&&r==y) return sum[k];
	pushdown(k);
	int mid=(l+r)/2;
	if (y<=mid) return ask(k*2,l,mid,x,y);
	if (x>mid) return ask(k*2+1,mid+1,r,x,y);
	return ask(k*2,l,mid,x,mid)+ask(k*2+1,mid+1,r,mid+1,y);
}
int main(){
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	for (int i=1;i<=k;i++){
		int L,R; scanf("%d%d",&L,&R);
		L=lower_bound(a+1,a+n+1,L)-a;
		R=upper_bound(a+1,a+n+1,R)-a-1;
		if (L>=1&&L<=n&&R>=1&&R<=n)
			in[L].push_back(R),out[R].push_back(L);
	}
	ans=1ll*n*(n-1)*(n-2)/6ll;
	build(1,1,n);
	for (int i=1;i<=n;i++){
		for (int j=0;j<in[i].size();j++)
			reverse(1,1,n,i,in[i][j]);
		int tmp=0;
		if (i>1) tmp+=i-1-ask(1,1,n,1,i-1);
		if (i<n) tmp+=ask(1,1,n,i+1,n);
		ans-=1ll*tmp*(tmp-1)/2;
		for (int j=0;j<out[i].size();j++)
			reverse(1,1,n,out[i][j],i);
	}
	printf("%lld\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
const int N=100005;
vector<int> in[N],out[N];
int a[N],n,k,tot[N*3];
int sum[N*3],rev[N*3];
long long ans;
void chg(int x){
	rev[x]^=1;
	sum[x]=tot[x]-sum[x];
}
void pushdown(int x){
	if (!rev[x]) return;
	chg(x*2); chg(x*2+1); rev[x]=0;
}
void build(int k,int l,int r){
	tot[k]=r-l+1;
	if (l==r) return;
	int mid=(l+r)/2;
	build(k*2,l,mid);
	build(k*2+1,mid+1,r);
}
void reverse(int k,int l,int r,int x,int y){
	if (l==x&&r==y) return chg(k);
	int mid=(l+r)/2;
	pushdown(k);
	if (y<=mid) reverse(k*2,l,mid,x,y);
	else if (x>mid) reverse(k*2+1,mid+1,r,x,y);
	else reverse(k*2,l,mid,x,mid),reverse(k*2+1,mid+1,r,mid+1,y);
	sum[k]=sum[k*2]+sum[k*2+1];
}
int ask(int k,int l,int r,int x,int y){
	if (l==x&&r==y) return sum[k];
	pushdown(k);
	int mid=(l+r)/2;
	if (y<=mid) return ask(k*2,l,mid,x,y);
	if (x>mid) return ask(k*2+1,mid+1,r,x,y);
	return ask(k*2,l,mid,x,mid)+ask(k*2+1,mid+1,r,mid+1,y);
}
int main(){
	scanf("%d%d",&n,&k);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	for (int i=1;i<=k;i++){
		int L,R; scanf("%d%d",&L,&R);
		L=lower_bound(a+1,a+n+1,L)-a;
		R=upper_bound(a+1,a+n+1,R)-a-1;
		if (L>=1&&L<=n&&R>=1&&R<=n)
			in[L].push_back(R),out[R].push_back(L);
	}
	ans=1ll*n*(n-1)*(n-2)/6ll;
	build(1,1,n);
	for (int i=1;i<=n;i++){
		for (int j=0;j<in[i].size();j++)
			reverse(1,1,n,i,in[i][j]);
		int tmp=0;
		if (i>1) tmp+=i-1-ask(1,1,n,1,i-1);
		if (i<n) tmp+=ask(1,1,n,i+1,n);
		printf("%d %d\n",i,tmp);
		ans-=1ll*tmp*(tmp-1)/2;
		for (int j=0;j<out[i].size();j++)
			reverse(1,1,n,out[i][j],i);
	}
	printf("%lld\n",ans);
}


n,m = map(int,input().split())
c=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
u=0
for t in c:
    if c[0]<=a[0]:
        u+=1
        del c[0]
        del a[0]
        if a==False:
            break
print(u)


n,m = map(int,input().split())
c=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
u=0
for t in range(n):
    if c[0]<=a[0]:
        u+=1
        del a[0]
        m-=1
        if m==0:
            break
    del c[0]
print(u)


input().split()
c = list(map(int, input().split()))
a = list(map(int, input().split()))
# if min(a) >= max(c):
#     print(len(a))
#     exit()
if a[0] < min(c):
    print(0)
    exit()
counter = 0
i = 0
j = 0
while j < len(c):
    if a[i] >= c[j]:
        counter += 1
        i, j = 1 + i, 1 + j
    elif a[i] < c[j]:
        j += 1
print(counter)

# HaPpY NoWrUz 1398
# UBCF
# CodeForcesian
# ♥


input().split()
c = list(map(int, input().split()))
a = list(map(int, input().split()))
# if min(a) >= max(c):
#     print(min(len(a), len(c)))
#     exit()
# if a[0] < min(c):
#     print(0)
#     exit()
counter = 0
i = 0
j = 0
while j < len(c) and i < len(a):
    if a[i] >= c[j]:
        counter += 1
        i, j = 1 + i, 1 + j
    elif a[i] < c[j]:
        j += 1
print(counter)

# HaPpY NoWrUz 1398
# UBCF
# CodeForcesian
# ♥


input().split()
c = list(map(int, input().split()))
a = list(map(int, input().split()))
# if min(a) >= max(c):
#     print(min(len(a), len(c)))
#     exit()
# if a[0] < min(c):
#     print(0)
#     exit()
counter = 0
i = 0
j = 0
while j < len(c) or i < len(a):
    if a[i] >= c[j]:
        counter += 1
        i, j = 1 + i, 1 + j
    elif a[i] < c[j]:
        j += 1
print(counter)

# HaPpY NoWrUz 1398
# UBCF
# CodeForcesian
# ♥


input().split()
c = list(map(int, input().split()))
a = list(map(int, input().split()))
if min(a) >= max(c):
    print(min(len(a), len(c)))
    exit()
if a[0] < min(c):
    print(0)
    exit()
counter = 0
i = 0
j = 0
while j < len(c):
    if a[i] >= c[j]:
        counter += 1
        i, j = 1 + i, 1 + j
    elif a[i] < c[j]:
        j += 1
print(counter)

# HaPpY NoWrUz 1398
# UBCF
# CodeForcesian
# ♥


input().split()
c = list(map(int, input().split()))
a = list(map(int, input().split()))
if min(a) >= max(c):
    print(len(a))
    exit()
if a[0] < min(c):
    print(0)
    exit()
counter = 0
i = 0
j = 0
while j < len(c):
    if a[i] >= c[j]:
        counter += 1
        i, j = 1 + i, 1 + j
    elif a[i] < c[j]:
        j += 1
print(counter)

# HaPpY NoWrUz 1398
# UBCF
# CodeForcesian
# ♥


n,m=map(int,input().strip().split())
a=list(map(int,input().strip().split()))
b=list(map(int,input().strip().split()))
cnt=0
i,j=0,0
for _ in range(min(n,m)):
    if a[i]<=b[i]:
        cnt+=1
        i+=1
        j+=1
    else:
        i+=1
print(cnt)


n,m=map(int,input().strip().split())
a=list(map(int,input().strip().split()))
b=list(map(int,input().strip().split()))
cnt=0
i,j=0,0
while True:
    if i==n or j==m:
        break
    else:
        if a[i]<=b[j]:
            cnt+=1
            i+=1
            j+=1
        else:
            i+=1
print(cnt)


input()
games = [int(x) for x in input().split()]
wallet = [int(x) for x in input().split()]

numgames = 0
for game in games:
    for i in range(len(wallet)):
        bill = wallet[i]
        if game <= bill:
            #print(bill)
            del wallet[i]
            #print(wallet)
            numgames += 1
            break

print(numgames)


input()
games = [int(x) for x in input().split()]
wallet = [int(x) for x in input().split()]

numgames = 0
for game in games:
    for i in range(len(wallet)):
        bill = wallet[i]
        if game < bill:
            #print(bill)
            del wallet[i]
            #print(wallet)
            numgames += 1
            break

print(numgames)


input()
games = [int(x) for x in input().split()]
wallet = [int(x) for x in input().split()]

numgames = 0
for game in games:
    if len(wallet):
        if wallet[0] >= game:
            del wallet[0]
            numgames += 1
    else:
        break

print(numgames)


n, m = map(int, input().split())
C, A = list(map(int, input().split())), list(map(int, input().split()))
i = j = 0

while i < n and j < m:
    if C[i] <= A[j]:
        j += 1
    i += 1

print(j)


n, m = map(int, input().split())
C, A = list(map(int, input().split())), list(map(int, input().split()))
i = j = games = 0

while i < n and j < m:
    if C[i] <= A[j]:
        j, games = j + 1, games + 1
    i += 1

print(games)


def count (a , c):
	num = 0
	while len(c) != 0 and len(a) != 0:
		if a[0] >= c[0]:
			a.pop(0)
			c.pop(0)
			num += 1
		else:
			c.pop(0)

	return num

n , m = input().split()
c = list(input().split())
a = list(input().split())
#for i in range(int(n)):
	#c.append(int(input()))
#for i in range(int(m)):
#	a.append(int(input()))
print(count(a,c))


def count (a , c):
	num = 0
	while len(c) != 0 and len(a) != 0:
		if a[0] >= c[0]:
			a.pop(0)
			c.pop(0)
			num += 1
		else:
			c.pop(0)

	return num

n = input()
m = input()
c = []
a = []
for i in range(int(n)):
	c.append(int(input()))
for i in range(int(m)):
	a.append(int(input()))
print(count(a,c))


def count (a , c):
	num = 0
	while len(c) != 0 and len(a) != 0:
		if a[0] >= c[0]:
			a.pop(0)
			c.pop(0)
			num += 1
		else:
			c.pop(0)

	return num

n , m = input().split()
c = list(input().split())
a = list(input().split())
for i in range(len(c)):
	c[i] = int (c[i])
for i in range(len(a)):
	a [i] = int (a[i])
print(count(a,c))


if __name__ == '__main__':
    x = input().split()
    z = input().split()
    l = []
    for i in range(len(z)):
        l.append(int(z[i]))
    y = input().split()
    m = []
    for i in range(len(y)):
        m.append(int(y[i]))
    n = j = 0
    for i in range(len(l)):
        if m[j] >= l[i]:
            n += 1
            j += 1
        if j == len(m):
            break
    print(n)


if __name__ == '__main__':
    x = input().split()
    z = input().split()
    l = []
    for i in range(len(z)):
        l.append(int(z[i]))
    y = input().split()
    m = []
    for i in rage(len(y)):
        m.append(int(m[i]))
    n = j = 0
    for i in range(len(l)):
        if m[j] >= l[i]:
            n += 1
            j += 1
        if j == len(m)-1:
            break
    print(n)


if __name__ == '__main__':
    x = input().split()
    z = input().split()
    l = []
    for i in range(len(z)):
        l.append(int(z[i]))
    y = input().split()
    m = []
    for i in range(len(y)):
        m.append(int(y[i]))
    n = j = 0
    for i in range(len(l)):
        if m[j] >= l[i]:
            n += 1
            j += 1
        if j == len(m)-1:
            break
    print(n)


n,m=list(map(int,input().split()))
c=list(map(int,input().split()))
a=list(map(int,input().split()))
count=0
for i in c:
    if len(a)==0:
        break
    if a[0]>=i:
        a.remove(a[0])
        count+=1
print(count)


n,m = map(int,input().split())
games = list(map(int,input().split()))
bills = list(map(int,input().split()))
counter = 0
i = 0
j = 0
while len(bills) != 0 and len(games)!=0:
    while i<=len(games) and j<=len(bills):
        try:
            if bills[j]>=games[i]:
                counter += 1
                i += 1
                j += 1
            else:
                i+=1
        except:
            print(counter)
            bills[:] = []


n,m = map(int,input().split())
games = list(map(int,input().split()))
bills = list(map(int,input().split()))
counter = 0
i = 0
j = 0
while len(bills) != 0 and len(games)!=0:
    while i<=len(games) and j<=len(bills):
        try:
            if bills[j]>=games[i]:
                counter += 1
                i += 1
                j += 1
            else:
                i+=1
        except:
            print(counter)
            bills[:] = []
            games[:] = []


n, m = map(int, input().split())
cost = list(map(int, input().split()))
wallet = list(map(int, input().split()))
count = 0
for i in range(len(wallet)):
    for j in range(len(cost)):
        try:
            if wallet[i] > cost[j]:
                wallet.pop(0)
                count += 1
        except:
            print(count)
            exit()
print(count)


n, m = map(int, input().split())
cost = list(map(int, input().split()))
wallet = list(map(int, input().split()))
count = 0
for i in range(len(wallet)):
    for j in range(len(cost)):
        try:
            if wallet[i] > cost[j]:
                wallet.pop(0)
                count += 1
        except IndexError:
            print(count)
            exit()
print(count)


n, m = map(int, input().split())
cost = list(map(int, input().split()))
wallet = list(map(int, input().split()))
count = 0
length = list([len(cost), len(wallet)])
length.sort()
for i in range(length[1]):
    try:
        if wallet[0] >= cost[0]:
            count += 1
            wallet.pop(0)
            cost.pop(0)
            continue
        else:
            cost.pop(0)
    except IndexError:
        print(count)
        exit()
print(count)


n,k=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
c=0
w=0
for i in range(n):
    if c<k and p[i]<=q[c]:
        c+=1
        w+=1
print(w)


n,k=map(int,input().split())
p=list(map(int,input().split()))
q=list(map(int,input().split()))
c=0
w=0
for i in range(n):
    if p[i]<=q[c]:
        c+=1
        w+=1
print(w)


input()
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
while a and b:
    if a.pop() <= b[-1]:
        b.pop()
        ans += 1
print(ans)


input()
a = list(map(int, input().split()))[::-1]
b = list(map(int, input().split()))[::-1]
ans = 0
while a and b:
    if a.pop() <= b[-1]:
        b.pop()
        ans += 1
print(ans)


n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pos = 0
for i in a:
	pos += (pos < len(b) and b[pos] >= i)
print(pos)


n,m=map(int,input().split())
c=list(map(int,input().split()))
a=list(map(int,input().split()))
i=0;cnt=0
while(a!=[] and i!=len(c)):
    if(a[0]>=c[i]):
        cnt=cnt+1
        a.remove(a[0])
        i=i+1
    else:
        i=i+1
print(cnt)


n,m=map(int,input().split())
c=list(map(int,input().split()))
a=list(map(int,input().split()))
cnt=0
while(a!=[] and c!=[]):
    if(a[0]>=c[0]):
        cnt=cnt+1
        a.remove(a[0])
        c.remove(c[0])
    else:
        c.remove(c[0])
print(cnt)


def fair(a,b,n,m):
    j,c=0,0
    for i in range(m):
        while j<n:
            if b[i]>=a[j]:
                c+=1
                j+=1
                break
            j+=1
    print(c)

ca=list(map(int,input().split()))
n,m=ca[0],ca[1]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
fair(a,b,n,m)


def fair(a,b,n,m):
    j,c=0,0
    for i in range(m):
        while j<n:
            if b[i]>a[j]:
                c+=1
                j+=1
                break
            j+=1
    print(c)

ca=list(map(int,input().split()))
n,m=ca[0],ca[1]
a=list(map(int,input().split()))
b=list(map(int,input().split()))
fair(a,b,n,m)


# import sys
# sys.stdin=open("input1.in","r")
# sys.stdout=open("output2.out","w")
N,M=map(int,input().split())
L=list(map(int,input().split()))
S=list(map(int,input().split()))
count=0
j=0
for i in range(N):
	if j<M:
		if S[j]>=L[i]:
			j+=1
			count+=1

print(count)


# import sys
# sys.stdin=open("input1.in","r")
# sys.stdout=open("output2.out","w")
N,M=map(int,input().split())
L=list(map(int,input().split()))
S=list(map(int,input().split()))
count=0
j=0
for i in range(N):
	if S[j]>=L[i]:
		j+=1
		count+=1

print(count)


# import sys
# sys.stdin=open("input1.in","r")
# sys.stdout=open("output2.out","w")
N,M=map(int,input().split())
L=list(map(int,input().split()))
S=list(map(int,input().split()))
count=0
for i in range(len(L)):
	for j in range(len(S)):
		if S[j]>=L[i]:
			S[j]=-1
			count+=1
			break
print(count)


# import sys
# sys.stdin=open("input1.in","r")
# sys.stdout=open("output2.out","w")
N,M=map(int,input().split())
L=list(map(int,input().split()))
S=list(map(int,input().split()))
count=0
for i in range(len(L)):
	for j in range(len(S)):
		if S[j]>L[i]:
			S[j]=-1
			count+=1
			break
print(count)


def main():
    inp = open('input.txt','r')
    out = open('output.txt','w')
    n,m = list(map(int,inp.readline().split()))
    c = list(map(int,inp.readline().split()))
    a = list(map(int,inp.readline().split()))
    for i in range(n):
        if len(a)==0 or len(c)==0:
            break
        elif a[0]>=c[0]:
            a.pop(0)
            c.pop(0)
            continue
        c.pop(0)
    out.write(str(m-len(a))+'\n')
    inp.close()
    out.close()

if __name__ == '__main__':
	main()


inp = input().split()
n,m = inp[0],inp[1]
c = inp[2:2+n]
a = inp[2+n:]
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
print(str(m-len(a)))


inp = open('input.txt','r')
out = open('output.txt','w')
n,m = list(map(int,inp.readline().split()))
c = list(map(int,inp.readline().split()))
a = list(map(int,inp.readline().split()))
inp.close()
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
out.write(str(m-len(a))+'\n')
out.close()


inp = list(map(int,input().split()))
n,m = inp[0],inp[1]
c = inp[2:2+n]
a = inp[2+n:]
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    if a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
print(str(m-len(a)))


def one(inp):
    n,m = list(map(int,inp[0].split()))
    c = list(map(int,inp[1].split()))
    a = list(map(int,inp[2].split()))
    for i in range(n):
        if len(a)==0 or len(c)==0:
            break
        elif a[0]>=c[0]:
            a.pop(0)
            c.pop(0)
            continue
        c.pop(0)
    return(m-len(a))

if __name__ == '__main__':
	main()


inp = input().split()
n,m = inp[0],inp[1]
c = inp[2:2+n]
a = inp[2+n:2+n+m]
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
print(str(m-len(a))+'\n')


def one():
    inp = open('input.txt','r')
    out = open('output.txt','w')
    n,m = list(map(int,inp.readline().split()))
    c = list(map(int,inp.readline().split()))
    a = list(map(int,inp.readline().split()))
    for i in range(n):
        if len(a)==0 or len(c)==0:
            break
        elif a[0]>=c[0]:
            a.pop(0)
            c.pop(0)
            continue
        c.pop(0)
    out.write(str(m-len(a))+'\n')
    inp.close()
    out.close()

if __name__ == '__main__':
	main()


inp = open('input.txt','r')
out = open('output.txt','w')
n,m = list(map(int,inp.readline().split()))
c = list(map(int,inp.readline().split()))
a = list(map(int,inp.readline().split()))
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
out.write(str(m-len(a))+'\n')
out.close()


inp = open('inp.txt','r')
out = open('out.txt','w')
n,m = list(map(int,inp.readline().split()))
c = list(map(int,inp.readline().split()))
a = list(map(int,inp.readline().split()))
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
out.write(str(m-len(a))+'\n')
inp.close()
out.close()


inp = open('input.txt','r')
out = open('output.txt','w')
n,m = list(map(int,inp.readline().split()))
c = list(map(int,inp.readline().split()))
a = list(map(int,inp.readline().split()))
inp.close()
for i in range(n):
    if len(a)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)

out.write(str(m-len(a)))
print(m-len(a))
out.close()


inp = input().split('\n')

n,m = list(map(int,inp.[0].split()))
c = list(map(int,inp.[1].split()))
a = list(map(int,inp.[2].split()))
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
print(str(m-len(a))+'\n')


inp = input().split()
n,m = inp[0],inp[1]
c = inp[2:2+n-1]
a = inp[2+n-1:]
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
print(str(m-len(a)))


inp = input().split()
n,m = inp[0],inp[1]
c = inp[2:2+n]
a = inp[2+n:]
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    if a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
print(str(m-len(a)))


inp = input().split()
n,m = inp[0],inp[1]
c = inp[2:2+n]
a = inp[2+n+1:]
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
print(str(m-len(a)))


inp = list(map(int,input().split()))
n,m = inp[0],inp[1]
c = inp[2:2+n]
a = inp[2+n:]
for j in range(m):
    if len(a)==0 or len(c)==0:

        break
    if a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
print(str(m-len(a)))


inp = open('input.txt','r')
out = open('output.txt','w')
n,m = list(map(int,inp.readline().split()))
c = list(map(int,inp.readline().split()))
a = list(map(int,inp.readline().split()))
for i in range(n):
    if len(a)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
out.write(str(m-len(a)))
inp.close()
out.close()


inp = open('inp.txt','r')
out = open('out.txt','w')
n,m = list(map(int,inp.readline().split()))
c = list(map(int,inp.readline().split()))
a = list(map(int,inp.readline().split()))
inp.close()
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
out.write(str(m-len(a))+'\n')
out.close()


n,m = list(map(int,input().split()))
c = list(map(int,input().split()))
a = list(map(int,input().split()))
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    if a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
print(str(m-len(a)))


inp = input()
n,m = list(map(int,inp.readline().split()))
c = list(map(int,inp.readline().split()))
a = list(map(int,inp.readline().split()))
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
print(str(m-len(a)))


inp = open('input.txt','r')
out = open('out.txt','w')
n,m = list(map(int,inp.readline().split()))
c = list(map(int,inp.readline().split()))
a = list(map(int,inp.readline().split()))
inp.close()
for i in range(n):
    if len(a)==0 or len(c)==0:
        break
    elif a[0]>=c[0]:
        a.pop(0)
        c.pop(0)
        continue
    c.pop(0)
out.write(str(m-len(a))+'\n')
out.close()


def main(inp):
    n,m = list(map(int,inp[0].split()))
    c = list(map(int,inp[1].split()))
    a = list(map(int,inp[2].split()))
    for i in range(n):
        if len(a)==0 or len(c)==0:
            break
        elif a[0]>=c[0]:
            a.pop(0)
            c.pop(0)
            continue
        c.pop(0)
    main.return(m-len(a))

if __name__ == '__main__':
	main()


n,m = map(int,input().split())
l = list(map(int,input().split()))
k = list(map(int,input().split()))
ans = 0
while len(l) > 0 and len(k) > 0 and len(l) > len(k):
        if l[0] <= k[0]:
            ans += 1
            del(l[0])
            del(k[0])
        else:
            del(l[0])
print(ans)


n,m = map(int,input().split())
l = list(map(int,input().split()))
k = list(map(int,input().split()))
ans = 0
while len(l) != 0:
    if l[0] <= k[0]:
        ans += 1
        del(l[0])
        del(k[0])
    else:
        del(l[0])
print(ans)


n,m = map(int,input().split())
l = list(map(int,input().split()))
k = list(map(int,input().split()))
ans = 0
while len(l) != 0 and len(k) != 0 and len(l) > len(k):
        if l[0] <= k[0]:
            ans += 1
            del(l[0])
            del(k[0])
        else:
            del(l[0])
print(ans)


n,m = map(int,input().split())
l = list(map(int,input().split()))
k = list(map(int,input().split()))
ans = 0
while len(l) > 0 and len(k) > 0:
        if l[0] <= k[0]:
            ans += 1
            del(l[0])
            del(k[0])
        else:
            del(l[0])
print(ans)


l = lambda: map(int, input().split())
n, m = l()
C = list(l())
A = list(l())
c= 0
j = 0
for i in C:
    if j < m and i <= A[j]:
        c += 1
        j += 1
print(c)


a = lambda: map(int, input().split())
n,m = a()
l=list(a())
d =list(a())
c= 0
j = 0
for i in l:
    if j < m and i <= d[j]:
        c += 1
        j += 1
print(c)


input_nm=input().split(" ")
games=input().split(" ")
money=input().split(" ")
start_g=0
count=0
for now in money:
    if now>games[start_g]:
        start_g=start_g+1
        count=count+1


input_nm=(input().split(" "))
games=(input().split(" "))
money=(input().split(" "))
start_g=0
count=0
money = [int(item) for item in money]
games = [int(item) for item in games]
for now in games:
    if (now)<=money[start_g]:
        count=count+1
        if start_g<int(input_nm[1])-1:
            start_g=start_g+1
        else:
            break
print(count)


input_nm=input().split(" ")
games=input().split(" ")
money=input().split(" ")
start_g=0
count=0
for now in money:
    if now>=games[start_g]:
        start_g=start_g+1
        count=count+1
print(count)


input_nm=input().split(" ")
games=input().split(" ")
money=input().split(" ")
start_g=0
count=0
for now in money:
    if now>games[start_g]:
        start_g=start_g+1
        count=count+1
print(count)


def go():
    n, m = [int(i) for i in input().split(' ')]
    a = [int(i) for i in input().split(' ')]
    b = [int(i) for i in input().split(' ')]
    i = j = 0
    while i < n and j < m:
        if a[i] <= b[j]:
            j += 1
        i += 1
    return j

print(go())


def go():
    vowels = set(['a', 'o', 'u', 'i', 'e'])
    a = [i for i in input()]
    for i in range(len(a) - 1):
        if a[i] not in vowels and a[i + 1] not in vowels and a[i] != 'n':
            return 'NO'
    if a[-1] != 'n' and a[-1] not in vowels:
        return 'NO'
    return 'YES'

print(go())


n,m=map(int,input().split())
c=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
count=j=0
if n>=m:
    x=n
else:
    x=m
for i in range(x):
    if a[j]>=c[i]:
        j+=1
        count+=1
    if j==m or i==n:
        break
print(count)


n,m=map(int,input().split())
c=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
count=j=0
if n>=m:
    x=n
else:
    x=m
for i in range(x):
    if j==m or i==n:
        break
    if a[j]>=c[i]:
        j+=1
        count+=1
print(count)


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

c = list(map(int,input().split()))
a = list(map(int,input().split()))

r = 0
w = 0

for i in range(n):
    if w >= len(a):
        break
    if c[i] <= a[w]:
        w += 1
        r += 1

print(r)


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

cost = list(map(int,input().split()))
bill = list(map(int,input().split()))

j=0
count=0
for i in range(n):
    if cost[i]<=bill[j]:
        count+=1
        j+=1

    if i>=n-1 or i>=m-1:
        break


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

cost = list(map(int,input().split()))
bill = list(map(int,input().split()))

j=0
count=0
for i in range(n):
    if cost[i]<=bill[j]:
        count+=1
        j+=1

    if i>=n-1 or i>=m-1:
        break

print(count)


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

cost = list(map(int,input().split()))
bill = list(map(int,input().split()))

j=0
count=0
for i in range(n):
    if cost[i]<=bill[j]:
        count+=1
        j+=1

    if i>=n or j>=m:
        break

print(count)


n,m = map(int,input().split())
a = list(map(int,input().split()))
c = list(map(int,input().split()))
count = 0
j = 0
for i in c:
    while a[j] > i:
        j +=1
        if j == n:
            print(0)
            exit()
    count += 1
    j += 1
    if j == n:
        break
print(count)


n, m = map(int, input().split())
a = map(int, input().split())
c = map(int, input().split())
count = 0
for i in c:
    for j in a:
        if j <= i:
            count += 1
            break
print(count)


nm = input().split(' ')
n = int(nm[0])
m = int(nm[1])

c = input().split(' ')
a = input().split(' ')
i = 0
for x in a :
    for y in c:
        if int(y)<=int(x) :
            i += 1
            c.remove(y)
            break
        else :
            c.remove(y)

print(i)


"""
Your module description
"""
nm = input().split(' ')
n = int(nm[0])
m = int(nm[1])

c = input().split(' ')
a = input().split(' ')
i = 0
j = 0
for x in a :
    if j < len(c) :
        for y in range(j,len(c)) :
            if int(x)>=int(c[y]) :
                j = y+1
                i += 1
                break
            else :
                j = y+1
print(i)


"""
Your module description
"""
nm = input().split(' ')
n = int(nm[0])
m = int(nm[1])

c = input().split(' ')
a = input().split(' ')
i = 0
j = 0
for x in a :
    if j < len(c) :
        for y in range(j,len(c)) :
            if int(x)>=int(c[y]) :
                j = y+1
                i += 1
                break
            else :
                j = y
print(i)


n,m = list(map(int,input().split()))
c = list(map(int,input().split()[:n]))
a = list(map(int,input().split()[:m]))

count = 0
for i in range(len(c)):
	if c[i] <= a[0]:
		count+=1
		a.remove(a[0])
	if len(a) == 0:
		break
print(count)


def countBuys(c, b):
    if len(c) == 0 or len(b) == 0:
        return 0
    if len(c) == 0:
        return 0
    if b[0] >= c[0]:
        return 1 + countBuys(c[1:], b[1:])
    else:
        return countBuys(c[1:], b)

s = input().split()
n = int(s[0])
m = int(s[1])
c = list(map(int, input().split()))
b = list(map(int, input().split()))
print(countBuys(c, b))


s = input().split()
n = int(s[0])
m = int(s[1])
c = list(map(int, input().split()))
b = list(map(int, input().split()))
b_idx = c_idx = 0
cnt = 0
while c_idx < len(c) and b_idx < len(b):
    if b[b_idx] >= c[c_idx]:
        cnt += 1
        b_idx += 1
    c_idx += 1

print(cnt)


n, m = list(map(int, input().split()))
c = list(map(int, input().split()))
a = list(map(int, input().split()))
i, j = 0, 0
x = 0
while i < n and j < m:
    if a[j] >= c[i]:
        x += 1
        i += 1
        j += 1
    else:
        i += 1
print(c)


n, m = list(map(int, input().split()))
c = list(map(int, input().split()))
a = list(map(int, input().split()))
i, j = 0, 0
c = 0
while i < n and j < m:
    if a[j] >= c[i]:
        c += 1
        i += 1
        j += 1
    else:
        i += 1
print(c)


n, m = list(map(int, input().split()))
c = list(map(int, input().split()))
a = list(map(int, input().split()))
i, j = 0, 0
x = 0
while i < n and j < m:
    if a[j] >= c[i]:
        x += 1
        i += 1
        j += 1
    else:
        i += 1
print(x)


_,a,b=$<.map{|e|e.split.map &:to_i}
r=0
a.each{|e|b[r].to_i>=e&&r+=1}
p r


N, M = gets.split.map(&:to_i)
cs = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)
ans = 0
cs.each do |c|
    if c <= as[0]
        ans += 1
        as.shift
        break if as.length == 0
    end
end
puts ans


n, m = gets.strip.split.map(&:to_i)
cost = gets.strip.split.map(&:to_i)
bills = gets.strip.split.map(&:to_i)

ans = 0
j = 0

0.upto(n-1).each do |i|
  if cost[i] <= bills[j]
    ans += 1
    j += 1
  end
  break if j >= m
end

puts ans


n, m = gets.split.map(&:to_i)
cs = gets.split.map(&:to_i)
as = gets.split.map(&:to_i)

ans = 0
a = as.shift
(0..n-1).each do |i|
  if a >= cs[i]
    ans += 1
    a = as.shift
  end
  break unless a
end

puts ans


n, m = gets.split.map &:to_i
c = gets.split.map &:to_i
a = gets.split.map &:to_i
ngames = 0
(0...n).each do |i|
  if a[0] && a[0] >= c[i]
    ngames += 1
    a.shift
  end
end
puts ngames


tmp=gets.split
n=tmp[0].to_i
m=tmp[1].to_i
c=gets.split
a=gets.split
i=0
j=0
ans=0
while i<n and j<m
	#puts "#{i} #{n}/ #{j} #{m}"
	a[j]=a[j].to_i
	c[i]=c[i].to_i
	#puts a[j]>c[i]
	if a[j]>=c[i]
		ans=ans+1
		j=j+1
	end
	i=i+1
end
puts ans


n,m=gets.split.map(&:to_i)
c=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)
ans=0
c.each{|v|
  break if a.empty?
  if v<=a.first
    ans+=1
    a.shift
  end
}

puts ans


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define per(i, a, b) for(int i=(b)-1; i>=(a); i--)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll<<(x))
#define endl "\n"
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int P = 1e9 + 7;
int add(int a, int b) {if((a += b) >= P) a -= P; return a;}
int sub(int a, int b) {if((a -= b) < 0) a += P; return a;}
int mul(int a, int b) {return 1ll * a * b % P;}
int kpow(int a, int b) {int r=1;for(;b;b>>=1,a=mul(a,a)) {if(b&1)r=mul(r,a);}return r;}
//----

const int N = 505;
int a[N], n, f[N][N];
//unordered_map<int, int> f[N][N];

int work(int l, int r) {
	if (l >= r) return 1;
	if (f[l][r] != -1) return f[l][r];
	int res = 0;
	rep(i, l, r+1) {
		if (i + 1 > r || a[l] < a[i+1]) res = add(res, mul(work(l+1, i), work(i+1, r)));
	}
	return f[l][r] = res;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	//cout << setiosflags(ios::fixed);
	//cout << setprecision(2);
	cin >> n;
	memset(f, -1, sizeof(f));
	rep(i, 1, n+1) cin >> a[i];
	cout << work(2, n) << endl;
	//rep(i, 2, n+1) rep(j, i, n+1) dd(i),dd(j),de(f[i][j]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define per(i, a, b) for(int i=(b)-1; i>=(a); i--)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define pw(x) (1ll<<(x))
#define endl "\n"
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int P = 1e9 + 7;
int add(int a, int b) {if((a += b) >= P) a -= P; return a;}
int sub(int a, int b) {if((a -= b) < 0) a += P; return a;}
int mul(int a, int b) {return 1ll * a * b % P;}
int kpow(int a, int b) {int r=1;for(;b;b>>=1,a=mul(a,a)) {if(b&1)r=mul(r,a);}return r;}
//----

const int N = 505;
int a[N], n;
unordered_map<int, int> f[N][N];

int work(int l, int r, int lim) {
	if (l > r) return 1;
	if (a[l] <= lim) return 0;
	if (l == r) return 1;
	if (f[l][r].count(lim)) return f[l][r][lim];
	int res = 0;
	rep(i, l, r+1) {
		res = add(res, mul(work(l+1, i, 1), work(i+1, r, max(lim, a[l]))));
	}
	return f[l][r][lim] = res;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	//cout << setiosflags(ios::fixed);
	//cout << setprecision(2);
	cin >> n;
	rep(i, 1, n+1) cin >> a[i];
	cout << work(2, n, 1) << endl;
	//rep(i, 2, n+1) rep(j, i, n+1) dd(i),dd(j),de(f[i][j]);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <bits/stdc++.h>
#include <fstream>

#include <bits/stdc++.h>
#define MOD 1000000007

class FProgressMonitoring {
public:
	void solve(std::istream& in, std::ostream& out) {
	    int n; in >> n;
	    long long dp[n+1][n+1];
	    int arr[n];
	    for(int i = 0; i<n; i++) in >> arr[i];
	    for(int i = 0; i<=n; i++) dp[i][i] = 1;
	    for(int len = 1; len <=n; len++){
	        for(int i = 0; i+len <= n; i++){
	            int j = i+len;
	            dp[i][j] = 0;
	            for(int k = i+1; k<=j; k++){
	                if(k == j || arr[k] > arr[i]) dp[i][j] = (dp[i][j]+dp[i+1][k]*dp[k][j]%MOD)%MOD;
	            }
	        }
	    }
	    out << dp[1][n] << endl;
	}
};

int main() {
	FProgressMonitoring solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Locke Cai
 */

#include <bits/stdc++.h>

#include <bits/stdc++.h>
#define MOD 1000000007

class FProgressMonitoring {
public:
    void solve(std::istream& in, std::ostream& out) {
        int n; in >> n;
        long long dp[n+1][n+1];
        int arr[n];
        for(int i = 0; i<n; i++) in >> arr[i];
        for(int i = 0; i<=n; i++) dp[i][i] = 1;
        for(int len = 1; len <=n; len++){
            for(int i = 0; i+len <= n; i++){
                int j = i+len;
                dp[i][j] = 0;
                for(int k = i+1; k<=j; k++){
                    if(k == j || arr[k] > arr[i]) dp[i][j] = (dp[i][j]+dp[i+1][k]*dp[k][j]%MOD)%MOD;
                }
            }
        }
        out << dp[1][n];
    }
};

int main() {
	FProgressMonitoring solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAX_N = 505;
const long long MOD = 1000000007;
int a[MAX_N];
int n;
long long dp[MAX_N][MAX_N];

int main () {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    dp[i][i] = 1;
  }
  for (int r = 2; r <= n; r++) {
    for (int l = r - 1; l >= 1; l--) {
      for (int k = l + 1; k <= r; k++) {
        if (k == r || a[l + 1] < a[k + 1]) {
          (dp[l][r] += (dp[l + 1][k] * dp[k][r]) % MOD) %= MOD;
        }
      }
    }
  }
  printf("%lld", dp[1][n]);
  return 0;
}


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl;
#define Get(x,y) scanf("%I64d%I64d",&x,&y);
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define Gett(x,y) scanf("%d%d",&x,&y);
#define f(i,a,b) for(int i=a;i<b;i++)
#define get(x) scanf("%I64d",&x);
#define gett(x) scanf("%d",&x);
#define ops(x) cout<<x<<" !"<<endl;
#define maxm(a,b) a=max(a,b);
#define minm(a,b) a=min(a,b);
#define mp make_pair
#define ll long long
#define pb push_back
#define add(x,y) x=(x+y)%mod;
#define S second
#define F first

const int N=505,mod=1e9+7;

int n,a[N],dp[N][N];

int pd(int l,int r){
	return max(dp[l+1][r],1);
}
void solve(int l,int r){
	dp[l][r]=max(1,dp[l+1][r]);
	f(i,l,r)
		if(a[l]<a[i+1])
			add(dp[l][r],1ll*pd(l,i)*dp[i+1][r]%mod);
}

int main(){
	cin>>n;
	f(i,1,n+1) cin>>a[i];
	f(r,1,n+1)
		f_(l,r,1)
			solve(l,r);
	cout<<pd(1,n);
}