#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
signed main() {
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        int i = 0;
        if(n == 1)
        {
            cout<<1337<<endl;
            continue;
        }
        if(n == 2)
        {
            cout<<13337<<endl;
            return 0;
        }
        if(n == 3)
        {
            cout<<133737<<endl;
            return 0;
        }
        for(; i * i  + 3*i<= n; i++)
        {
            // \_(-_-)_/ flex
        }
        i--;
        //cout<<i<<endl;
       deque<char> s;
       int cnt = n - (i * i + 4*i);
        s.push_front('1');
        s.push_back('3'), s.push_back('3');
        for(int j = 1; j < i; j++)
            s.push_back('1');
        for(int j = 0; j < cnt; j++)
            s.push_back('7');
        s.push_back('3'), s.push_back('3');
        for(int j = 0; j < i; j++)
            s.push_back('7');
        for(auto k : s)
            cout<<k;
        cout<<endl;
    }
}


#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
signed main() {
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        int i = 0;
        if(n == 1)
        {
            cout<<1337<<endl;
            continue;
        }
        if(n == 2)
        {
            cout<<13337<<endl;
            return 0;
        }
        if(n == 3)
        {
            cout<<133737<<endl;
            return 0;
        }
        for(; i * i  + 3*i<= n; i++)
        {
            // \_(-_-)_/ flex
        }
        i--;
       deque<char> s;
       int cnt = n - (i * i + 3*i);
        for(int j = 0; j < i; j++)
            s.push_front('1');
        s.push_back('3'), s.push_back('3');
        for(int j = 0; j < cnt; j++)
            s.push_back('7');
        s.push_back('3'), s.push_back('3');
        for(int j = 0; j < i; j++)
            s.push_back('7');
        for(auto k : s)
            cout<<k;
        cout<<endl;
    }
}


#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
signed main() {
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        int i = 0;
        if(n == 1)
        {
            cout<<1337<<endl;
            continue;
        }
        if(n == 2)
        {
            cout<<13337<<endl;
            return 0;
        }
        if(n == 3)
        {
            cout<<133737<<endl;
            return 0;
        }
        for(; i * i  + 3*i<= n; i++)
        {
            // \_(-_-)_/ flex
        }
        i--;
        cout<<i<<endl;
       deque<char> s;
       int cnt = n - (i * i + 4*i);
        s.push_front('1');
        s.push_back('3'), s.push_back('3');
        for(int j = 1; j < i; j++)
            s.push_back('1');
        for(int j = 0; j < cnt; j++)
            s.push_back('7');
        s.push_back('3'), s.push_back('3');
        for(int j = 0; j < i; j++)
            s.push_back('7');
        for(auto k : s)
            cout<<k;
        cout<<endl;
    }
}


#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
signed main() {
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        int i = 1;
        for(; (i * (i - 1)) / 2 <= n; i++)
        {
            // \_(-_-)_/ flex
        }
        i--;

        int cnt = n - ((i - 1) * i) / 2;
        cout<<133;
        for(int j = 0; j < cnt; j++)
            cout<<7;
        for(int j = 0; j < i - 2;j++)
            cout<<3;
        cout<<7<<endl;
    }
}


#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
signed main() {
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        int i = 0;
        if(n == 1)
        {
            cout<<1337<<endl;
            continue;
        }
        if(n == 2)
        {
            cout<<13337<<endl;
            return 0;
        }
        if(n == 3)
        {
            cout<<133737<<endl;
            return 0;
        }
        for(; i * i  + 5*i<= n; i++)
        {
            // \_(-_-)_/ flex
        }
        i--;
       // cout<<i<<endl;
       deque<char> s;
       int cnt = n - (i * i + 5*i);
        s.push_front('1');
        s.push_back('3'), s.push_back('3');
        for(int j = 1; j < i; j++)
            s.push_back('1');
        for(int j = 0; j < cnt; j++)
            s.push_back('7');
        s.push_back('3'), s.push_back('3');
        for(int j = 0; j < i; j++)
            s.push_back('7');
        for(auto k : s)
            cout<<k;
        cout<<endl;
    }
}


#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
signed main() {
    int q;
    cin>>q;
    while(q--)
    {
        int n;
        cin>>n;
        int i = 0;
        if(n == 1)
        {
            cout<<1337<<endl;
            continue;
        }
        if(n == 2)
        {
            cout<<13337<<endl;
            return 0;
        }
        if(n == 3)
        {
            cout<<133737<<endl;
            return 0;
        }
        for(; i * i  + 3*i<= n; i++)
        {
            // \_(-_-)_/ flex
        }
        i--;
       deque<char> s;
       int cnt = n - (i * i + 3*i);
        for(int j = 0; j < i; j++)
            s.push_front('1');
        s.push_back('3'), s.push_back('3');
        for(int j = 0; j < cnt; j++)
            s.push_back('7');
        s.push_back('3'), s.push_back('3');
        for(int j = 0; j < i; j++)
            s.push_back('7');
        for(auto k : s)
            cout<<k;
    }
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ii pair<int ,int>
#define iii tuple<int ,int, int>
#define vi vector<int>
#define vii vector<int, int>
#define fo(i,a,n) for(int i = (a); i < (n); i++)
#define pb push_back
#define newl cout<<"\n"
#define mp make_pair
#define mt make_tuple
#define f first
#define s second
#define N1 100000

//int visited[N1]={};
//int st[4*N1 + 10]={};

void findPair(ll &x, ll &z, ll n){
	for(ll i=(ll)pow(n,0.5); i>0; i--){
		if(n%i==0){
				z=i;
				x=n/i;
				break;
		}
	}
}

int main(){
	int t;
	cin>>t;
	ll n;
	//char s[500000];

	ll pr[500000];
	map<ll,int> map;
	pr[1]=1;
	fo(i,2,50000){
		pr[i]=pr[i-1]+i;
		//cout<<pr[i]<<" ";
	}

	/*fo(i,0,200){
		cout<<pr[i]<<" ";
	}
	newl;*/

	//newl;
	while(t--){
		cin>>n;
		ll x,y,z;
		x=y=z=0;
		ll f=1;
		ll k;

		for(ll i=(ll)pow(n,0.5); i>2; i--){
			if(n%i==0){
				k=lower_bound(pr,pr+50000,i)-pr+1;
				if(k<49999){
					//cout<<k<<":";
					if(n%pr[k-1]==0){
						f=0;
						y=k;
						findPair(x,z,n/k);
						break;

					}

				}
			}
		}

		if(f){
			//cout<<"YO";
			findPair(x,z,n);
			y=2;

		}

		while(x--){
			cout<<"1";
		}
		while(y--){
			cout<<"3";
		}
		while(z--){
			cout<<"7";
		}

		newl;
		}

}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ii pair<int ,int>
#define iii tuple<int ,int, int>
#define vi vector<int>
#define vii vector<int, int>
#define fo(i,a,n) for(int i = (a); i < (n); i++)
#define pb push_back
#define newl cout<<"\n"
#define mp make_pair
#define mt make_tuple
#define f first
#define s second
#define N1 100000

//int visited[N1]={};
//int st[4*N1 + 10]={};

int main(){
	int t;
	cin>>t;
	ll n;
	char s[500000];
	while(t--){
		cin>>n;
		int x,y,z;
		x=y=z=0;
		if(n==1){
			cout<<"1337\n";
			continue;
		}

		if(n%2){
			y=2;
			for(int i=1; i*i<=n; i++){
				if(n%i==0){
					x=i;
					z=n/i;
					break;
				}
			}

			while(x--){
				cout<<"1";
			}
			cout<<"33";
			while(z--){
				cout<<"7";
			}

		}
		else{
			cout<<"133";
			z=n;
			while(z--){
				cout<<"7";
			}

		}

		newl;
	}
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ii pair<int ,int>
#define iii tuple<int ,int, int>
#define vi vector<int>
#define vii vector<int, int>
#define fo(i,a,n) for(int i = (a); i < (n); i++)
#define pb push_back
#define newl cout<<"\n"
#define mp make_pair
#define mt make_tuple
#define f first
#define s second
#define N1 100000

int main(){
	int t;
	cin>>t;
	ll n;

	ll pr[500000];

	pr[1]=1;
	fo(i,2,50000){
		pr[i]=pr[i-1]+i;

	}

	while(t--){
		cin>>n;
		ll x,y,z;
		x=y=z=0;
		ll k;

		k=upper_bound(pr,pr+50000,n)-pr;
		//cout<<"hey:"<<k-1<<":"<<pr[k-1];
		//newl;
		x=1;
		y=k;
		z=n-pr[k-1];

		//cout<<x<<" "<<y<<" "<<z;
		//newl;
		cout<<133;
		while(z>0){
			cout<<"7";
			z--;
		}

		y-=2;
		while(y>0){
			cout<<"3";
			y--;
		}
		cout<<"7";
		newl;
	}

}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ii pair<int ,int>
#define iii tuple<int ,int, int>
#define vi vector<int>
#define vii vector<int, int>
#define fo(i,a,n) for(int i = (a); i < (n); i++)
#define pb push_back
#define newl cout<<"\n"
#define mp make_pair
#define mt make_tuple
#define f first
#define s second
#define N1 100000

//int visited[N1]={};
//int st[4*N1 + 10]={};

void findPair(ll &x, ll &z, ll n){
	for(ll i=(ll)pow(n,0.5); i>0; i--){
		if(n%i==0){
				z=i;
				x=n/i;
				break;
		}
	}
}

int main(){
	int t;
	cin>>t;
	ll n;
	//char s[500000];

	ll pr[500000];
	map<ll,int> map;
	pr[1]=1;
	fo(i,2,50000){
		pr[i]=pr[i-1]+i;
		//cout<<pr[i]<<" ";
	}

	/*fo(i,0,200){
		cout<<pr[i]<<" ";
	}
	newl;*/

	//newl;
	while(t--){
		cin>>n;
		ll x,y,z;
		x=y=z=0;
		ll f=1;
		ll k;

		for(ll i=(ll)pow(n,0.5); i>2; i--){
			if(n%i==0){
				k=lower_bound(pr,pr+50000,i)-pr+1;
				if(k<49999){
					//cout<<k<<":";
					if(n%pr[k-1]==0){
						f=0;
						y=k;
						findPair(x,z,n/k);
						break;

					}

				}
			}
		}

		if(f){
			cout<<"YO";
			findPair(x,z,n);
			y=2;

		}

		while(x--){
			cout<<"1";
		}
		while(y--){
			cout<<"3";
		}
		while(z--){
			cout<<"7";
		}

		newl;
		}

}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ii pair<int ,int>
#define iii tuple<int ,int, int>
#define vi vector<int>
#define vii vector<int, int>
#define fo(i,a,n) for(int i = (a); i < (n); i++)
#define pb push_back
#define newl cout<<"\n"
#define mp make_pair
#define mt make_tuple
#define f first
#define s second
#define N1 100000

//int visited[N1]={};
//int st[4*N1 + 10]={};

void findPair(ll &x, ll &z, ll n){
	for(ll i=(ll)pow(n,0.5); i>0; i--){
		if(n%i==0){
				z=i;
				x=n/i;
				break;
		}
	}
}

int main(){
	int t;
	cin>>t;
	ll n;
	//char s[500000];

	ll pr[500000];
	map<ll,int> map;
	pr[1]=1;
	fo(i,2,50000){
		pr[i]=pr[i-1]+i;
		//cout<<pr[i]<<" ";
	}

	fo(i,0,200){
		cout<<pr[i]<<" ";
	}
	newl;

	//newl;
	while(t--){
		cin>>n;
		ll x,y,z;
		x=y=z=0;
		ll f=1;
		ll k;

		for(ll i=(ll)pow(n,0.5); i>2; i--){
			if(n%i==0){
				k=lower_bound(pr,pr+50000,i)-pr+1;
				if(k<49999){
					cout<<k<<":";
					if(n%pr[k-1]==0){
						f=0;
						y=k;
						findPair(x,z,n/k);
						break;

					}

				}
			}
		}

		if(f){
			cout<<"YO";
			findPair(x,z,n);
			y=2;

		}

		while(x--){
			cout<<"1";
		}
		while(y--){
			cout<<"3";
		}
		while(z--){
			cout<<"7";
		}

		newl;
		}

}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ii pair<int ,int>
#define iii tuple<int ,int, int>
#define vi vector<int>
#define vii vector<int, int>
#define fo(i,a,n) for(int i = (a); i < (n); i++)
#define pb push_back
#define newl cout<<"\n"
#define mp make_pair
#define mt make_tuple
#define f first
#define s second
#define N1 100000

//int visited[N1]={};
//int st[4*N1 + 10]={};

void findPair(int &x, int &z, int n){
	for(int i=(int)pow(n,0.5); i>0; i--){
		if(n%i==0){
				z=i;
				x=n/i;
				break;
		}
	}
}

int main(){
	int t;
	cin>>t;
	ll n;
	char s[500000];

	ll pr[500000];
	map<ll,int> map;
	pr[1]=1;
	fo(i,2,50000){
		pr[i]=pr[i-1]+i;
		//cout<<pr[i]<<" ";
	}

	//newl;
	while(t--){
		cin>>n;
		int x,y,z;
		x=y=z=0;
		int f=1;
		int k;

		for(int i=(int)pow(n,0.5); i>2; i--){
			if(n%i==0){
				k=lower_bound(pr,pr+50000,i)-pr+1;
				if(k<49999){
					//cout<<k<<":";
					if(n%pr[k-1]==0){
						f=0;
						y=k;
						findPair(x,z,n/k);
						break;

					}

				}
			}
		}

		if(f){
			//cout<<"YO";
			findPair(x,z,n);
			y=2;

		}

		while(x--){
			cout<<"1";
		}
		while(y--){
			cout<<"3";
		}
		while(z--){
			cout<<"7";
		}

		newl;
		}

}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ii pair<int ,int>
#define iii tuple<int ,int, int>
#define vi vector<int>
#define vii vector<int, int>
#define fo(i,a,n) for(int i = (a); i < (n); i++)
#define pb push_back
#define newl cout<<"\n"
#define mp make_pair
#define mt make_tuple
#define f first
#define s second
#define N1 100000

//int visited[N1]={};
//int st[4*N1 + 10]={};

void findPair(ll &x, ll &z, ll n){
	for(ll i=(ll)pow(n,0.5); i>0; i--){
		if(n%i==0){
				z=i;
				x=n/i;
				break;
		}
	}
}

int main(){
	int t;
	cin>>t;
	ll n;
	//char s[500000];

	/*ll pr[500000];
	map<ll,int> map;
	pr[1]=1;*/
	/*fo(i,2,50000){
		pr[i]=pr[i-1]+i;
		//cout<<pr[i]<<" ";
	}*/

	/*fo(i,0,200){
		cout<<pr[i]<<" ";
	}
	newl;*/

	//newl;
	while(t--){
		cin>>n;
		ll x,y,z;
		x=y=z=0;
		ll f=1;
		ll k;

		for(ll i=(ll)pow(n,0.5); i>2; i--){
			if(n%i==0){
				/*k=lower_bound(pr,pr+50000,i)-pr+1;
				if(k<49999){
					//cout<<k<<":";
					if(n%pr[k-1]==0){
						f=0;
						y=k;
						findPair(x,z,n/k);
						break;

					}

				}*/
				ll r=pow(i,0.5);
				if(r%1==0){
					if(r%2){
						//cout<<i<<":"<<r<<":YES:";
						f=0;
						y=i;
						findPair(x,z,n/y);
						break;
					}
				}
			}
		}

		if(f){
			//cout<<"YO";
			findPair(x,z,n);
			y=2;

		}

		while(x--){
			cout<<"1";
		}
		while(y--){
			cout<<"3";
		}
		while(z--){
			cout<<"7";
		}

		newl;
		}

}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ii pair<int ,int>
#define iii tuple<int ,int, int>
#define vi vector<int>
#define vii vector<int, int>
#define fo(i,a,n) for(int i = (a); i < (n); i++)
#define pb push_back
#define newl cout<<"\n"
#define mp make_pair
#define mt make_tuple
#define f first
#define s second
#define N1 100000

//int visited[N1]={};
//int st[4*N1 + 10]={};

void findPair(ll &x, ll &z, ll n){
	for(ll i=(ll)pow(n,0.5); i>0; i--){
		if(n%i==0){
			z=i;
			x=n/i;
			break;
		}
	}
}

int main(){
	int t;
	cin>>t;
	ll n;

	ll pr[500000];

	pr[1]=1;
	fo(i,2,50000){
		pr[i]=pr[i-1]+i;

	}

	while(t--){
		cin>>n;
		ll x,y,z;
		x=y=z=0;
		ll k;

		ll N=sqrt(n);
		k=lower_bound(pr,pr+50000,n)-pr;
		x=1;
		y=k;
		z=n-pr[k-1]-1;

		cout<<133;
		while(z>0){
			cout<<"7";
			z--;
		}

		y-=2;
		while(y>0){
			cout<<"3";
			y--;
		}
		cout<<"7";
		newl;
	}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long t,n,k,d,p,c;
	string s;
	cin>>t;
	while(t--)
	{
		 c=0;
		cin>>n;
		k=n;
		while(1)
		{
			d=sqrt(1+8*n);
			if (d*d!=1+8*n)
				n--,c++;
			else
			{
				p=(1+d)/2;
				break;
			}
		}

	cout<<133;

	for (long long i = 0; i < c; ++i)
	{
		cout<<7;
	}
	for (long long i = 0; i < p-2; ++i)
	{
		cout<<3;
	}
	cout<<7<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long t,n,s,d,k,e;
	cin>>t;
	while(t--)
	{
		s=1;
		cin>>n;
		while(1)
		{
			if (n%2==0)
				s++,n/=2;
			else
			{
				//s--;
				n+=n;
				//k=pow(2,s);
				d=sqrt(1+(4*n));
				d=(d+1)/2;
				e=(d-1)/2;
				if (e>=2)
					d=e;
			break;
			}
		}
		//s++;
		for (long long i = 0; i < s; ++i)
		{
			cout<<1;
		}
		for (long long i = 0; i < d; ++i)
		{
			cout<<3;
		}
		cout<<7<<endl;
	}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long t,n,s,d,k;
	cin>>t;
	while(t--)
	{
		s=1;
		cin>>n;
		while(1)
		{
			if (n%2==0)
				s++,n/=2;
			else
			{
				//s--;
				n+=n;
				//k=pow(2,s);
				d=sqrt(1+(4*n));
				d=(d+1)/2;
				e=(d-1)/2;
				if (e>=2)
					d=e;
			break;
			}
		}
		//s++;
		for (long long i = 0; i < s; ++i)
		{
			cout<<1;
		}
		for (long long i = 0; i < d; ++i)
		{
			cout<<3;
		}
		cout<<7<<endl;
	}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long t,n,s,d,k;
	cin>>t;
	while(t--)
	{
		s=1;
		cin>>n;
		while(1)
		{
			if (n%2==0)
				s++,n/=2;
			else
			{
			//	s--;
				//k=pow(2,s);
				d=sqrt(1+(4*n));
				d=(d+1)/2;
			break;
			}
		}
		//s++;
		for (long long i = 0; i < s; ++i)
		{
			cout<<1;
		}
		for (long long i = 0; i <= d; ++i)
		{
			cout<<3;
		}
		cout<<7<<endl;
	}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long t,n,k,d,p,c;
	string s;
	cin>>t;
	while(t--)
	{
		 c=0;
		cin>>n;
		k=n;
		while(1)
		{
			d=sqrt(1+8*n);
			if (d*d!=1+8*n)
				n--,c++;
			else
			{
				p=(1+d)/2;
				break;
			}
		}

	cout<<1;
	if (c)
		cout<<33;
	for (long long i = 0; i < c; ++i)
	{
		cout<<7;
	}
	for (long long i = 0; i < p; ++i)
	{
		cout<<3;
	}
	cout<<7<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long t,n,s,d,k,e;
	cin>>t;
	while(t--)
	{
		s=1;
		cin>>n;
		while(1)
		{
			//if (n%2==0)
			//	s++,n/=2;
			//else
			//{
				//s--;
				//n+=n;
				//k=pow(2,s);
				d=sqrt(1+(8*n));
				if (d*d!=1+8*n)
				{
					n--,s++;
					//cout<<"e"<<endl;
				}
				else if (d%2)
				{
				d=(d+1)/2;

			break;
		}
		else
			n--,s++;
			//}
		}
		//s++;
		for (long long i = 0; i < s; ++i)
		{
			cout<<1;
		}
		for (long long i = 0; i < d; ++i)
		{
			cout<<3;
		}
		cout<<7<<endl;
	}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long t,n,s,d,k;
	cin>>t;
	while(t--)
	{
		s=1;
		cin>>n;
		while(1)
		{
			if (n%2==0)
				s++,n/=2;
			else
			{
				//s--;
				n+=n;
				//k=pow(2,s);
				d=sqrt(1+(4*n));
				d=(d+1)/2;
			break;
			}
		}
		//s++;
		for (long long i = 0; i < s; ++i)
		{
			cout<<1;
		}
		for (long long i = 0; i < d; ++i)
		{
			cout<<3;
		}
		cout<<7<<endl;
	}

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long t,n,k,d,p,c;
	string s;
	cin>>t;
	while(t--)
	{
		 c=0;
		cin>>n;
		k=n;
		while(1)
		{
			d=sqrt(1+8*n);
			if (pow(d,2)!=1+8*n)
				n--,c++;
			else
			{
				p=(1+d)/2;
				break;
			}
		}

	cout<<1;
	if (c)
		cout<<33;
	for (long long i = 0; i < c; ++i)
	{
		cout<<7;
	}
	for (long long i = 0; i < p; ++i)
	{
		cout<<3;
	}
	cout<<7<<endl;
}}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define D1(i,j) printf("[%d]%d\n",i,j);
#define D2(i,j) printf("%s=%d\n",i,j);
const int N=5e5+5;
const int mod=1e9+7;

LL n;
LL c[N], tot;

int main()
{
    c[tot=1]=0;
    for(int i=2;c[tot]<1e9;i++)
        c[++tot]=i*(i-1)/2;

    int _; scanf("%d",&_);
    while(_--) {
        scanf("%lld",&n);
        int k1=1;
        for(int i=2;i*i<=n;i++) if(n%i==0) {
            k1=i; break;
        } n/=k1;
        vector<int>s7;
        while(n) {
            int id=lower_bound(c+2,c+tot,n)-c;
            if(c[id]>n)id--;
            s7.push_back(id);//printf("%d\n",id);
            n-=c[id];
        }
        reverse(s7.begin(),s7.end());
        while(k1--) printf("1");
        int len=s7.size(), ind=0;
        for(int i=1;i<=s7[len-1];i++) {
            printf("3");
            if(ind<len && s7[ind]==i) printf("7"), ind++;
        } printf("\n");
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define D1(i,j) printf("[%d]%d\n",i,j);
#define D2(i,j) printf("%s=%d\n",i,j);
const int N=5e5+5;
const int mod=1e9+7;

LL n;
LL c[N], tot;

int main()
{
    c[tot=1]=0;
    for(int i=2;c[tot]<1e9;i++)
        c[++tot]=i*(i-1)/2;

    int _; scanf("%d",&_);
    while(_--) {
        scanf("%lld",&n);
        int k1=1;
        for(int i=2;i*i<=n;i++) if(n%i==0) {
            k1=i;
        } n/=k1;
        vector<int>s7;
        while(n) {
            int id=lower_bound(c+2,c+tot,n)-c;
            if(c[id]>n)id--;
            s7.push_back(id);//printf("%d\n",id);
            n-=c[id];
        }
    //printf("%d\n",k1+s7.size()+s7[0]);
        reverse(s7.begin(),s7.end());
        while(k1--) printf("1");
        int len=s7.size(), ind=0;
        for(int i=1;i<=s7[len-1];i++) {
            printf("3");
            if(ind<len && s7[ind]==i) printf("7"), ind++;
        } printf("\n");
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define D1(i,j) printf("[%d]%d\n",i,j);
#define D2(i,j) printf("%s=%d\n",i,j);
const int N=5e5+5;
const int mod=1e9+7;

LL n;
LL c[N], tot;

int main()
{
    c[tot=1]=0;
    for(int i=2;c[tot]<1e9;i++)
        c[++tot]=i*(i-1)/2;

    int _; scanf("%d",&_);
    while(_--) {
        scanf("%lld",&n);
        int k1=1;
        vector<int>s7;
        while(n) {
            int id=lower_bound(c+2,c+tot+1,n)-c;
            if(c[id]>n)id--;
            s7.push_back(id);//printf("%d\n",id);
            n-=c[id];
        }
    //printf("%d\n",k1+s7.size()+s7[0]);
        reverse(s7.begin(),s7.end());
        while(k1--) printf("1");
        int len=s7.size(), ind=0;
        for(int i=1;i<=s7[len-1];i++) {
            printf("3");
            while(ind<len && s7[ind]==i) printf("7"), ind++;
        } printf("\n");
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define D1(i,j) printf("[%d]%d\n",i,j);
#define D2(i,j) printf("%s=%d\n",i,j);
const int N=2e6+5;
const int mod=1e9+7;

LL n;

int main()
{
    int _; scanf("%d",&_);
    while(_--) {
        scanf("%lld",&n);
        int k1=1,k2=2,k3=1;
        for(int i=(int)sqrt(2*n);i>=2;i--)
            if((2*n)%(i*(i-1))==0) {
                k2=i; break;
            }
        n=n/(k2*(k2-1)/2);
        //printf("%d %d\n",k2*(k2-1)/2,n);
        k1=1, k3=n;
        for(int i=2;i*i<=n;i++)
            if(n%i==0) { k1=i, k3=n/i; }
        //printf("%d %d %d\n",k1,k2,k3);
        while(k1--) printf("1");
        while(k2--) printf("3");
        while(k3--) printf("7");
        puts("");
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define D1(i,j) printf("[%d]%d\n",i,j);
#define D2(i,j) printf("%s=%d\n",i,j);
const int N=5e5+5;
const int mod=1e9+7;

LL n;
LL c[N], tot;

int main()
{
    c[tot=1]=0;
    for(int i=2;c[tot]<1e9;i++)
        c[++tot]=i*(i-1)/2;

    int _; scanf("%d",&_);
    while(_--) {
        scanf("%lld",&n);
        int k1=1;
        vector<int>s7;
        while(n) {
            int id=lower_bound(c+2,c+tot,n)-c;
            if(c[id]>n)id--;
            s7.push_back(id);//printf("%d\n",id);
            n-=c[id];
        }
    //printf("%d\n",k1+s7.size()+s7[0]);
        reverse(s7.begin(),s7.end());
        while(k1--) printf("1");
        int len=s7.size(), ind=0;
        for(int i=1;i<=s7[len-1];i++) {
            printf("3");
            if(ind<len && s7[ind]==i) printf("7"), ind++;
        } printf("\n");
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define inc(i,j,k) for(int i=j;i<=k;i++)
#define dec(i,j,k) for(int i=j;i>=k;i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define D1(i,j) printf("[%d]%d\n",i,j);
#define D2(i,j) printf("%s=%d\n",i,j);
const int N=5e5+5;
const int mod=1e9+7;

LL n;
LL c[N], tot;

int main()
{
    c[tot=1]=0;
    for(int i=2;c[tot]<1e9;i++)
        c[++tot]=i*(i-1)/2;

    int _; scanf("%d",&_);
    while(_--) {
        scanf("%lld",&n);
        int k1=1;
        for(int i=2;i*i<=n;i++)
            if(n%i==0) k1=i;
        n/=k1;
        vector<int>s7;
        while(n) {
            int id=lower_bound(c+2,c+tot+1,n)-c;
            if(c[id]>n)id--;
            s7.push_back(id);//printf("%d\n",id);
            n-=c[id];
        }
    //printf("%d\n",k1+s7.size()+s7[0]);
        reverse(s7.begin(),s7.end());
        while(k1--) printf("1");
        int len=s7.size(), ind=0;
        for(int i=1;i<=s7[len-1];i++) {
            printf("3");
            while(ind<len && s7[ind]==i) printf("7"), ind++;
        } printf("\n");
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{fast;
       long long m;
       cin>>m;
       while(m--)
       {
   long long n;
   cin>>n;
   ll k=2;
   ll prev=2;
   ll val=2;
   ll ans=1,ansk;
   while(1)
   {
          val=k*(k-1)/2;
          if(val>n)
          break;
                 ans=val;ansk=k;
        //  cout<<val<<endl;
          k++;
   }
  // cout<<ansk<<" "<<ans<<endl;
   long long ele1=1;long long ele2=1;
   ll n1=n%ans;
        //  cout<<ele1<<" "<<ele2<<endl;
  //for(int i=0;i<;i++)
  cout<<"1";
  for(int i=0;i<ansk;i++)
  cout<<"3";
 // for(int i=0;i<ele2;i++)
  cout<<"7";
  for(int i=0;i<n1;i++)
  {
   cout<<"337";
  }
  cout<<"\n";
       }
       return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
       long long m;
       cin>>m;
       while(m--)
       {
   long long n;
   cin>>n;
   for(int i=0;i<n;i++)
   {
          cout<<"1";
   }
   cout<<"337";
   cout<<endl;
       }
       return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{fast;
       long long m;
       cin>>m;
       while(m--)
       {
   long long n;
   cin>>n;
   ll k=2;
   ll prev=2;
   ll val=2;
   ll ans=1,ansk;
   while(1)
   {
          val=k*(k-1)/2;
          if(val>n)
          break;
                 ans=val;ansk=k;
        //  cout<<val<<endl;
          k++;
   }
 //  cout<<ansk<<" "<<ans<<endl;
   long long ele1=1;long long ele2=1;
     n=n/ans;
     //cout<<n<<endl;
          for(int i=1;i<=sqrt(n);i++)
          {
                 if(n%i==0)
                 {
                     ele1=i;
                     ele2=n/i;
                 }
          }
        //  cout<<ele1<<" "<<ele2<<endl;
  for(int i=0;i<ele1;i++)
  cout<<"1";
  for(int i=0;i<ansk;i++)
  cout<<"3";
  for(int i=0;i<ele2;i++)
  cout<<"7";
  for(int i=0;i<n;i++)
  {
   cout<<"337";
  }
  cout<<"\n";
       }
       return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{fast;
       long long m;
       cin>>m;
       vector<long long>v;
       while(m--)
       {
   long long n;
   cin>>n;
   ll k=2;
   ll prev=2;
   ll val=2;
   ll ans=1,ansk;
   if(n==1)
   {
          cout<<"1337"<<endl;continue;
   }
   while(n)
   {k=2;prev=2;val=2;
          while(1)
          {
          val=k*(k-1)/2;
          if(val>n)
          break;
          ans=val;ansk=k;
          //cout<<val<<endl;
          prev=val;
          k++;
          }
          //cout<<prev<<endl;
          v.push_back(ansk);
          n=n-prev;
          if(n==1)
          {v.push_back(2);n=n-1;
          break;}
   }
  // cout<<n<<endl;
   ll last=n;
   vector<long long>v1;
   sort(v.begin(),v.end());
   /*for(int i=0;i<v.size();i++)
   {
          cout<<v[i]<<" ";
   }
   cout<<endl;*/
   v1.push_back(v[0]);
   for(int i=1;i<v.size();i++)
   v1.push_back(v[i]-v[i-1]);
  /* for(int i=0;i<v1.size();i++)
   {
          cout<<v1[i]<<" ";
   }
   cout<<endl;*/
   cout<<"1";
      for(int i=0;i<v1.size();i++)
       {
         while(v1[i]--)
         cout<<"3";
         cout<<"7";
       }
       for(int i=0;i<last;i++)
       cout<<"7";
       cout<<"\n";
       }
       return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{fast;
       long long m;
       cin>>m;
       while(m--)
       {
   long long n;
   cin>>n;
   ll k=2;
   ll prev=2;
   ll val=2;
   ll ans=1,ansk;
   while(1)
   {
          val=k*(k-1)/2;
          if(val>n)
          break;
                 ans=val;ansk=k;
        //  cout<<val<<endl;
          k++;
   }
  // cout<<ansk<<" "<<ans<<endl;
   long long ele1=1;long long ele2=1;
   ll n1=n%ans;
     n=n/ans;
     //cout<<n<<endl;
          for(int i=1;i<=sqrt(n);i++)
          {
                 if(n%i==0)
                 {
                     ele1=i;
                     ele2=n/i;
                 }
          }
        //  cout<<ele1<<" "<<ele2<<endl;
  for(int i=0;i<ele1;i++)
  cout<<"1";
  for(int i=0;i<ansk;i++)
  cout<<"3";
  for(int i=0;i<ele2;i++)
  cout<<"7";
  for(int i=0;i<n1;i++)
  {
   cout<<"337";
  }
  cout<<"\n";
       }
       return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{fast;
       long long m;
       cin>>m;
       vector<long long>v;
       while(m--)
       {//vector<long long>v;
   long long n;
   cin>>n;
   ll k=2;
   ll prev=2;
   ll val=2;
   ll ans=1,ansk=0;
   if(n==1)
   {
          cout<<"1337"<<endl;continue;
   }
   while(n)
   {k=2;prev=2;val=2;
          while(1)
          {
          val=k*(k-1)/2;
          if(val>n)
          break;
          ans=val;ansk=k;
          //cout<<val<<endl;
          prev=val;
          k++;
          }
         // cout<<prev<<endl;
          v.push_back(ansk);
          n=n-prev;
          if(n==1)
          {v.push_back(2);n=n-1;
          break;}
   }
   //cout<<n<<endl;
   ll last=n;
   vector<long long>v1;
   sort(v.begin(),v.end());
   /*for(int i=0;i<v.size();i++)
   {
          cout<<v[i]<<" ";
   }
   cout<<endl;*/
   v1.push_back(v[0]);
   for(int i=1;i<v.size();i++)
   v1.push_back(v[i]-v[i-1]);
   /*for(int i=0;i<v1.size();i++)

   {
          cout<<v1[i]<<" ";
   }
   cout<<endl;*/
   cout<<"1";
      for(int i=0;i<v1.size();i++)
       {
         while(v1[i]--)
         cout<<"3";
         cout<<"7";
       }
       cout<<"\n";
       v.clear();v1.clear();
       }
       return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{fast;
       long long m;
       cin>>m;
       vector<long long>v;
       while(m--)
       {
   long long n;
   cin>>n;
   ll k=2;
   ll prev=2;
   ll val=2;
   ll ans=1,ansk;
   if(n==1)
   {
          cout<<"1337"<<endl;continue;
   }
   while(n)
   {k=2;prev=2;val=2;
          while(1)
          {
          val=k*(k-1)/2;
          if(val>n)
          break;
          ans=val;ansk=k;
          //cout<<val<<endl;
          prev=val;
          k++;
          }
          //cout<<prev<<endl;
          v.push_back(ansk);
          n=n-prev;
          if(n==1)
          {v.push_back(2);
          break;}
   }
   /*for(int i=0;i<v.size();i++)
   {
          cout<<v[i]<<" ";
   }
   cout<<endl;*/
   vector<long long>v1;
   sort(v.begin(),v.end());
   v1.push_back(v[0]);
   for(int i=1;i<v.size();i++)
   v1.push_back(v[i]-v[i-1]);
   cout<<"1";
      for(int i=0;i<v1.size();i++)
       {
         while(v1[i]--)
         cout<<"3";
         cout<<"7";
       }
       cout<<"\n";
       }
       return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{fast;
       long long m;
       cin>>m;
       while(m--)
       {
   long long n;
   cin>>n;
   ll k=2;
   ll prev=2;
   ll val=2;
   ll ans=1,ansk;
   while(1)
   {
          val=k*(k-1)/2;
          if(val>n)
          break;
                 ans=val;ansk=k;
        //  cout<<val<<endl;
          k++;
   }
   //cout<<ansk<<" "<<ans<<endl;
   long long ele1=1;long long ele2=1;
   ll n1=n%ans;
   //cout<<n1<<endl;
        //  cout<<ele1<<" "<<ele2<<endl;
 cout<<"1";
  for(int i=0;i<ansk;i++)
  cout<<"3";
  cout<<"7";
  for(int i=0;i<n1;i++)
  {
   cout<<"337";
  }
  cout<<"\n";
       }
       return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
       long long m;
       cin>>m;
       while(m--)
       {
   long long n;
   cin>>n;
   ll k=2;
   ll prev=2;
   ll val=2;
   ll ans=1,ansk;
   while(1)
   {
          val=k*(k-1)/2;
          if(val>n)
          break;
          if(n%val==0)
          {
                 ans=val;ansk=k;
          }
     ///     cout<<val<<endl;
          k++;
   }
   //cout<<ansk<<" "<<ans<<endl;
   long long ele1=1;long long ele2=1;
     n=n/ans;
          for(int i=1;i<=sqrt(n);i++)
          {
                 if(n%i==0)
                 {
                     ele1=i;
                     ele2=n/i;
                 }
          }
        //  cout<<ele1<<" "<<ele2<<endl;
  for(int i=0;i<ele1;i++)
  cout<<"1";
  for(int i=0;i<ansk;i++)
  cout<<"3";
  for(int i=0;i<ele2;i++)
  cout<<"7";
  cout<<endl;
       }
       return 0;
}


/*
    AUTHOR:shivam51
    IIESTS
*/
  #include<bits/stdc++.h>
  using namespace std;
  //
  #define add	 	accumulate
  #define ll 	 	long long int
  #define ff	 	first
  #define ss 	 	second
  #define pb 	 	push_back
  #define pob		pop_back
  #define pf 	 	push_front
  #define pof		pop_front
  #define mp 	 	make_pair
  #define vll 	 	vector<ll>
  #define lb 	 	lower_bound
  #define ub     	upper_bound
  #define pll 	 	pair<ll,ll>
  #define vpll   	vector<pll>
  #define all(v) 	v.begin(), v.end()
  #define rep(i,k,n)    for(int i=k;i<n;i++)
  #define hs            ios_base::sync_with_stdio(false);cin.tie(NULL);
  template <class C> void setmin(C &a,C b){if(a>b)a=b;}
  template <class C> void setmax(C &a,C b){if(a<b)a=b;}
  const ll mod=1e9+7;
  const ll MAX=1e5;
  //
  //
  	void solve(){
  		ll n;
  		cin>>n;
  		for(ll i=MAX;i>0;i--){
  			if((ll)(i*(i-1))>2*n)
  				continue;
  			ll rem=n-i*(i-1)/2;
  			if(rem&1||(rem+i+1)>MAX)
  				continue;
  			cout<<"133";
  			// cout<<i<<" "<<rem<<n;
  			rep(j,0,rem-1)
  				cout<<"7";
  			rep(j,0,i-2)
  				cout<<"3";
  			cout<<"7\n";
  			return ;
  		}
  	}

	int main(){
		hs;
		ll t;
		cin>>t;
		// t=1;
		while(t--)
		  solve();
    return 0;
}


/*
    AUTHOR:shivam51
    IIESTS
*/
  #include<bits/stdc++.h>
  using namespace std;
  //
  #define add	 	accumulate
  #define ll 	 	long long int
  #define ff	 	first
  #define ss 	 	second
  #define pb 	 	push_back
  #define pob		pop_back
  #define pf 	 	push_front
  #define pof		pop_front
  #define mp 	 	make_pair
  #define vll 	 	vector<ll>
  #define lb 	 	lower_bound
  #define ub     	upper_bound
  #define pll 	 	pair<ll,ll>
  #define vpll   	vector<pll>
  #define all(v) 	v.begin(), v.end()
  #define rep(i,k,n)    for(int i=k;i<n;i++)
  #define hs            ios_base::sync_with_stdio(false);cin.tie(NULL);
  template <class C> void setmin(C &a,C b){if(a>b)a=b;}
  template <class C> void setmax(C &a,C b){if(a<b)a=b;}
  const ll mod=1e9+7;
  const ll MAX=1e6;
  //
  //
  	void solve(){
  		ll n,k=1;
  		cin>>n;
  		for(int i=2;i*i<=n;i++){
  			if(n%i==0){
  				k=i;
  				// n/=i;
  			}
  		}
  		rep(j,0,k)
		cout<<"1";
		cout<<"33";
		// n/=i;
		ll p=floor((n/k)/10);
  		rep(i,0,p*10)
  			cout<<"7777777777";
  		// cout<<((n/k)%10);
  		rep(i,0,((n/k)%10))
  			cout<<'7';
  		cout<<'\n';
  	}
	int main(){
		hs;
		ll t;
		cin>>t;
		// t=1;
		while(t--)
		  solve();
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef tree<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

ordered_set xx;

ll n, m, x, y, z;
vector < pair < ll , ll > > vec;

int main()
{
    cin >> n;
    for(ll i = 1; i <= n; i++){

        scanf("%lld %lld", &x, &y);
        vec.push_back({y, -x});

    }

    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());

    ll ans = 0;
    for(ll i = 0; i < vec.size(); i++) vec[i].second = -vec[i].second;

    for(ll i = 0; i < vec.size(); ){

        ll j = i;
        while(j < vec.size() && vec[j].first == vec[i].first) j++;

        xx.insert(vec[i].second);
        ll lft = xx.order_of_key(vec[i].second + 1);
        ll rgh = xx.size() - xx.order_of_key(vec[i].second);

        ans += (lft) * (rgh);

        for(ll k = i + 1; k < j; k++){

            if(xx.find(vec[k].second) == xx.end()){

                ll lft = xx.order_of_key(vec[k - 1].second + 1);
                ans += lft;
            }

            xx.insert(vec[k].second);
            ll lft = xx.order_of_key(vec[k].second + 1) - xx.order_of_key(vec[k - 1].second + 1);;
            ll rgh = xx.size() - xx.order_of_key(vec[k].second);

            ans += lft * rgh;

        i = j;

    }

    cout << ans << endl;

    return 0;
}

/*

5
3 6
5 10
4 3
2 1
1 3

*/


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef tree<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

ordered_set xx;

ll n, m, x, y, z;
vector < pair < ll , ll > > vec;

int main()
{
    cin >> n;
    for(ll i = 1; i <= n; i++){

        scanf("%lld %lld", &x, &y);
        vec.push_back({y, -x});

    }

    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());

    ll ans = 0;
    for(ll i = 0; i < vec.size(); i++) vec[i].second = -vec[i].second;

    for(ll i = 0; i < vec.size(); ){

        ll j = i;
        while(j < vec.size() && vec[j].first == vec[i].first) j++;

        xx.insert(vec[i].second);
        ll lft = xx.order_of_key(vec[i].second + 1);
        ll rgh = xx.size() - xx.order_of_key(vec[i].second);

//        cout << i << ' ' << lft << ' ' << rgh << endl;

        ans += (lft) * (rgh);

//        cout << i << ' ' << j << endl;

        for(ll k = i + 1; k < j; k++){

            if(*(--xx.end()) >= vec[k].second){
                xx.insert(vec[k].second);

                ll lft = xx.order_of_key(vec[k].second + 1) - xx.order_of_key(vec[k - 1].second + 1);;
                ll rgh = xx.size() - xx.order_of_key(vec[k].second);

                ans += lft * rgh;

            }
            else{
                ans += xx.size() + 1;
                xx.insert(vec[k].second);
            }

        }

        i = j;

    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef tree<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

ordered_set xx;

ll n, m, x, y, z;
vector < pair < ll , ll > > vec;

int main()
{
    cin >> n;
    for(ll i = 1; i <= n; i++){

        scanf("%lld %lld", &x, &y);
        vec.push_back({y, -x});

    }

    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());

    ll ans = 0;
    for(ll i = 0; i < vec.size(); i++) vec[i].second = -vec[i].second;

    for(ll i = 0; i < vec.size(); ){

        ll j = i;
        while(j < vec.size() && vec[j].first == vec[i].first) j++;

        xx.insert(vec[i].second);
        ll lft = xx.order_of_key(vec[i].second + 1);
        ll rgh = xx.size() - xx.order_of_key(vec[i].second);

//        cout << i << ' ' << lft << ' ' << rgh << endl;

        ans += (lft) * (rgh);

//        cout << i << ' ' << j << endl;

        for(ll k = i + 1; k < j; k++){

            if(*(--xx.end()) >= vec[k].second){
                ans++;
                xx.insert(vec[k].second);
            }
            else{
                ans += xx.size() + 1;
                xx.insert(vec[k].second);
            }

        }

        i = j;

    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef tree<
ll,
null_type,
less<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

ordered_set xx;

ll n, m, x, y, z;
vector < pair < ll , ll > > vec;

int main()
{
    cin >> n;
    for(ll i = 1; i <= n; i++){

        scanf("%lld %lld", &x, &y);
        vec.push_back({y, -x});

    }

    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());

    ll ans = 0;
    for(ll i = 0; i < vec.size(); i++) vec[i].second = -vec[i].second;

    for(ll i = 0; i < vec.size(); ){

        ll j = i;
        while(j < vec.size() && vec[j].first == vec[i].first) j++;

        xx.insert(vec[i].second);
        ll lft = xx.order_of_key(vec[i].second + 1);
        ll rgh = xx.size() - xx.order_of_key(vec[i].second);

        ans += (lft) * (rgh);

        for(ll k = i + 1; k < j; k++){

            if(xx.find(vec[k].second) == xx.end()){

                ll lft = xx.order_of_key(vec[k - 1].second + 1);
                ans += lft;
            }

            xx.insert(vec[k].second);
            ll lft = xx.order_of_key(vec[k].second + 1) - xx.order_of_key(vec[k - 1].second + 1);;
            ll rgh = xx.size() - xx.order_of_key(vec[k].second);

            ans += lft * rgh;

        }
        i = j;

    }

    cout << ans << endl;

    return 0;
}

/*

5
3 6
5 10
4 3
2 1
1 3

*/


#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define ii pair<ll,ll>
#define mp make_pair
using namespace std;
int n;
ii a[300005];
set < int > s;
vector < int > v[300005];

/// Segment Tree
const int max_node = 20000000;
int tree[max_node];
int L[max_node];
int R[max_node];
int id = 2;
void update(int node , int start , int _end , int idx , int val)
{
    if(start == _end)
    {
        tree[node] = val;
    }
    else
    {
        int mid = (start+_end)/2;
        if(start <= idx && idx <= mid)
        {
            if(L[node] == 0)
            {
                L[node] = id++;
            }
            update(L[node],start,mid,idx,val);
        }
        else
        {
            if(R[node] == 0)
            {
                R[node] = id++;
            }
            update(R[node],mid+1,_end,idx,val);
        }
        tree[node] = tree[L[node]]+tree[R[node]];
    }
 }

int query(int node , int start , int _end, int l , int r)
{
    if(tree[node] == 0)
    {
        return 0;
    }
    if(r < start || l > _end)
    {
        return 0;
    }
    if(l <= start && _end<= r)
    {
        return tree[node];
    }
    if(L[node] == 0)
    {
        L[node] = id++;
    }
    if(R[node] == 0)
    {
        R[node] = id++;
    }
    int mid = (start+_end)/2;
    int x1 = query(L[node]  ,start , mid , l , r);
    int x2 = query(R[node],mid+1 ,_end , l , r);
    return (x1+x2);
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&a[i].fr,&a[i].sc);
        s.insert(a[i].sc);
    }
    int cnt = 1;
    map < int , int > my;
    for(auto x : s)
    {
        my[x] = cnt;
        cnt++;
    }
    for(int i=0;i<n;i++)
    {
        a[i].sc  = my[a[i].sc];
        v[a[i].sc].push_back(a[i].fr);
    }
    set < int > temp;
    ll res = 0;
    ll last = 0;
    ll En = 1e9;
    for(int i=cnt;i>=0;i--)
    {
        set < int > E;
        for(int j=0;j<v[i].size();j++)
        {
            E.insert(v[i][j]);

        }
        if(E.size()!= 0)
        {
            ll sum = E.size();
            sum = sum * (sum+1)/2;
            res+= sum;
            set < int > :: iterator itr = E.begin();
            int f1 = *itr;
            itr = E.end();
            itr--;
            int f2 = *itr;
            ll L = query(1,1,En,1,f1-1);
            ll R = query(1,1,En,f2+1,En);
            res += (L * R);
            for(auto x : E)
            {
                temp.insert(x);
                update(1,1,En,x,1);
            }
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define ii pair<ll,ll>
#define mp make_pair
using namespace std;
int n;
ii a[300005];
set < int > s;
vector < int > v[300005];
/// Segment Tree
const int max_node = 20000000;
int tree[max_node];
int L[max_node];
int R[max_node];
int id = 2;
void update(int node , int start , int _end , int idx , int val)
{
    if(start == _end)
    {
        tree[node] = val;
    }
    else
    {
        int mid = (start+_end)/2;
        if(start <= idx && idx <= mid)
        {
            if(L[node] == 0)
            {
                L[node] = id++;
            }
            update(L[node],start,mid,idx,val);
        }
        else
        {
            if(R[node] == 0)
            {
                R[node] = id++;
            }
            update(R[node],mid+1,_end,idx,val);
        }
        tree[node] = tree[L[node]]+tree[R[node]];
    }
 }

int query(int node , int start , int _end, int l , int r)
{
    if(tree[node] == 0)
    {
        return 0;
    }
    if(r < start || l > _end)
    {
        return 0;
    }
    if(l <= start && _end<= r)
    {
        return tree[node];
    }
    if(L[node] == 0)
    {
        L[node] = id++;
    }
    if(R[node] == 0)
    {
        R[node] = id++;
    }
    int mid = (start+_end)/2;
    int x1 = query(L[node]  ,start , mid , l , r);
    int x2 = query(R[node],mid+1 ,_end , l , r);
    return (x1+x2);
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&a[i].fr,&a[i].sc);
        s.insert(a[i].sc);
    }
    int cnt = 1;
    map < int , int > my;
    for(auto x : s)
    {
        my[x] = cnt;
        cnt++;
    }
    for(int i=0;i<n;i++)
    {
        a[i].sc  = my[a[i].sc];
        v[a[i].sc].push_back(a[i].fr);
    }
    ll res = 0;
    ll last = 0;
    ll En = 1e9;
    for(int i=cnt;i>=0;i--)
    {
        set < int > E;
        for(int j=0;j<v[i].size();j++)
        {
            E.insert(v[i][j]);
        }
        if(E.size()!= 0)
        {
            ll sum = E.size();
//            sum = (sum * (sum+1))/2;
//            res+= sum;
            set < int > :: iterator itr = E.begin();
            int f1 = *itr;
            itr = E.end();
            itr--;
            int f2 = *itr;
            ll L = query(1,1,En,1,f1-1);
            ll R = query(1,1,En,f2+1,En);
            for(auto x : E)
            {
                update(1,1,En,x,0);
            }
            ll in = query(1,1,En,f1,f2);
            ll sz = E.size();
            ll sz2 = sz + in;
            res += L * sz2;
            res += R * sz2;
            res += (L * R);

            res += sz2 * (sz2+1)/2;
            res -= in * (in+1)/2;

            for(auto x : E)
            {
                update(1,1,En,x,1);
            }
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define ii pair<ll,ll>
#define mp make_pair
using namespace std;
int n;
ii a[300005];
set < int > s;
vector < int > v[300005];

/// Segment Tree
const int max_node = 20000000;
int tree[max_node];
int L[max_node];
int R[max_node];
int id = 2;
void update(int node , int start , int _end , int idx , int val)
{
    if(start == _end)
    {
        tree[node] = val;
    }
    else
    {
        int mid = (start+_end)/2;
        if(start <= idx && idx <= mid)
        {
            if(L[node] == 0)
            {
                L[node] = id++;
            }
            update(L[node],start,mid,idx,val);
        }
        else
        {
            if(R[node] == 0)
            {
                R[node] = id++;
            }
            update(R[node],mid+1,_end,idx,val);
        }
        tree[node] = tree[L[node]]+tree[R[node]];
    }
 }

int query(int node , int start , int _end, int l , int r)
{
    if(tree[node] == 0)
    {
        return 0;
    }
    if(r < start || l > _end)
    {
        return 0;
    }
    if(l <= start && _end<= r)
    {
        return tree[node];
    }
    if(L[node] == 0)
    {
        L[node] = id++;
    }
    if(R[node] == 0)
    {
        R[node] = id++;
    }
    int mid = (start+_end)/2;
    int x1 = query(L[node]  ,start , mid , l , r);
    int x2 = query(R[node],mid+1 ,_end , l , r);
    return (x1+x2);
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&a[i].fr,&a[i].sc);
        s.insert(a[i].sc);
    }
    int cnt = 1;
    map < int , int > my;
    for(auto x : s)
    {
        my[x] = cnt;
        cnt++;
    }
    for(int i=0;i<n;i++)
    {
        a[i].sc  = my[a[i].sc];
        v[a[i].sc].push_back(a[i].fr);
    }
    ll res = 0;
    ll last = 0;
    ll En = 1e9;
    for(int i=cnt;i>=0;i--)
    {
        set < int > E;
        for(int j=0;j<v[i].size();j++)
        {
            E.insert(v[i][j]);
        }
        if(E.size()!= 0)
        {
            ll sum = E.size();
            sum = sum * (sum+1)/2;
            res+= sum;
            set < int > :: iterator itr = E.begin();
            int f1 = *itr;
            itr = E.end();
            itr--;
            int f2 = *itr;
            ll L = query(1,1,En,1,f1-1);
            ll R = query(1,1,En,f2+1,En);
            ll sz = E.size();
            res += L * sz;
            res += R * sz;
            res += (L * R);

            for(auto x : E)
            {
                update(1,1,En,x,1);
            }
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define ii pair<ll,ll>
#define mp make_pair
using namespace std;
int n;
ii a[300005];
set < int > s;
vector < int > v[300005];
/// Segment Tree
const int max_node = 20000000;
int tree[max_node];
int L[max_node];
int R[max_node];
int id = 2;
void update(int node , int start , int _end , int idx , int val)
{
    if(start == _end)
    {
        tree[node] = val;
    }
    else
    {
        int mid = (start+_end)/2;
        if(start <= idx && idx <= mid)
        {
            if(L[node] == 0)
            {
                L[node] = id++;
            }
            update(L[node],start,mid,idx,val);
        }
        else
        {
            if(R[node] == 0)
            {
                R[node] = id++;
            }
            update(R[node],mid+1,_end,idx,val);
        }
        tree[node] = tree[L[node]]+tree[R[node]];
    }
 }

int query(int node , int start , int _end, int l , int r)
{
    if(tree[node] == 0)
    {
        return 0;
    }
    if(r < start || l > _end)
    {
        return 0;
    }
    if(l <= start && _end<= r)
    {
        return tree[node];
    }
    if(L[node] == 0)
    {
        L[node] = id++;
    }
    if(R[node] == 0)
    {
        R[node] = id++;
    }
    int mid = (start+_end)/2;
    int x1 = query(L[node]  ,start , mid , l , r);
    int x2 = query(R[node],mid+1 ,_end , l , r);
    return (x1+x2);
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&a[i].fr,&a[i].sc);
        s.insert(a[i].sc);
    }
    int cnt = 1;
    map < int , int > my;
    for(auto x : s)
    {
        my[x] = cnt;
        cnt++;
    }
    for(int i=0;i<n;i++)
    {
        a[i].sc  = my[a[i].sc];
        v[a[i].sc].push_back(a[i].fr);
    }
    ll res = 0;
    ll last = 0;
    ll En = 1e9;
    for(int i=cnt;i>=0;i--)
    {
        set < int > E;
        for(int j=0;j<v[i].size();j++)
        {
            E.insert(v[i][j]);
        }
        if(E.size()!= 0)
        {
            ll sum = E.size();
            set < int > :: iterator itr = E.begin();
            int f1 = *itr;
            itr = E.end();
            itr--;
            int f2 = *itr;
            ll L = query(1,1,En,1,f1-1);
            ll R = query(1,1,En,f2+1,En);
            for(auto x : E)
            {
                update(1,1,En,x,0);
            }
            ll in = query(1,1,En,f1,f2);
            ll sz = E.size();
            ll sz2 = sz + in;
            res += L * sz2;
            res += R * sz2;
            res += (L * R);

            res += (sz2 * (sz2+1))/2;
            int temp1 = f1,temp2 = f1;
            for(auto x : E)
            {
                if(x == f1)
                    continue;
                temp1 = temp2;
                temp2 = x;
                int q2 = query(1,1,En,temp1+1,temp2-1);
                res -= q2 * (q2+1)/2;
            }

            for(auto x : E)
            {
                update(1,1,En,x,1);
            }
        }
    }
    cout<<res<<endl;
}


#include <bits/stdc++.h>
#define ll long long
#define fr first
#define sc second
#define ii pair<ll,ll>
#define mp make_pair
using namespace std;
int n;
ii a[300005];
set < int > s;
vector < int > v[300005];
/// Segment Tree
const int max_node = 20000000;
int tree[max_node];
int L[max_node];
int R[max_node];
int id = 2;
void update(int node , int start , int _end , int idx , int val)
{
    if(start == _end)
    {
        tree[node] = val;
    }
    else
    {
        int mid = (start+_end)/2;
        if(start <= idx && idx <= mid)
        {
            if(L[node] == 0)
            {
                L[node] = id++;
            }
            update(L[node],start,mid,idx,val);
        }
        else
        {
            if(R[node] == 0)
            {
                R[node] = id++;
            }
            update(R[node],mid+1,_end,idx,val);
        }
        tree[node] = tree[L[node]]+tree[R[node]];
    }
 }

int query(int node , int start , int _end, int l , int r)
{
    if(tree[node] == 0)
    {
        return 0;
    }
    if(r < start || l > _end)
    {
        return 0;
    }
    if(l <= start && _end<= r)
    {
        return tree[node];
    }
    if(L[node] == 0)
    {
        L[node] = id++;
    }
    if(R[node] == 0)
    {
        R[node] = id++;
    }
    int mid = (start+_end)/2;
    int x1 = query(L[node]  ,start , mid , l , r);
    int x2 = query(R[node],mid+1 ,_end , l , r);
    return (x1+x2);
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&a[i].fr,&a[i].sc);
        s.insert(a[i].sc);
    }
    int cnt = 1;
    map < int , int > my;
    for(auto x : s)
    {
        my[x] = cnt;
        cnt++;
    }
    for(int i=0;i<n;i++)
    {
        a[i].sc  = my[a[i].sc];
        v[a[i].sc].push_back(a[i].fr);
    }
    ll res = 0;
    ll last = 0;
    ll En = 1e9;
    for(int i=cnt;i>=0;i--)
    {
        set < int > E;
        for(int j=0;j<v[i].size();j++)
        {
            E.insert(v[i][j]);
        }
        if(E.size()!= 0)
        {
            ll sum = E.size();
            set < int > :: iterator itr = E.begin();
            int f1 = *itr;
            itr = E.end();
            itr--;
            int f2 = *itr;
            ll L = query(1,1,En,1,f1-1);
            ll R = query(1,1,En,f2+1,En);
            for(auto x : E)
            {
                update(1,1,En,x,0);
            }
            ll in = query(1,1,En,f1,f2);
            ll sz = E.size();
            ll sz2 = sz + in;
            res += L * sz2;
            res += R * sz2;
            res += (L * R);

            res += (sz2 * (sz2+1))/2;
            int temp1 = f1,temp2 = f1;

            for(auto x : E)
            {
                if(x == f1)
                    continue;
                temp1 = temp2;
                temp2 = x;
                ll q2 = query(1,1,En,temp1+1,temp2-1);
                res -= (q2 * (q2+1))/2;
            }

            for(auto x : E)
            {
                update(1,1,En,x,1);
            }
        }
    }
    cout<<res<<endl;
}


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

#define ll long long
#define pb push_back

const int maxn = 2e5 + 20;

int x[maxn] , y[maxn];

vector<int> p[maxn] , pos[maxn];

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

	int n;
	cin >> n;

	vector<int> cmpx , cmpy;
	for(int i = 0; i < n; i++)
	{
		cin >> x[i] >> y[i];
		cmpx.pb(x[i]);
		cmpy.pb(y[i]);
	}

	sort(cmpx.begin() , cmpx.end());
	sort(cmpy.begin() , cmpy.end());

	cmpx.resize(unique(cmpx.begin() , cmpx.end()) - cmpx.begin());
	cmpy.resize(unique(cmpy.begin() , cmpy.end()) - cmpy.begin());

	for(int i = 0; i < n; i++)
	{
		x[i] = lower_bound(cmpx.begin() , cmpx.end() , x[i]) - cmpx.begin();
		y[i] = lower_bound(cmpy.begin() , cmpy.end() , y[i]) - cmpy.begin();
		p[y[i]].pb(x[i]);
	}

	ll res = 0;
	for(int i = 0; i < (int)cmpy.size(); i++)
	{
		sort(p[i].begin() , p[i].end());
		p[i].resize(unique(p[i].begin() , p[i].end()) - p[i].begin());
		int sz = p[i].size();
		res += 1LL * sz * (sz + 1) / 2;
	}

	cout << res << endl;
}


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

#define ll long long
#define pb push_back
#define lb(a) ((a)&(-(a)))

const int maxn = 2e5 + 20;

int x[maxn] , y[maxn];

vector<int> p[maxn] , shit[maxn];

int fen[maxn] , mx[maxn];

void add(int p)
{
	for(p += 5; p < maxn; p += lb(p))
		fen[p]++;
}

int get(int p)
{
	int res = 0;
	for(p += 5; p > 0; p -= lb(p))
		res += fen[p];

	return res;
}

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

	int n;
	cin >> n;

	vector<int> cmpx , cmpy;
	for(int i = 0; i < n; i++)
	{
		cin >> x[i] >> y[i];
		cmpx.pb(x[i]);
		cmpy.pb(y[i]);
	}

	sort(cmpx.begin() , cmpx.end());
	sort(cmpy.begin() , cmpy.end());

	cmpx.resize(unique(cmpx.begin() , cmpx.end()) - cmpx.begin());
	cmpy.resize(unique(cmpy.begin() , cmpy.end()) - cmpy.begin());

	for(int i = 0; i < n; i++)
	{
		x[i] = lower_bound(cmpx.begin() , cmpx.end() , x[i]) - cmpx.begin();
		y[i] = lower_bound(cmpy.begin() , cmpy.end() , y[i]) - cmpy.begin();
		mx[x[i]] = max(mx[x[i]] , y[i]);
		p[y[i]].pb(x[i]);
	}

	for(int i = 0; i < (int)cmpx.size(); i++)
		shit[mx[i]].pb(i);

	ll res = 0;
	for(int i = (int)cmpy.size() - 1; i >= 0; i--)
	{
		for(auto x : shit[i])
			add(x);

		sort(p[i].begin() , p[i].end());
		p[i].pb(n);
		for(int j = 0; j + 1 < (int)p[i].size(); j++)
			res += 1LL * get(p[i][j]) * (get(p[i][j + 1] - 1) - get(p[i][j] - 1));
	}

	cout << res << endl;
}


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

#define ll long long
#define pb push_back

const int maxn = 2e5 + 20;

int x[maxn] , y[maxn];

vector<int> p[maxn] , pos[maxn];

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

	int n;
	cin >> n;

	vector<int> cmpx , cmpy;
	for(int i = 0; i < n; i++)
	{
		cin >> x[i] >> y[i];
		cmpx.pb(x[i]);
		cmpy.pb(y[i]);
	}

	sort(cmpx.begin() , cmpx.end());
	sort(cmpy.begin() , cmpy.end());

	cmpx.resize(unique(cmpx.begin() , cmpx.end()) - cmpx.begin());
	cmpy.resize(unique(cmpy.begin() , cmpy.end()) - cmpy.begin());

	for(int i = 0; i < n; i++)
	{
		x[i] = lower_bound(cmpx.begin() , cmpx.end() , x[i]) - cmpx.begin();
		y[i] = lower_bound(cmpy.begin() , cmpy.end() , y[i]) - cmpy.begin();
		p[x[i]].pb(i);
	}

	for(int i = 0; i < maxn; i++)
		pos[i].pb(-1);

	ll X = 0 , res = 0;
	for(int i = 0; i < (int)cmpx.size(); i++)
	{
		for(auto ind : p[i])
		{
			X += i - pos[y[ind]].back();
			pos[y[ind]].pb(i);
		}

		res += X;
	}

	cout << res << endl;
}


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

#define ll long long
#define pb push_back

const int maxn = 2e5 + 20;

int x[maxn] , y[maxn];

vector<int> p[maxn] , pos[maxn];

bool is[maxn];

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

	int n;
	cin >> n;

	vector<int> cmpx , cmpy;
	for(int i = 0; i < n; i++)
	{
		cin >> x[i] >> y[i];
		cmpx.pb(x[i]);
		cmpy.pb(y[i]);
	}

	sort(cmpx.begin() , cmpx.end());
	sort(cmpy.begin() , cmpy.end());

	cmpx.resize(unique(cmpx.begin() , cmpx.end()) - cmpx.begin());
	cmpy.resize(unique(cmpy.begin() , cmpy.end()) - cmpy.begin());

	for(int i = 0; i < n; i++)
	{
		x[i] = lower_bound(cmpx.begin() , cmpx.end() , x[i]) - cmpx.begin();
		y[i] = lower_bound(cmpy.begin() , cmpy.end() , y[i]) - cmpy.begin();
		p[y[i]].pb(x[i]);
	}

	ll res = 0;

	int On = 0;
	for(int i = (int)cmpy.size() - 1; i >= 0; i--)
	{
		sort(p[i].begin() , p[i].end());

		int nw = 0;
		for(auto x : p[i])
			if(is[x])
				nw++;

		int sz = p[i].size();
		res += 1LL * sz * (On - nw) + (1LL * sz * (sz + 1) / 2);

		for(auto x : p[i])
			if(!is[x])
				is[x] = 1 , On++;
	}

	cout << res << endl;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
namespace mine
{
	typedef long long ll;
	#define pr pair<int,int>
	#define FR first
	#define SE second
	#define MP make_pair
	#define PB pushback
	#define vc vector
	void chmax(int &x,const ll y) {x=(x>y?x:y);}
	void chmin(int &x,const ll y) {x=(x<y?x:y);}
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num)
	{
		if(num<0) putchar('-'),num=-num;
		if(num>=10) write(num/10);
		putchar('0'+num%10);
	}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	const int MOD=1e9+7;
	void add(ll &a,ll b){a+=b;if(a>=MOD)a-=MOD;if(a<=-MOD)a+=MOD;}
	ll qpower(ll x,int e)
	{
		ll ans=1;
		while(e)
		{
			if(e&1) ans=ans*x%MOD;
			x=x*x%MOD;e>>=1;
		}
		return ans;
	}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MAX_N=2e5+10;

	pr p[MAX_N];set<int> hash;
	bool cmp(pr a,pr b) {return a.SE>b.SE;}
	void main()
	{
		int n=qread();
		for(int i=1;i<=n;i++) {int x=qread(),y=qread();p[i]=MP(x,y);}
		sort(p+1,p+n+1,cmp);
		ll ans=0;int cnt=0;
		for(int i=1;i<=n;)
		{
			int j=i;while(j+1<=n and p[i].SE==p[j+1].SE) j++;
			for(int t=i;t<=j;t++) if(!hash.count(p[t].FR)) cnt++,hash.insert(p[t].FR);
			i=j+1;
			ans+=ll(cnt+1)*cnt/2;
		}
		printf("%lld",ans);
	}
};
int main()
{
	srand(time(0));
	mine::main();
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
namespace mine
{
	typedef long long ll;
	#define pr pair<int,int>
	#define FR first
	#define SE second
	#define MP make_pair
	#define PB pushback
	#define vc vector
	void chmax(int &x,const ll y) {x=(x>y?x:y);}
	void chmin(int &x,const ll y) {x=(x<y?x:y);}
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num)
	{
		if(num<0) putchar('-'),num=-num;
		if(num>=10) write(num/10);
		putchar('0'+num%10);
	}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	const int INF=0x3f3f3f3f;
	const int MOD=1e9+7;
	void add(ll &a,ll b){a+=b;if(a>=MOD)a-=MOD;if(a<=-MOD)a+=MOD;}
	ll qpower(ll x,int e)
	{
		ll ans=1;
		while(e)
		{
			if(e&1) ans=ans*x%MOD;
			x=x*x%MOD;e>>=1;
		}
		return ans;
	}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MAX_N=2e5+10;

	namespace Splay
	{
		struct Nod{int son[2],fa,siz,d;}p[MAX_N];
		int root=0,id=0;
		#define lc p[x].son[0]
		#define rc p[x].son[1]
		bool son(int x){return p[p[x].fa].son[1]==x;}
		void pushup(int x){p[x].siz=1+p[lc].siz+p[rc].siz;}
		void rotate(int x)
		{
			int f=p[x].fa,ff=p[f].fa,c=son(x);if(ff)p[ff].son[son(f)]=x;
			int tson=p[x].son[c^1];p[f].son[c]=tson;p[x].son[c^1]=f;
			p[x].fa=ff;p[f].fa=x;if(tson) p[tson].fa=f;pushup(f);pushup(x);
		}
		void splay(int x)
		{
			for(int f=p[x].fa;p[x].fa>0;rotate(x),x=p[f].fa) if(p[f].fa) son(x)^son(f)?rotate(x):rotate(f);
			root=x;
		}
		int findip(int d)
		{
			int x=root;
			while(p[x].d!=d)
			{
				if(d<p[x].d) {if(lc==0) return x;x=lc;}
				else {if(rc==0) return x;x=rc;}
			}
			return x;
		}
		void insert(int d)
		{
			if(root==0) {p[root=++id]=(Nod){0,0,0,1,d};return;}
			int x=findip(d);if(p[x].d==d) return;
			int y=++id;p[y]=(Nod){0,0,x,1,d};p[x].son[d>p[x].d]=y;splay(y);
		}
	};

	pr pt[MAX_N];set<int> hash;
	bool cmp(pr a,pr b) {return a.SE>b.SE;}
	ll pp(ll x) {return (x+1)*x/2;}
	void main()
	{
		int n=qread();
		for(int i=1;i<=n;i++) {int x=qread(),y=qread();pt[i]=MP(x,y);}
		sort(pt+1,pt+n+1,cmp);
		ll ans=0;
		for(int i=1;i<=n;)
		{
			int j=i;while(j+1<=n and pt[i].SE==pt[j+1].SE) j++;
			int mi=INF,mx=-INF;
			for(int t=i;t<=j;t++) hash.insert(pt[t].FR),chmin(mi,pt[t].FR),chmax(mx,pt[t].FR);
			i=j+1;
			int cnt=hash.size();
			ans+=pp(cnt);

			using namespace Splay;
			int x=findip(mi);splay(x);ans-=pp(p[p[x].son[0]].siz);
			int y=findip(mx);splay(y);ans-=pp(p[p[y].son[1]].siz);
		}
		printf("%lld",ans);
	}
};
int main()
{
	srand(time(0));
	mine::main();
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
namespace mine
{
	typedef long long ll;
	#define pr pair<int,int>
	#define FR first
	#define SE second
	#define MP make_pair
	#define PB pushback
	#define vc vector
	void chmax(int &x,const ll y) {x=(x>y?x:y);}
	void chmin(int &x,const ll y) {x=(x<y?x:y);}
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num)
	{
		if(num<0) putchar('-'),num=-num;
		if(num>=10) write(num/10);
		putchar('0'+num%10);
	}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	const int INF=0x3f3f3f3f;
	const int MOD=1e9+7;
	void add(ll &a,ll b){a+=b;if(a>=MOD)a-=MOD;if(a<=-MOD)a+=MOD;}
	ll qpower(ll x,int e)
	{
		ll ans=1;
		while(e)
		{
			if(e&1) ans=ans*x%MOD;
			x=x*x%MOD;e>>=1;
		}
		return ans;
	}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MAX_N=2e5+10;

	namespace Splay
	{
		struct Nod{int son[2],fa,siz,d;}p[MAX_N];
		int root=0,id=0;
		#define lc p[x].son[0]
		#define rc p[x].son[1]
		bool son(int x){return p[p[x].fa].son[1]==x;}
		void pushup(int x){p[x].siz=1+p[lc].siz+p[rc].siz;}
		void rotate(int x)
		{
			int f=p[x].fa,ff=p[f].fa,c=son(x);if(ff)p[ff].son[son(f)]=x;
			int tson=p[x].son[c^1];p[f].son[c]=tson;p[x].son[c^1]=f;
			p[x].fa=ff;p[f].fa=x;if(tson) p[tson].fa=f;
			pushup(f);pushup(x);
		}
		void splay(int x)
		{
			for(int f=p[x].fa;p[x].fa>0;rotate(x),f=p[x].fa)
				if(p[f].fa>0) son(x)^son(f)?rotate(x):rotate(f);
			root=x;
		}
		int findip(int d)
		{
			int x=root;
			while(p[x].d!=d)
			{
				if(d<p[x].d) {if(lc==0) return x;x=lc;}
				else {if(rc==0) return x;x=rc;}
			}
			return x;
		}
		void insert(int d)
		{
			if(root==0) {p[root=++id]=(Nod){0,0,0,1,d};return;}
			int x=findip(d);if(p[x].d==d) return;
			int y=++id;p[y]=(Nod){0,0,x,1,d};p[x].son[d>p[x].d]=y;splay(y);
		}
		int getrk(int d) {int x=findip(d);splay(x);return p[lc].siz+1;}
	};

	pr pt[MAX_N];set<int> hash;
	bool cmp(pr a,pr b) {return a.SE>b.SE or (a.SE==b.SE and a.FR<b.FR);}
	ll pp(ll x) {return (x+1)*x/2;}
	void main()
	{
		int n=qread();
		for(int i=1;i<=n;i++) {int x=qread(),y=qread();pt[i]=MP(x,y);}
		sort(pt+1,pt+n+1,cmp);
		ll ans=0;
		for(int i=1;i<=n;)
		{
			int j=i;while(j+1<=n and pt[i].SE==pt[j+1].SE) j++;

			using namespace Splay;
			for(int t=i;t<=j;t++)
			{
				insert(pt[t].FR);
				if(t>i) ans-=pp(getrk(pt[t].FR)-getrk(pt[t-1].FR)-1);
			}
			ans+=pp(p[root].siz);
			int x=findip(pt[i].FR);splay(x);ans-=pp(p[p[x].son[0]].siz);
			int y=findip(pt[j].FR);splay(y);ans-=pp(p[p[y].son[1]].siz);

			i=j+1;
		}
		printf("%lld",ans);
	}
};
int main()
{
	srand(time(0));
	mine::main();
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
namespace mine
{
	typedef long long ll;
	#define pr pair<int,int>
	#define FR first
	#define SE second
	#define MP make_pair
	#define PB pushback
	#define vc vector
	void chmax(int &x,const ll y) {x=(x>y?x:y);}
	void chmin(int &x,const ll y) {x=(x<y?x:y);}
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num)
	{
		if(num<0) putchar('-'),num=-num;
		if(num>=10) write(num/10);
		putchar('0'+num%10);
	}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	const int INF=0x3f3f3f3f;
	const int MOD=1e9+7;
	void add(ll &a,ll b){a+=b;if(a>=MOD)a-=MOD;if(a<=-MOD)a+=MOD;}
	ll qpower(ll x,int e)
	{
		ll ans=1;
		while(e)
		{
			if(e&1) ans=ans*x%MOD;
			x=x*x%MOD;e>>=1;
		}
		return ans;
	}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MAX_N=2e5+10;

	namespace Splay
	{
		struct Nod{int son[2],fa,siz,d;}p[MAX_N];
		int root=0,id=0;
		#define lc p[x].son[0]
		#define rc p[x].son[1]
		bool son(int x){return p[p[x].fa].son[1]==x;}
		void pushup(int x){p[x].siz=1+p[lc].siz+p[rc].siz;}
		void rotate(int x)
		{
			// if(x==4 and p[p[x].fa].d==4)
				// puts("");
			int f=p[x].fa,ff=p[f].fa,c=son(x);if(ff)p[ff].son[son(f)]=x;
			int tson=p[x].son[c^1];p[f].son[c]=tson;p[x].son[c^1]=f;
			p[x].fa=ff;p[f].fa=x;if(tson) p[tson].fa=f;
			pushup(f);pushup(x);
		}
		void splay(int x)
		{
			while(p[x].fa>0) rotate(x);
			// for(int f=p[x].fa;p[x].fa>0;rotate(x))
			// 	if(p[f].fa>0)
			// 	{
			// 		if(p[x].d==1 and p[f].d==4)
			// 			puts("");
			// 			son(x)^son(f)?rotate(x):rotate(f);
			// 	}
			root=x;
		}
		int findip(int d)
		{
			int x=root;
			while(p[x].d!=d)
			{
				if(d<p[x].d) {if(lc==0) return x;x=lc;}
				else {if(rc==0) return x;x=rc;}
			}
			return x;
		}
		void insert(int d)
		{
			if(root==0) {p[root=++id]=(Nod){0,0,0,1,d};return;}
			int x=findip(d);if(p[x].d==d) return;
			int y=++id;p[y]=(Nod){0,0,x,1,d};p[x].son[d>p[x].d]=y;splay(y);
		}
	};

	pr pt[MAX_N];set<int> hash;
	bool cmp(pr a,pr b) {return a.SE>b.SE;}
	ll pp(ll x) {return (x+1)*x/2;}
	void main()
	{
		int n=qread();
		for(int i=1;i<=n;i++) {int x=qread(),y=qread();pt[i]=MP(x,y);}
		sort(pt+1,pt+n+1,cmp);
		ll ans=0;
		for(int i=1;i<=n;)
		{
			int j=i;while(j+1<=n and pt[i].SE==pt[j+1].SE) j++;
			int mi=INF,mx=-INF;

			using namespace Splay;
			//printf("i=%d j=%d\n",i,j);
			for(int t=i;t<=j;t++) insert(pt[t].FR),chmin(mi,pt[t].FR),chmax(mx,pt[t].FR);
			ans+=pp(p[root].siz);//printf("A %lld\n",ans);
			int x=findip(mi);splay(x);ans-=pp(p[p[x].son[0]].siz);//printf("B %lld\n",ans);
			int y=findip(mx);splay(y);ans-=pp(p[p[y].son[1]].siz);//printf("C %lld\n",ans);

			i=j+1;
		}
		printf("%lld",ans);
	}
};
int main()
{
	srand(time(0));
	mine::main();
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
namespace mine
{
	typedef long long ll;
	#define pr pair<int,int>
	#define FR first
	#define SE second
	#define MP make_pair
	#define PB pushback
	#define vc vector
	void chmax(int &x,const ll y) {x=(x>y?x:y);}
	void chmin(int &x,const ll y) {x=(x<y?x:y);}
	ll qread()
	{
		ll ans=0,f=1;char c=getchar();
		while(c<'0' or c>'9') {if(c=='-')f=-1;c=getchar();}
		while('0'<=c and c<='9') ans=ans*10+c-'0',c=getchar();
		return ans*f;
	}
	void write(ll num)
	{
		if(num<0) putchar('-'),num=-num;
		if(num>=10) write(num/10);
		putchar('0'+num%10);
	}
	void write1(ll num){write(num);putchar(' ');}
	void write2(ll num){write(num);putchar('\n');}
	const int INF=0x3f3f3f3f;
	const int MOD=1e9+7;
	void add(ll &a,ll b){a+=b;if(a>=MOD)a-=MOD;if(a<=-MOD)a+=MOD;}
	ll qpower(ll x,int e)
	{
		ll ans=1;
		while(e)
		{
			if(e&1) ans=ans*x%MOD;
			x=x*x%MOD;e>>=1;
		}
		return ans;
	}
	ll invm(ll x){return qpower(x,MOD-2);}
	const int MAX_N=2e5+10;

	namespace Splay
	{
		struct Nod{int son[2],fa,siz,d;}p[MAX_N];
		int root=0,id=0;
		#define lc p[x].son[0]
		#define rc p[x].son[1]
		bool son(int x){return p[p[x].fa].son[1]==x;}
		void pushup(int x){p[x].siz=1+p[lc].siz+p[rc].siz;}
		void rotate(int x)
		{
			// if(x==4 and p[p[x].fa].d==4)
				// puts("");
			int f=p[x].fa,ff=p[f].fa,c=son(x);if(ff)p[ff].son[son(f)]=x;
			int tson=p[x].son[c^1];p[f].son[c]=tson;p[x].son[c^1]=f;
			p[x].fa=ff;p[f].fa=x;if(tson) p[tson].fa=f;
			pushup(f);pushup(x);
		}
		void splay(int x)
		{
			while(p[x].fa>0) rotate(x);
			// for(int f=p[x].fa;p[x].fa>0;rotate(x))
			// 	if(p[f].fa>0)
			// 	{
			// 		if(p[x].d==1 and p[f].d==4)
			// 			puts("");
			// 			son(x)^son(f)?rotate(x):rotate(f);
			// 	}
			root=x;
		}
		int findip(int d)
		{
			int x=root;
			while(p[x].d!=d)
			{
				if(d<p[x].d) {if(lc==0) return x;x=lc;}
				else {if(rc==0) return x;x=rc;}
			}
			return x;
		}
		void insert(int d)
		{
			if(root==0) {p[root=++id]=(Nod){0,0,0,1,d};return;}
			int x=findip(d);if(p[x].d==d) return;
			int y=++id;p[y]=(Nod){0,0,x,1,d};p[x].son[d>p[x].d]=y;splay(y);
		}
		int getrk(int d) {int x=findip(d);splay(x);return p[lc].siz+1;}
	};

	pr pt[MAX_N];set<int> hash;
	bool cmp(pr a,pr b) {return a.SE>b.SE or (a.SE==b.SE and a.FR<b.FR);}
	ll pp(ll x) {return (x+1)*x/2;}
	void main()
	{
		int n=qread();
		for(int i=1;i<=n;i++) {int x=qread(),y=qread();pt[i]=MP(x,y);}
		sort(pt+1,pt+n+1,cmp);
		ll ans=0;
		for(int i=1;i<=n;)
		{
			int j=i;while(j+1<=n and pt[i].SE==pt[j+1].SE) j++;

			using namespace Splay;
			for(int t=i;t<=j;t++)
			{
				insert(pt[t].FR);
				if(t>i) ans-=pp(getrk(pt[t].FR)-getrk(pt[t-1].FR)-1);
			}
			ans+=pp(p[root].siz);
			int x=findip(pt[i].FR);splay(x);ans-=pp(p[p[x].son[0]].siz);
			int y=findip(pt[j].FR);splay(y);ans-=pp(p[p[y].son[1]].siz);

			i=j+1;
		}
		printf("%lld",ans);
	}
};
int main()
{
	srand(time(0));
	mine::main();
}


#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
using namespace std;
map<int, vector<int> > df;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		int x, y;
		cin >> x >> y;
		df[y].pb(x);
	}
	set<int> dis;
	ll ans = 0;
	for(auto i = df.rbegin(); i != df.rend(); i++){
		vector<int> cur = i->second;
		for(int j = 0; j < cur.size(); j++)
			dis.insert(cur[j]);
		ll x = dis.size();
		ans += x * (x + 1) / 2;
	}
	cout << ans << '\n';
}


#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 200010
using namespace std;
int tree[4 * N];
map<int, int> bts;
void update(int low, int high, int ind, int value, int node){
	if(low == high){
		tree[node] = value;
		return;
	}
	int mid = (low + high) / 2;
	if(ind <= mid)
		update(low, mid, ind, value, 2 * node);
	else update(mid + 1, high, ind, value, 2 * node + 1);
	tree[node] = tree[2 * node] + tree[2 * node + 1];
}
int query(int low, int high, int begin, int end, int node){
	if(low > end || high < begin || begin > end)
		return 0;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high) / 2;
	return query(low, mid, begin, end, 2 * node) + query(mid + 1, high, begin, end, 2 * node + 1);
}
map<int, set<int> > df;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	vector<int> rt;
	for(int i = 0; i < n; i++){
		int x, y;
		cin >> x >> y;
		rt.pb(x);
		df[y].insert(x);
	}
	sort(rt.begin(), rt.end());
	for(int i = 0; i < rt.size(); i++){
		bts[rt[i]] = lower_bound(rt.begin(), rt.end(), rt[i]) - rt.begin();
	}
	ll ans = 0;
	set<int> occ;
	for(auto it = df.rbegin(); it != df.rend(); it++){
		//cerr << ans << '\n';
		set<int> rx = it->second;
		vector<int> cur;// = it->second;
		for(auto i = rx.begin(); i != rx.end(); i++)
			cur.pb(*i);

		if(cur.size() == 1){
			occ.insert(cur[0]);
			update(0, n - 1, bts[cur[0]], 1, 1);
			ll x = query(0, n - 1, 0, bts[cur[0]], 1);
			ll y = query(0, n - 1, bts[cur[0]], n - 1, 1);
			ans += x * y;
			continue;
		}
		for(int i = 0; i < cur.size(); i++){
			update(0, n - 1, bts[cur[i]], 1, 1);
		}
		ll curv = 0;
		int mk = 0;
		for(int i = 0; i < cur.size(); i++){
			occ.insert(cur[i]);
			if(i == 0){
				//ll x = query(0, n - 1, bts[cur[i]] + 1, bts[cur[i + 1]] - 1, 1);
				ll y = query(0, n - 1, 0, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1) / 2));
				}
			}
			else if(i == cur.size() - 1){
				ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				ll y = query(0, n - 1, bts[cur[i]] + 1, n - 1, 1);
				//cerr << x << ' ' << y << '\n';
				if(x && y){
				//	cerr << x << " " << y << '\n';
					mk++;
					curv += ((x * (x + 1) / 2 ) +  (y * (y + 1) / 2));
				}
				else if(x){
					mk++;
					curv += (x * (x + 1) / 2);
				}
				else if(y){
					mk++;
					curv += (y * (y + 1) / 2);
				}
			}
			else{
				//ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]], 1);
				ll y = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1)) / 2);;
				}
			}
		}
		//cerr << curv << '\n';
		//cerr << ans << '\n';
		//cerr << occ.size() << " " << curv << '\n';
		ans += occ.size() * (occ.size() + 1) / 2 - curv;

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


#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 200010
using namespace std;
int tree[4 * N];
map<int, int> bts;
void update(int low, int high, int ind, int value, int node){
	if(low == high){
		tree[node] = value;
		return;
	}
	int mid = (low + high) / 2;
	if(ind <= mid)
		update(low, mid, ind, value, 2 * node);
	else update(mid + 1, high, ind, value, 2 * node + 1);
	tree[node] = tree[2 * node] + tree[2 * node + 1];
}
int query(int low, int high, int begin, int end, int node){
	if(low > end || high < begin || begin > end)
		return 0;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high) / 2;
	return query(low, mid, begin, end, 2 * node) + query(mid + 1, high, begin, end, 2 * node + 1);
}
map<int, set<int> > df;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	vector<int> rt;
	for(int i = 0; i < n; i++){
		int x, y;
		cin >> x >> y;
		rt.pb(x);
		df[y].insert(x);
	}
	sort(rt.begin(), rt.end());
	for(int i = 0; i < rt.size(); i++){
		bts[rt[i]] = lower_bound(rt.begin(), rt.end(), rt[i]) - rt.begin();
	}
	ll ans = 0;
	set<int> occ;
	for(auto it = df.rbegin(); it != df.rend(); it++){
		//cerr << ans << '\n';
		set<int> rx = it->second;
		vector<int> cur;// = it->second;
		for(auto i = rx.begin(); i != rx.end(); i++)
			cur.pb(*i);

		if(cur.size() == 1){
			occ.insert(cur[0]);
			update(0, n - 1, bts[cur[0]], 1, 1);
			ll x = query(0, n - 1, 0, bts[cur[0]], 1);
			ll y = query(0, n - 1, bts[cur[0]], n - 1, 1);
			ans += x * y;
			continue;
		}
		for(int i = 0; i < cur.size(); i++){
			update(0, n - 1, bts[cur[i]], 1, 1);
		}
		ll curv = 1;
		int mk = 0;
		for(int i = 0; i < cur.size(); i++){
			occ.insert(cur[i]);
			if(i == 0){
				//ll x = query(0, n - 1, bts[cur[i]] + 1, bts[cur[i + 1]] - 1, 1);
				ll y = query(0, n - 1, 0, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv *= ((y * (y + 1) / 2) + 1);
				}
			}
			else if(i == cur.size() - 1){
				ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				ll y = query(0, n - 1, bts[cur[i]] + 1, n - 1, 1);
				//cerr << x << ' ' << y << '\n';
				if(x && y){
				//	cerr << x << " " << y << '\n';
					mk++;
					curv *= ((x * (x + 1) / 2 + 1) * (y * (y + 1) / 2) + 1);
				}
				else if(x){
					mk++;
					curv *= (x * (x + 1) / 2 + 1);
				}
				else if(y){
					mk++;
					curv *= (y * (y + 1) / 2 + 1);
				}
			}
			else{
				//ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]], 1);
				ll y = query(0, n - 1, bts[cur[i]] + 1, bts[cur[i + 1]] - 1, 1);
				if(y){
					mk++;
					curv *= ((y * (y + 1)) / 2 + 1);;
				}
			}
		}
		//cerr << curv << '\n';
		//cerr << ans << '\n';
		curv -= 1;
		//cerr << occ.size() << " " << curv << '\n';
		ans += occ.size() * (occ.size() + 1) / 2 - curv;

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


#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 200010
using namespace std;
int tree[4 * N];
map<int, int> bts;
void update(int low, int high, int ind, int value, int node){
	if(low == high){
		tree[node] = value;
		return;
	}
	int mid = (low + high) / 2;
	if(ind <= mid)
		update(low, mid, ind, value, 2 * node);
	else update(mid + 1, high, ind, value, 2 * node + 1);
	tree[node] = tree[2 * node] + tree[2 * node + 1];
}
int query(int low, int high, int begin, int end, int node){
	if(low > end || high < begin || begin > end)
		return 0;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high) / 2;
	return query(low, mid, begin, end, 2 * node) + query(mid + 1, high, begin, end, 2 * node + 1);
}
map<int, set<int> > df;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	vector<int> rt;
	for(int i = 0; i < n; i++){
		int x, y;
		cin >> x >> y;
		rt.pb(x);
		df[y].insert(x);
	}
	sort(rt.begin(), rt.end());
	for(int i = 0; i < rt.size(); i++){
		bts[rt[i]] = lower_bound(rt.begin(), rt.end(), rt[i]) - rt.begin();
	}
	ll ans = 0;
	set<int> occ;
	for(auto it = df.rbegin(); it != df.rend(); it++){
		//cerr << ans << '\n';
		set<int> rx = it->second;
		vector<int> cur;// = it->second;
		for(auto i = rx.begin(); i != rx.end(); i++)
			cur.pb(*i);

		if(cur.size() == 1){
			occ.insert(cur[0]);
			update(0, n - 1, bts[cur[0]], 1, 1);
			ll x = query(0, n - 1, 0, bts[cur[0]] - 1, 1);
			ll y = query(0, n - 1, bts[cur[0]] + 1, n - 1, 1);
			ll prev  = ans;
			ll val = occ.size();
			ans += val * (val + 1) / 2 - (x * (x + 1) / 2) - (y * (y + 1) / 2);
			if(ans < 0){
				cout << x << " " << y << '\n';
				cout << occ.size() << '\n';
				cout << prev << '\n';
				cout << "HERE\n";
				return 0;
			}
			continue;
		}
		for(int i = 0; i < cur.size(); i++){
			update(0, n - 1, bts[cur[i]], 1, 1);
		}
		ll curv = 0;
		int mk = 0;
		for(int i = 0; i < cur.size(); i++){
			occ.insert(cur[i]);
			if(i == 0){
				//ll x = query(0, n - 1, bts[cur[i]] + 1, bts[cur[i + 1]] - 1, 1);
				ll y = query(0, n - 1, 0, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1) / 2));
				}
			}
			else if(i == cur.size() - 1){
				ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				ll y = query(0, n - 1, bts[cur[i]] + 1, n - 1, 1);
				//cerr << x << ' ' << y << '\n';
				if(x && y){
				//	cerr << x << " " << y << '\n';
					mk++;
					curv += ((x * (x + 1) / 2 ) +  (y * (y + 1) / 2));
				}
				else if(x){
					mk++;
					curv += (x * (x + 1) / 2);
				}
				else if(y){
					mk++;
					curv += (y * (y + 1) / 2);
				}
			}
			else{
				//ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]], 1);
				ll y = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1)) / 2);;
				}
			}
		}
		//cerr << curv << '\n';
		//cerr << ans << '\n';
		//cerr << occ.size() << " " << curv << '\n';
		ans += occ.size() * (occ.size() + 1) / 2 - curv;

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


#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 200010
using namespace std;
int tree[4 * N];
map<int, int> bts;
void update(int low, int high, int ind, int value, int node){
	if(low == high){
		tree[node] = value;
		return;
	}
	int mid = (low + high) / 2;
	if(ind <= mid)
		update(low, mid, ind, value, 2 * node);
	else update(mid + 1, high, ind, value, 2 * node + 1);
	tree[node] = tree[2 * node] + tree[2 * node + 1];
}
int query(int low, int high, int begin, int end, int node){
	if(low > end || high < begin || begin > end)
		return 0;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high) / 2;
	return query(low, mid, begin, end, 2 * node) + query(mid + 1, high, begin, end, 2 * node + 1);
}
map<int, set<int> > df;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	vector<int> rt;
	for(int i = 0; i < n; i++){
		int x, y;
		cin >> x >> y;
		rt.pb(x);
		df[y].insert(x);
	}
	sort(rt.begin(), rt.end());
	for(int i = 0; i < rt.size(); i++){
		bts[rt[i]] = lower_bound(rt.begin(), rt.end(), rt[i]) - rt.begin();
	}
	ll ans = 0;
	set<int> occ;
	for(auto it = df.rbegin(); it != df.rend(); it++){
		//cerr << ans << '\n';
		set<int> rx = it->second;
		vector<int> cur;// = it->second;
		for(auto i = rx.begin(); i != rx.end(); i++)
			cur.pb(*i);

		if(cur.size() == 1){
			occ.insert(cur[0]);
			update(0, n - 1, bts[cur[0]], 1, 1);
			ll x = query(0, n - 1, 0, bts[cur[0]], 1);
			ll y = query(0, n - 1, bts[cur[0]], n - 1, 1);
			ans += x * y;
			continue;
		}
		for(int i = 0; i < cur.size(); i++){
			update(0, n - 1, bts[cur[i]], 1, 1);
		}
		ll curv = 0;
		int mk = 0;
		for(int i = 0; i < cur.size(); i++){
			occ.insert(cur[i]);
			if(i == 0){
				//ll x = query(0, n - 1, bts[cur[i]] + 1, bts[cur[i + 1]] - 1, 1);
				ll y = query(0, n - 1, 0, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1) / 2));
				}
			}
			else if(i == cur.size() - 1){
				ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				ll y = query(0, n - 1, bts[cur[i]] + 1, n - 1, 1);
				//cerr << x << ' ' << y << '\n';
				if(x && y){
				//	cerr << x << " " << y << '\n';
					mk++;
					curv += ((x * (x + 1) / 2 ) +  (y * (y + 1) / 2));
				}
				else if(x){
					mk++;
					curv += (x * (x + 1) / 2);
				}
				else if(y){
					mk++;
					curv += (y * (y + 1) / 2);
				}
			}
			else{
				//ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]], 1);
				ll y = query(0, n - 1, bts[cur[i]] + 1, bts[cur[i + 1]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1)) / 2);;
				}
			}
		}
		//cerr << curv << '\n';
		//cerr << ans << '\n';
		//cerr << occ.size() << " " << curv << '\n';
		ans += occ.size() * (occ.size() + 1) / 2 - curv;

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


#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 200010
using namespace std;
int tree[4 * N];
map<int, int> bts;
void update(int low, int high, int ind, int value, int node){
	if(low == high){
		tree[node] = value;
		return;
	}
	int mid = (low + high) / 2;
	if(ind <= mid)
		update(low, mid, ind, value, 2 * node);
	else update(mid + 1, high, ind, value, 2 * node + 1);
	tree[node] = tree[2 * node] + tree[2 * node + 1];
}
int query(int low, int high, int begin, int end, int node){
	if(low > end || high < begin || begin > end)
		return 0;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high) / 2;
	return query(low, mid, begin, end, 2 * node) + query(mid + 1, high, begin, end, 2 * node + 1);
}
map<int, set<int> > df;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	vector<int> rt;
	for(int i = 0; i < n; i++){
		int x, y;
		cin >> x >> y;
		rt.pb(x);
		df[y].insert(x);
	}
	sort(rt.begin(), rt.end());
	for(int i = 0; i < rt.size(); i++){
		bts[rt[i]] = lower_bound(rt.begin(), rt.end(), rt[i]) - rt.begin();
	}
	ll ans = 0;
	set<int> occ;
	for(auto it = df.rbegin(); it != df.rend(); it++){
		//cerr << ans << '\n';
		set<int> rx = it->second;
		vector<int> cur;// = it->second;
		for(auto i = rx.begin(); i != rx.end(); i++)
			cur.pb(*i);

		if(cur.size() == 1){
			occ.insert(cur[0]);
			update(0, n - 1, bts[cur[0]], 1, 1);
			ll x = query(0, n - 1, 0, bts[cur[0]] - 1, 1);
			ll y = query(0, n - 1, bts[cur[0]] + 1, n - 1, 1);
			ll prev  = ans;
			ll val = occ.size();
			ans += val * (val + 1) / 2 - (x * (x + 1) / 2) - (y * (y + 1) / 2);
			if(ans < 0){
				cout << x << " " << y << '\n';
				cout << occ.size() << '\n';
				cout << prev << '\n';
				cout << "HERE\n";
				return 0;
			}
			continue;
		}
		for(int i = 0; i < cur.size(); i++){
			update(0, n - 1, bts[cur[i]], 1, 1);
		}
		ll curv = 0;
		int mk = 0;
		for(int i = 0; i < cur.size(); i++){
			occ.insert(cur[i]);
			if(i == 0){
				//ll x = query(0, n - 1, bts[cur[i]] + 1, bts[cur[i + 1]] - 1, 1);
				ll y = query(0, n - 1, 0, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1) / 2));
				}
			}
			else if(i == cur.size() - 1){
				ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				ll y = query(0, n - 1, bts[cur[i]] + 1, n - 1, 1);
				//cerr << x << ' ' << y << '\n';
				if(x && y){
				//	cerr << x << " " << y << '\n';
					mk++;
					curv += ((x * (x + 1) / 2 ) +  (y * (y + 1) / 2));
				}
				else if(x){
					mk++;
					curv += (x * (x + 1) / 2);
				}
				else if(y){
					mk++;
					curv += (y * (y + 1) / 2);
				}
			}
			else{
				//ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]], 1);
				ll y = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1)) / 2);;
				}
			}
		}
		//cerr << curv << '\n';
		//cerr << ans << '\n';
		//cerr << occ.size() << " " << curv << '\n';
		ll val = occ.size();
		ans += val * (val + 1) / 2 - curv;

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


#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 200010
using namespace std;
int tree[4 * N];
map<int, int> bts;
void update(int low, int high, int ind, int value, int node){
	if(low == high){
		tree[node] = value;
		return;
	}
	int mid = (low + high) / 2;
	if(ind <= mid)
		update(low, mid, ind, value, 2 * node);
	else update(mid + 1, high, ind, value, 2 * node + 1);
	tree[node] = tree[2 * node] + tree[2 * node + 1];
}
int query(int low, int high, int begin, int end, int node){
	if(low > end || high < begin || begin > end)
		return 0;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high) / 2;
	return query(low, mid, begin, end, 2 * node) + query(mid + 1, high, begin, end, 2 * node + 1);
}
map<int, set<int> > df;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	vector<int> rt;
	for(int i = 0; i < n; i++){
		int x, y;
		cin >> x >> y;
		rt.pb(x);
		df[y].insert(x);
	}
	sort(rt.begin(), rt.end());
	for(int i = 0; i < rt.size(); i++){
		bts[rt[i]] = lower_bound(rt.begin(), rt.end(), rt[i]) - rt.begin();
	}
	ll ans = 0;
	set<int> occ;
	for(auto it = df.rbegin(); it != df.rend(); it++){
		//cerr << ans << '\n';
		set<int> rx = it->second;
		vector<int> cur;// = it->second;
		for(auto i = rx.begin(); i != rx.end(); i++)
			cur.pb(*i);

		if(cur.size() == 1){
			occ.insert(cur[0]);
			update(0, n - 1, bts[cur[0]], 1, 1);
			ll x = query(0, n - 1, 0, bts[cur[0]] - 1, 1);
			ll y = query(0, n - 1, bts[cur[0]] + 1, n - 1, 1);
			ll prev  = ans;
			ans += occ.size() * (occ.size() + 1) / 2 - (x * (x + 1) / 2) - (y * (y + 1) / 2);
			if(ans < 0){
				cout << x << " " << y << '\n';
				cout << occ.size() << '\n';
				cout << prev << '\n';
				cout << "HERE\n";
				return 0;
			}
			continue;
		}
		for(int i = 0; i < cur.size(); i++){
			update(0, n - 1, bts[cur[i]], 1, 1);
		}
		ll curv = 0;
		int mk = 0;
		for(int i = 0; i < cur.size(); i++){
			occ.insert(cur[i]);
			if(i == 0){
				//ll x = query(0, n - 1, bts[cur[i]] + 1, bts[cur[i + 1]] - 1, 1);
				ll y = query(0, n - 1, 0, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1) / 2));
				}
			}
			else if(i == cur.size() - 1){
				ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				ll y = query(0, n - 1, bts[cur[i]] + 1, n - 1, 1);
				//cerr << x << ' ' << y << '\n';
				if(x && y){
				//	cerr << x << " " << y << '\n';
					mk++;
					curv += ((x * (x + 1) / 2 ) +  (y * (y + 1) / 2));
				}
				else if(x){
					mk++;
					curv += (x * (x + 1) / 2);
				}
				else if(y){
					mk++;
					curv += (y * (y + 1) / 2);
				}
			}
			else{
				//ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]], 1);
				ll y = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1)) / 2);;
				}
			}
		}
		//cerr << curv << '\n';
		//cerr << ans << '\n';
		//cerr << occ.size() << " " << curv << '\n';
		ans += occ.size() * (occ.size() + 1) / 2 - curv;

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


#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 200010
using namespace std;
int tree[4 * N];
map<int, int> bts;
void update(int low, int high, int ind, int value, int node){
	if(low == high){
		tree[node] = value;
		return;
	}
	int mid = (low + high) / 2;
	if(ind <= mid)
		update(low, mid, ind, value, 2 * node);
	else update(mid + 1, high, ind, value, 2 * node + 1);
	tree[node] = tree[2 * node] + tree[2 * node + 1];
}
int query(int low, int high, int begin, int end, int node){
	if(low > end || high < begin || begin > end)
		return 0;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high) / 2;
	return query(low, mid, begin, end, 2 * node) + query(mid + 1, high, begin, end, 2 * node + 1);
}
map<int, set<int> > df;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	vector<int> rt;
	for(int i = 0; i < n; i++){
		int x, y;
		cin >> x >> y;
		rt.pb(x);
		df[y].insert(x);
	}
	sort(rt.begin(), rt.end());
	for(int i = 0; i < rt.size(); i++){
		bts[rt[i]] = lower_bound(rt.begin(), rt.end(), rt[i]) - rt.begin();
	}
	ll ans = 0;
	set<int> occ;
	for(auto it = df.rbegin(); it != df.rend(); it++){
		//cerr << ans << '\n';
		set<int> rx = it->second;
		vector<int> cur;// = it->second;
		for(auto i = rx.begin(); i != rx.end(); i++)
			cur.pb(*i);

		if(cur.size() == 1){
			occ.insert(cur[0]);
			update(0, n - 1, bts[cur[0]], 1, 1);
			ll x = query(0, n - 1, 0, bts[cur[0]] - 1, 1);
			ll y = query(0, n - 1, bts[cur[0]] + 1, n - 1, 1);
			ans += occ.size() * (occ.size() + 1) / 2 - (x * (x + 1) / 2) - (y * (y + 1) / 2);
			continue;
		}
		for(int i = 0; i < cur.size(); i++){
			update(0, n - 1, bts[cur[i]], 1, 1);
		}
		ll curv = 0;
		int mk = 0;
		for(int i = 0; i < cur.size(); i++){
			occ.insert(cur[i]);
			if(i == 0){
				//ll x = query(0, n - 1, bts[cur[i]] + 1, bts[cur[i + 1]] - 1, 1);
				ll y = query(0, n - 1, 0, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1) / 2));
				}
			}
			else if(i == cur.size() - 1){
				ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				ll y = query(0, n - 1, bts[cur[i]] + 1, n - 1, 1);
				//cerr << x << ' ' << y << '\n';
				if(x && y){
				//	cerr << x << " " << y << '\n';
					mk++;
					curv += ((x * (x + 1) / 2 ) +  (y * (y + 1) / 2));
				}
				else if(x){
					mk++;
					curv += (x * (x + 1) / 2);
				}
				else if(y){
					mk++;
					curv += (y * (y + 1) / 2);
				}
			}
			else{
				//ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]], 1);
				ll y = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1)) / 2);;
				}
			}
		}
		//cerr << curv << '\n';
		//cerr << ans << '\n';
		//cerr << occ.size() << " " << curv << '\n';
		ans += occ.size() * (occ.size() + 1) / 2 - curv;

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


#include<bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 200010
using namespace std;
int tree[4 * N];
map<int, int> bts;
void update(int low, int high, int ind, int value, int node){
	if(low == high){
		tree[node] = value;
		return;
	}
	int mid = (low + high) / 2;
	if(ind <= mid)
		update(low, mid, ind, value, 2 * node);
	else update(mid + 1, high, ind, value, 2 * node + 1);
	tree[node] = tree[2 * node] + tree[2 * node + 1];
}
int query(int low, int high, int begin, int end, int node){
	if(low > end || high < begin || begin > end)
		return 0;
	if(low >= begin && high <= end)
		return tree[node];
	int mid = (low + high) / 2;
	return query(low, mid, begin, end, 2 * node) + query(mid + 1, high, begin, end, 2 * node + 1);
}
map<int, set<int> > df;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin >> n;
	vector<int> rt;
	for(int i = 0; i < n; i++){
		int x, y;
		cin >> x >> y;
		rt.pb(x);
		df[y].insert(x);
	}
	sort(rt.begin(), rt.end());
	for(int i = 0; i < rt.size(); i++){
		bts[rt[i]] = lower_bound(rt.begin(), rt.end(), rt[i]) - rt.begin();
	}
	ll ans = 0;
	set<int> occ;
	for(auto it = df.rbegin(); it != df.rend(); it++){
		//cerr << ans << '\n';
		set<int> rx = it->second;
		vector<int> cur;// = it->second;
		for(auto i = rx.begin(); i != rx.end(); i++)
			cur.pb(*i);

		if(cur.size() == 1){
			occ.insert(cur[0]);
			update(0, n - 1, bts[cur[0]], 1, 1);
			ll x = query(0, n - 1, 0, bts[cur[0]] - 1, 1);
			ll y = query(0, n - 1, bts[cur[0]] + 1, n - 1, 1);
			ll prev  = ans;
			ll val = occ.size();
			ans += val * (val + 1) / 2 - (x * (x + 1) / 2) - (y * (y + 1) / 2);
			if(ans < 0){
				cout << x << " " << y << '\n';
				cout << occ.size() << '\n';
				cout << prev << '\n';
				cout << "HERE\n";
				return 0;
			}
			continue;
		}
		for(int i = 0; i < cur.size(); i++){
			update(0, n - 1, bts[cur[i]], 1, 1);
		}
		ll curv = 0;
		int mk = 0;
		for(int i = 0; i < cur.size(); i++){
			occ.insert(cur[i]);
			if(i == 0){
				//ll x = query(0, n - 1, bts[cur[i]] + 1, bts[cur[i + 1]] - 1, 1);
				ll y = query(0, n - 1, 0, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1) / 2));
				}
			}
			else if(i == cur.size() - 1){
				ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				ll y = query(0, n - 1, bts[cur[i]] + 1, n - 1, 1);
				//cerr << x << ' ' << y << '\n';
				if(x && y){
				//	cerr << x << " " << y << '\n';
					mk++;
					curv += ((x * (x + 1) / 2 ) +  (y * (y + 1) / 2));
				}
				else if(x){
					mk++;
					curv += (x * (x + 1) / 2);
				}
				else if(y){
					mk++;
					curv += (y * (y + 1) / 2);
				}
			}
			else{
				//ll x = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]], 1);
				ll y = query(0, n - 1, bts[cur[i - 1]] + 1, bts[cur[i]] - 1, 1);
				if(y){
					mk++;
					curv += ((y * (y + 1)) / 2);;
				}
			}
		}
		//cerr << curv << '\n';
		//cerr << ans << '\n';
		//cerr << occ.size() << " " << curv << '\n';
		ll val = occ.size()
		ans += val * (val + 1) / 2 - curv;

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