#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <stack>
#include <cassert>
#include <fstream>
#include <cstring>
#include <numeric>
#include <ctype.h>
#include <stdio.h>
#define MOD 1000000007
#define exit return 0
#define inf 10000000000
#define exit return 0
#define PS system("pause")
#define mx(a,b) a>b?a:b
#define R0(i,n) for (i = 0; i < n; i++)
#define R1(i,n) for (i = 1; i <= n; i++)
#define MS(a,x) memset(x, a, sizeof(x));
#define left (v<<1)
#define right (left|1)
#define mid ((l+r)>>1)
#define SZ(x) ((int)x.size())
#define MX(a,b)a>b?a:b
const double eps = 1e-10;
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
#define ll long long
using ul = unsigned long long;
//using PII = pair<int, int>;
const int NN = (10000009);
long long sum[NN];
int m,n;
int a,b,c;
pair<long long,long long >gg[100005];

int main() {
    scanf("%d",&n);
    for (int i = 0; i <n; ++i) {
        cin>>gg[i].first>>gg[i].second;
    }
    sort(gg,gg+n);
    long long x = gg[0].first;
    for(int i=1;i<n;i++){
        if(x<gg[i].first){
            a++;
            x= gg[i].first;
        }
    }
    if(a==0){
        cout<<0;exit;
    }
    cout<<a;
    return 0;

}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <stack>
#include <cassert>
#include <fstream>
#include <cstring>
#include <numeric>
#include <ctype.h>
#include <stdio.h>
#define MOD 1000000007
#define exit return 0
#define inf 10000000000
#define exit return 0
#define PS system("pause")
#define mx(a,b) a>b?a:b
#define R0(i,n) for (i = 0; i < n; i++)
#define R1(i,n) for (i = 1; i <= n; i++)
#define MS(a,x) memset(x, a, sizeof(x));
#define left (v<<1)
#define right (left|1)
#define mid ((l+r)>>1)
#define SZ(x) ((int)x.size())
#define MX(a,b)a>b?a:b
const double eps = 1e-10;
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
#define ll long long
using ul = unsigned long long;
//using PII = pair<int, int>;
const int NN = (3005);
const int EE = 5*NN;
long long C[NN][NN];
int m,n;
int a,b,c;
pair<int,int>gg[NN];

int cmp(const pair<int,int>a,const pair<int,int>b){
    return a.first<b.first&&a.second>b.second;
}
int main() {
    scanf("%d",&n);
    for (int i = 0; i <n; ++i) {
        scanf("%d %d",&gg[i].first,&gg[i].second);
    }
    sort(gg,gg+n);
    int i;
    for( i=0;i<n;i++){
        if(gg[i].first>gg[i+1].first||gg[i].second<gg[i+1].second)break;
    }
    cout<<i;
    return 0;

}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <stack>
#include <cassert>
#include <fstream>
#include <cstring>
#include <numeric>
#include <ctype.h>
#include <stdio.h>
#define MOD 1000000007
#define exit return 0
#define inf 10000000000
#define exit return 0
#define PS system("pause")
#define mx(a,b) a>b?a:b
#define R0(i,n) for (i = 0; i < n; i++)
#define R1(i,n) for (i = 1; i <= n; i++)
#define MS(a,x) memset(x, a, sizeof(x));
#define left (v<<1)
#define right (left|1)
#define mid ((l+r)>>1)
#define SZ(x) ((int)x.size())
#define MX(a,b)a>b?a:b
const double eps = 1e-10;
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
#define ll long long
using ul = unsigned long long;
//using PII = pair<int, int>;
const int NN = (10000009);
long long sum[NN];
int m,n;
int a,b,c;
pair<int,int>gg[100005];

int cmp(const pair<int,int>a,const pair<int,int>b){
    return a.first<b.first&&a.second>b.second;
}
int main() {
    scanf("%d",&n);
    for (int i = 0; i <n; ++i) {
        scanf("%d %d",&gg[i].first,&gg[i].second);
        int x = gg[i].first/1000000;
        int y = gg[i].second/1000000;
        gg[i].first/=1000000;
        gg[i].second/=1000000;
        sum[x+1]+=1;
        sum[y+1]-=1;
    }
    for(int i=1;i<NN;i++){
        sum[i]+=sum[i-1];
    }
    for(int i=0;i<n;i++){
        if(sum[gg[i].first+1]>0&&sum[gg[i].second]>0){
            a++;
        }
    }
    if(a==0){
        cout<<0;exit;
    }
    cout<<a-1;
    return 0;

}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <stack>
#include <cassert>
#include <fstream>
#include <cstring>
#include <numeric>
#include <ctype.h>
#include <stdio.h>
#define MOD 1000000007
#define exit return 0
#define inf 10000000000
#define exit return 0
#define PS system("pause")
#define mx(a,b) a>b?a:b
#define R0(i,n) for (i = 0; i < n; i++)
#define R1(i,n) for (i = 1; i <= n; i++)
#define MS(a,x) memset(x, a, sizeof(x));
#define left (v<<1)
#define right (left|1)
#define mid ((l+r)>>1)
#define SZ(x) ((int)x.size())
#define MX(a,b)a>b?a:b
const double eps = 1e-10;
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
#define ll long long
using ul = unsigned long long;
//using PII = pair<int, int>;
const int NN = (3005);
const int EE = 5*NN;
long long C[NN][NN];
int m,n;
int a,b,c;
pair<int,int>gg[NN];

int cmp(const pair<int,int>a,const pair<int,int>b){
    return a.first<b.first&&a.second>b.second;
}
int main() {
    scanf("%d",&n);
    for (int i = 0; i <n; ++i) {
        scanf("%d %d",&gg[i].first,&gg[i].second);
    }
    sort(gg,gg+n);
    int i;
    for( i=0;i<n;i++){
        if(gg[i].first>gg[i+1].first||gg[i].second<gg[i+1].second)break;
    }
    cout<<n-i;
    return 0;

}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <stack>
#include <cassert>
#include <fstream>
#include <cstring>
#include <numeric>
#include <ctype.h>
#include <stdio.h>
#define MOD 1000000007
#define exit return 0
#define inf 10000000000
#define exit return 0
#define PS system("pause")
#define mx(a,b) a>b?a:b
#define R0(i,n) for (i = 0; i < n; i++)
#define R1(i,n) for (i = 1; i <= n; i++)
#define MS(a,x) memset(x, a, sizeof(x));
#define left (v<<1)
#define right (left|1)
#define mid ((l+r)>>1)
#define SZ(x) ((int)x.size())
#define MX(a,b)a>b?a:b
const double eps = 1e-10;
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
#define ll long long
using ul = unsigned long long;
//using PII = pair<int, int>;
const int NN = (10000009);
long long sum[NN];
int m,n;
int a,b,c;
pair<int,int>gg[100005];

int cmp(const pair<int,int>a,const pair<int,int>b){
    return a.first<b.first&&a.second>b.second;
}
int main() {
    scanf("%d",&n);
    for (int i = 0; i <n; ++i) {
        scanf("%d %d",&gg[i].first,&gg[i].second);
        int x = gg[i].first/n;
        int y = gg[i].second/n;
        gg[i].first/=n;
        gg[i].second/=n;
        sum[x+1]+=1;
        sum[y+1]-=1;
    }
    for(int i=1;i<NN;i++){
        sum[i]+=sum[i-1];
    }
    for(int i=0;i<n;i++){
        if(sum[gg[i].first+1]>0&&sum[gg[i].second]>0){
            a++;
        }
    }
    if(a==0){
        cout<<0;exit;
    }
    cout<<a-1;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    vector<pair<long long,long long > > v;
    int n;
    bool m = 0,f = 0;
    long long mx=0,mn=1000000001,val1,val2;
    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        long long x,y;
        cin >> x >> y;
        if(y>mx)mx=y;
        if(y<mn)mn=y;
        v.push_back({x,y});
    }
    if(n==1)return cout << "0",0;
    sort(v.begin(),v.end());
    for(int i = 0 ; i < n ; i++)
    {
        if(v[i].second==mx)
            val1=n-(i+1),m=1;
        else if(v[i].second==mn)
        {
            if(m&&!f)
                f=1;
            val2=i;
        }

    }
    if(!f)
    cout << val1+val2;
    else cout << max(val1,val2);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    vector<pair<long long,long long > > v;
    int n;
    long long mx=0,mn=1000000001,val1,val2;
    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        long long x,y;
        cin >> x >> y;
        if(y>mx)mx=y;
        if(y<mn)mn=y;
        v.push_back({x,y});
    }
    sort(v.begin(),v.end());
    for(int i = 0 ; i < n ; i++)
    {
        if(v[i].second==mx)
            val1=n-(i+1);
        if(v[i].second==mn)
            val2=i;
    }
    cout << max(val1,val2);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    vector<pair<long long,long long > > v;
    int n;
    long long mx=0,mn=LONG_LONG_MAX,val1,val2;
    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        long long x,y;
        cin >> x >> y;
        if(y>mx)mx=y;
        if(y<mn)mn=y;
        v.push_back({x,y});
    }
    sort(v.begin(),v.end());
    for(int i = 0 ; i < n ; i++)
    {
        if(v[i].second==mx)
            val1=n-(i+1);
        if(v[i].second==mn)
            val2=i;
    }
    cout << max(val1,val2);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    vector<pair<long long,long long > > v;
    int n;
    bool m = 0,f = 0;
    long long mx=0,cnt=0;
    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        long long x,y;
        cin >> x >> y;
        v.push_back({x,y});
    }
    if(n==1)return cout << "0",0;
    sort(v.begin(),v.end());
    for(int i = 0 ; i < n ; i++)
    {
        if(v[i].second>mx)mx=v[i].second;
        else cnt++;

    }
    cout << cnt;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<pair<long long,long long > >st;
int main()
{
    long long n,mx=0;
    cin >> n;
    for(int i = 0 ; i < n ; i++)
    {
        long long x,y;
        cin >> x >> y;
        if(y>mx)mx=y;
        st.push_back({x,y});
    }
    sort(st.begin(),st.end());
    for(long long i = 0 ; i < n ; i++)
    {
        if(st[i].second==mx)
            cout << max(i-1,n-i-1);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int p[200005];
vector<pair<int,int> > vec;

int bs(int i)
{
    int lo=0;
    int hi=vec.size()-1;
    int res=-1;

    while(lo<=hi)
    {
        int mid= (lo+hi)/2;

        if(vec[mid].first<vec[i].first)
        {
            lo=mid+1;
            res=mid;
        }
        else
        {
            hi=mid-1;
        }
    }
    return res;

}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
    int a,b;
    cin>>a>>b;
    vec.push_back(make_pair(a,b));

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

int mx=0;

for(int i=0;i<vec.size();i++)
{
    mx=max(mx,vec[i].second);
    p[i]=mx;
}

int cnt=0;
for(int i=0;i<n;i++)
{
int res = bs(i);
cnt+= (res!=-1);

}
cout<<cnt;

 }


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int p[200005];
vector<pair<int,int> > vec;

int bs(int i)
{
    int lo=0;
    int hi=vec.size()-1;
    int res=-1;

    while(lo<=hi)
    {
        int mid= (lo+hi)/2;

        if(vec[mid].first<vec[i].first)
        {
            lo=mid+1;
            res=mid;
        }
        else
        {
            hi=mid-1;
        }
    }
    return res;

}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
    int a,b;
    cin>>a>>b;
    vec.push_back(make_pair(a,b));

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

int mx=0;

for(int i=0;i<vec.size();i++)
{
    mx=max(mx,vec[i].second);
    p[i]=mx;
}

int cnt=0;
for(int i=0;i<n;i++)
{
int res = bs(i);
if(res!=-1)
    cnt+= (vec[res].second>vec[i].second);

}
cout<<cnt;

 }


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int p[200005];
vector<pair<int,int> > vec;

int bs(int i)
{
    int lo=0;
    int hi=vec.size()-1;
    int res=-1;

    while(lo<=hi)
    {
        int mid= (lo+hi)/2;

        if(vec[mid].first<vec[i].first)
        {
            lo=mid+1;
            res=mid;
        }
        else
        {
            hi=mid-1;
        }
    }
    return res;

}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
    int a,b;
    cin>>a>>b;
    vec.push_back(make_pair(a,b));

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

int mx=0;

for(int i=0;i<vec.size();i++)
{
    mx=max(mx,vec[i].second);
    p[i]=mx;
}

int cnt=0;
for(int i=0;i<n;i++)
{
int res = bs(i);
if(res!=-1)
    cnt+= (p[res]>vec[i].second);

}
cout<<cnt;

 }


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   cin>>n;
   pair<int,int>p[n];
   for(int i=0;i<n;i++)
    cin>>p[i].first>>p[i].second;
   sort(p,p+n);
   int w=0,i=0;
   while(i<n)
     {
         int j=i+1;
         int x=p[i].first,y=p[i].second;
         if(j<n)
         {
             while(y>p[j].first&&j<n)
             {
                 if(y>p[j].second)
                 {
                 w++;
                     i++;
                     j++;

                 }
                 else
                 {
                     x=p[j].first;
                     y=p[j].second;
                     i++;
                     j++;
                 }
             }
         }
         else
            break;
     }
     cout<<w;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   cin>>n;
   pair<int,int>p[n];
   for(int i=0;i<n;i++)
    cin>>p[i].first>>p[i].second;
   sort(p,p+n);
   int w=0,i=0;
   while(i<n)
     {
         int j=i+1;
         int x=p[i].first,y=p[i].second;
         if(j<n)
         {
             while(y>p[j].first&&j<n)
             {
                 w++;
                 if(y>p[j].second)
                 {
                     i++;
                     j++;

                 }
                 else
                 {
                     x=p[j].first;
                     y=p[j].second;
                     i++;
                     j++;
                 }
             }
         }
         else
            break;
     }
     cout<<w;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
   int n;
   cin>>n;
   pair<int,int>p[n];
   for(int i=0;i<n;i++)
    cin>>p[i].first>>p[i].second;
   sort(p,p+n);
   int w=0,i=0;
   while(i<n)
     {
         int j=i+1;
         int x=p[i].first,y=p[i].second;
         if(j<n)
         {
             while(y>p[j].first&&j<n)
             {
                 if(y>p[j].second)
                 {
                 w++;
                     i++;
                     j++;

                 }
                 else
                 {
                     x=p[j].first;
                     y=p[j].second;
                     i++;
                     j++;
                 }
             }
             i++;
         }
         else
            break;
     }
     cout<<w;

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define PI acos(-1)
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]"<<endl;;}
#define pnt(v,s) print(v,sz(v),s)
#define rd read("C:\\Users\\Miller\\Desktop\\in.txt")
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

typedef long long ll ;
const double EPS = (1e-9);

int n , a , b , cnt = 0 ;
vector<pair<int,int>> d ;
vector<int> nums ;
unordered_map<int,int> m ;

int A[2*O5] , cover[2*O5] ;
bool start[2*O5] ;

int main()
{
    fast ;

    cin >> n ;

    for(int i = 0 ; i < n ; ++i){
        cin >> a >> b ;
        d.push_back(make_pair(a,b));
        nums.push_back(a);
        nums.push_back(b);
    }

    sort(nums.begin(),nums.end());

    for(int i = 0 ; i < nums.size() ; ++i){
        m[nums[i]] = ++cnt ;
    }

    for(int i = 0 ; i < d.size() ; ++i){
        d[i].first = m[d[i].first] ;
        d[i].second = m[d[i].second] ;
        A[d[i].first] = A[d[i].second] = i+1 ;//in left&right position I saved same value
        start[d[i].first] = 1 ;
    }

    set<int> maxi;
    for(int i = O5*2-2 ; i ; --i){
        if( !start[i] && A[i] )//end
            maxi.insert(i);
        else if( start[i] ){
            maxi.erase( d[A[i]-1].second );
        }

        set<int>::iterator it = maxi.end();
        if( maxi.size()==0 )continue ;

        --it ;
        cover[i] = *it ;
    }

    cnt = 0 ;
    for(int i = 0 ; i < O5*2-1 ; ++i){
        if( start[i] && d[A[i]-1].second < cover[i] )++cnt ;
    }

    cout<<cnt ;

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define PI acos(-1)
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]"<<endl;;}
#define pnt(v,s) print(v,sz(v),s)
#define rd read("C:\\Users\\Miller\\Desktop\\in.txt")
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

typedef long long ll ;
const double EPS = (1e-9);

int n , a , b , cnt = 0 ;
vector<pair<int,int>> d ;

int main()
{
    fast ;

    cin >> n ;

    for(int i = 0 ; i < n ; ++i){
        cin >> a >> b ;
        d.push_back(make_pair(a,b));
    }

    sort(d.begin(),d.end());

    int maxi = -1 ;
    for(int i = 0 ; i < d.size() ; ++i){
        if( d[i].second < maxi )++cnt ;
        else maxi = d[i].second ;
    }

    cout<<cnt ;

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define PI acos(-1)
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]"<<endl;;}
#define pnt(v,s) print(v,sz(v),s)
#define rd read("C:\\Users\\Miller\\Desktop\\in.txt")
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

typedef long long ll ;
const double EPS = (1e-9);

int n , a , b , cnt = 0 ;
vector<pair<int,int>> d ;
vector<int> nums ;
unordered_map<int,int> m ;

int A[O5] , cover[O5] ;
bool start[O5] ;

int main()
{
    fast ;

    cin >> n ;

    for(int i = 0 ; i < n ; ++i){
        cin >> a >> b ;
        d.push_back(make_pair(a,b));
        nums.push_back(a);
        nums.push_back(b);
    }

    sort(nums.begin(),nums.end());

    for(int i = 0 ; i < nums.size() ; ++i){
        m[nums[i]] = ++cnt ;
    }

    for(int i = 0 ; i < d.size() ; ++i){
        d[i].first = m[d[i].first] ;
        d[i].second = m[d[i].second] ;
        A[d[i].first] = A[d[i].second] = i+1 ;//in left&right position I saved same value
        start[d[i].first] = 1 ;
    }

    set<int> maxi;
    for(int i = O5-1 ; i ; --i){
        if( !start[i] && A[i] )
            maxi.insert(i);
        else if( start[i] ){
            maxi.erase( d[A[i]-1].second );
        }

        set<int>::iterator it = maxi.end();
        if( maxi.size()==0 )continue ;

        --it ;
        cover[i] = *it ;
    }

    cnt = 0 ;
    for(int i = 0 ; i < O5 ; ++i){
        if( start[i] && d[A[i]-1].second < cover[i] )++cnt ;
    }

    cout<<cnt ;

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define PI acos(-1)
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]"<<endl;;}
#define pnt(v,s) print(v,sz(v),s)
#define rd read("C:\\Users\\Miller\\Desktop\\in.txt")
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

typedef long long ll ;
const double EPS = (1e-9);

int n , a , b , cnt = 0 ;
vector<pair<int,int>> d ;
vector<int> nums ;
unordered_map<int,int> m ;

int A[2*O5] , cover[2*O5] ;
bool start[2*O5] ;

int main()
{
    fast ;

    cin >> n ;

    for(int i = 0 ; i < n ; ++i){
        cin >> a >> b ;
        d.push_back(make_pair(a,b));
        nums.push_back(a);
        nums.push_back(b);
    }

    sort(nums.begin(),nums.end());

    for(int i = 0 ; i < nums.size() ; ++i){
        m[nums[i]] = ++cnt ;
    }

    for(int i = 0 ; i < d.size() ; ++i){
        d[i].first = m[d[i].first] ;
        d[i].second = m[d[i].second] ;
        A[d[i].first] = A[d[i].second] = i+1 ;//in left&right position I saved same value
        start[d[i].first] = 1 ;
    }

    set<int> maxi;
    for(int i = O5-1 ; i ; --i){
        if( !start[i] && A[i] )
            maxi.insert(i);
        else if( start[i] ){
            maxi.erase( d[A[i]-1].second );
        }

        set<int>::iterator it = maxi.end();
        if( maxi.size()==0 )continue ;

        --it ;
        cover[i] = *it ;
    }

    cnt = 0 ;
    for(int i = 0 ; i < O5 ; ++i){
        if( start[i] && d[A[i]-1].second < cover[i] )++cnt ;
    }

    cout<<cnt ;

    return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<set>
#include<iomanip>

#define forn(i,a,b) for(int i = a; i < b; i++)
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define eb emplace_back
#define pb push_back
#define fr first
#define sc second
#define sins insert

typedef long long ll;

#define M 1000000000
const int mod = 1000000007;

using namespace std;

bool cmp(const pair<int,int> &p1,const pair<int,int> &p2)
{
    return p1.sc>p2.sc;
}

int main()
{
    boost;
   int n;
   cin >> n;
   pair<int, int> p[n];
   forn(i,0,n)
   {
       cin >> p[i].fr >> p[i].sc;
   }
   sort(p,p+n,cmp);
   int ans = 0, min = p[0].fr+1;
   forn(i,0,n)
   {
       if(min > p[i].fr)
        {
            min = p[i].fr;
            continue;
        }
       ans++;
   }
   cout << ans;
   return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<set>
#include<iomanip>

#define forn(i,a,b) for(int i = a; i < b; i++)
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define eb emplace_back
#define pb push_back
#define fr first
#define sc second
#define sins insert

typedef long long ll;

#define M 1000000000
const int mod = 1000000007;

using namespace std;

bool cmp(const pair<int,int> &p1,const pair<int,int> &p2)
{
    return p1.sc>p2.sc;
}

int main()
{
    boost;
   int n;
   cin >> n;
   pair<int, int> p[n];
   forn(i,0,n)
   {
       cin >> p[i].fr >> p[i].sc;
   }
   sort(p,p+n,cmp);
   int ans = 0, min = p[0].fr+1;
   forn(i,0,n)
   {
       if(min > p[i].fr)
        continue;
       ans++;
       min = p[i].fr;
   }
   cout << ans;
   return 0;
}


#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<set>
#include<iomanip>

#define forn(i,a,b) for(int i = a; i < b; i++)
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define eb emplace_back
#define pb push_back
#define fr first
#define sc second
#define sins insert

typedef long long ll;

#define M 1000000000
const int mod = 1000000007;

using namespace std;

bool cmp(const pair<int,int> &p1,const pair<int,int> &p2)
{
    return p1.sc>p2.sc;
}

int main()
{
    boost;
   int n;
   cin >> n;
   pair<int, int> p[n];
   forn(i,0,n)
   {
       cin >> p[i].fr >> p[i].sc;
   }
   sort(p,p+n,cmp);
   int ans = 0, min = p[0].fr+1;
   forn(i,0,n)
   {
       if(min > p[i].fr)
        continue;
       ans++;
   }
   cout << ans;
   return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<regex>
#include<stdlib.h>
#include<math.h>

using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define oo (1<<30)
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef vector<int> vi;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
ll n;
vector<pair<long long, long long> > vec;
bool com(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first)
        return a.second > b.second;
    return a.first < b.first;
}
int main() {
#ifndef ONLINE_JUDGE
    //freopen("test.in", "rt", stdin);
    //    freopen("o.txt", "wt", stdout);
#endif
    scanf("%lld", &n);
    vec.resize(n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &vec[i].first, &vec[i].second);
    }
    sort(vec.begin(), vec.end(), com);
    ll ans = 0;
    ll last = 0;
    for (int i = 1; i < n; i++) {
        if (vec[last].first < vec[i].first && vec[last].second > vec[i].second)
            ans++;
        if (vec[i].second - vec[i].first > vec[last].second - vec[last].first)
            last = i;
    }
    printf("%lld\n", ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<regex>
#include<stdlib.h>
#include<math.h>

using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define oo (1<<30)
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef vector<int> vi;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
ll n;
vector<pair<long long, long long> > vec;
bool com(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first)
        return a.second > b.second;
    return a.first < b.first;
}
int main() {
#ifndef ONLINE_JUDGE
    //freopen("test.in", "rt", stdin);
    //    freopen("o.txt", "wt", stdout);
#endif
    scanf("%lld", &n);
    vec.resize(n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &vec[i].first, &vec[i].second);
    }
    sort(vec.begin(), vec.end(), com);
    ll ans = 0;
    ll last = 0;
    for (int i = 1; i < n; i++) {
        if (vec[last].first < vec[i].first && vec[last].second > vec[i].second)
            ans++;
        if (vec[i].second > vec[last].second)
            last = i;
    }
    printf("%lld\n", ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<regex>
#include<stdlib.h>
#include<math.h>

using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define oo (1<<30)
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef vector<int> vi;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
ll n;
vector<pair<long long, long long> > vec;
bool com(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first)
        return a.second > b.second;
    return a.first < b.first;
}
int main() {
#ifndef ONLINE_JUDGE
    //freopen("test.in", "rt", stdin);
    //    freopen("o.txt", "wt", stdout);
#endif
    scanf("%lld", &n);
    vec.resize(n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &vec[i].first, &vec[i].second);
    }
    sort(vec.begin(), vec.end(), com);
    ll ans = 0;
    ll last = 0;
    for (int i = 1; i < n; i++) {
        if (vec[last].first < vec[i].first && vec[last].second > vec[i].second)
            ans++;
        if (vec[i].first>vec[last].second)
            last = i;
    }
    printf("%lld\n", ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<regex>
#include<stdlib.h>
#include<math.h>

using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define oo (1<<30)
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef vector<int> vi;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
ll n;
vector<pair<long long, long long> > vec;
bool com(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first)
        return a.second > b.second;
    return a.first < b.first;
}
int main() {
#ifndef ONLINE_JUDGE
    //freopen("test.in", "rt", stdin);
    //    freopen("o.txt", "wt", stdout);
#endif
    scanf("%lld", &n);
    vec.resize(n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &vec[i].first, &vec[i].second);
    }
    sort(vec.begin(), vec.end(), com);
    ll ans = 0;
    ll last = 0;
    for (int i = 1; i < n; i++) {
        if (vec[last].first < vec[i].first && vec[last].second > vec[i].second)
            ans++;
    }
    printf("%lld\n", ans);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<regex>
#include<stdlib.h>
#include<math.h>

using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define oo (1<<30)
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef vector<int> vi;

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
ll n;
vector<pair<long long, long long> > vec;
bool com(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first)
        return a.second > b.second;
    return a.first < b.first;
}
int main() {
#ifndef ONLINE_JUDGE
    //freopen("test.in", "rt", stdin);
    //    freopen("o.txt", "wt", stdout);
#endif
    scanf("%lld", &n);
    vec.resize(n);
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &vec[i].first, &vec[i].second);
    }
    sort(vec.begin(), vec.end(), com);
    ll ans = 0;
    ll last = 0;
    for (int i = 1; i < n; i++) {
        if (vec[last].first < vec[i].first && vec[last].second > vec[i].second)
            ans++;
        if (vec[i].first>=vec[last].second)
            last = i;
    }
    printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<vector<int> >vvi;
typedef vector<int> vi;
bool used[100001];
ii a[100001];
struct node{
    bool isB;
    int idx;
    int val;
    node(bool _isB,int _idx,int _val)
    {
        isB=_isB;
        idx=_idx;
        val=_val;
    }
    void print()
    {
        cout<<isB<<" "<<idx<<" "<<val<<endl;
    }
};
bool comp(node a,node b)
{
    if(a.val!=b.val)
    {
        return a.val<=b.val;
    }
    return !(a.isB);
}
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    vector<node>v;
    for(int i=0;i<n;i++){
        cin>>a[i].first>>a[i].second;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        v.push_back(node(true,i,a[i].first));
        v.push_back(node(false,i,a[i].second));
    }

    sort(v.begin(),v.end(),comp);
    int cnt=0;
    int curIdx=-1;
    int curVal=-1;
    int closingVal=-1;
    for(int i=0;i<v.size();i++)
    {
        //v[i].print();
        //cout<<curIdx<<endl;

        if(v[i].isB)
        {
            if(curIdx==-1)
            {
                used[v[i].idx]=1;
                curIdx=v[i].idx;
                curVal=v[i].val;
                closingVal=a[v[i].idx].second;
            }
        }
        else{
            if(v[i].idx!=curIdx)
            {
                cnt++;
            }
            else{
                while(curIdx<n)
                {
                    curIdx++;
                    if(!used[curIdx])
                    {
                        used[v[i].idx]=1;
                        curIdx=v[i].idx;
                        curVal=v[i].val;
                        closingVal=a[v[i].idx].second;
                        break;
                    }
                }
            }
        }
        if(curIdx>=n)
            break;
    }
    cout<<cnt<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<vector<int> >vvi;
typedef vector<int> vi;
bool used[100001];
ii a[100001];
struct node{
    bool isB;
    int idx;
    int val;
    node(bool _isB,int _idx,int _val)
    {
        isB=_isB;
        idx=_idx;
        val=_val;
    }
    void print()
    {
        cout<<isB<<" "<<idx<<" "<<val<<endl;
    }
};
bool comp(node a,node b)
{
    if(a.val!=b.val)
    {
        return a.val<=b.val;
    }
    return !(a.isB);
}
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    vector<node>v;
    for(int i=0;i<n;i++){
        cin>>a[i].first>>a[i].second;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        v.push_back(node(true,i,a[i].first));
        v.push_back(node(false,i,a[i].second));
    }

    sort(v.begin(),v.end(),comp);
    int cnt=0;
    int curIdx=-1;
    int curVal=-1;
    int closingVal=-1;
    for(int i=0;i<v.size();i++)
    {

        if(v[i].isB)
        {
            if(curIdx==-1)
            {
                used[v[i].idx]=1;
                curIdx=v[i].idx;
                curVal=v[i].val;
                closingVal=a[v[i].idx].second;
            }
        }
        else{
            if(v[i].idx!=curIdx)
            {
                cnt++;
            }
            else{
                while(curIdx<n)
                {
                    curIdx++;
                    if(curIdx<n && !used[curIdx])
                    {
                        used[curIdx]=1;
                        curIdx=curIdx;
                        curVal=a[curIdx].first;
                        closingVal=a[curIdx].second;
                        break;
                    }
                }
            }
        }
        if(curIdx>=n)
            break;
    }
    cout<<cnt<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef vector<vector<int> >vvi;
typedef vector<int> vi;
bool used[100001];
ii a[100001];
struct node{
    bool isB;
    int idx;
    int val;
    node(bool _isB,int _idx,int _val)
    {
        isB=_isB;
        idx=_idx;
        val=_val;
    }
    void print()
    {
        cout<<isB<<" "<<idx<<" "<<val<<endl;
    }
};
bool comp(node a,node b)
{
    if(a.val!=b.val)
    {
        return a.val<=b.val;
    }
    return !(a.isB);
}
int main()
{
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    vector<node>v;
    for(int i=0;i<n;i++){
        cin>>a[i].first>>a[i].second;
    }
    sort(a,a+n);
    for(int i=0;i<n;i++){
        v.push_back(node(true,i,a[i].first));
        v.push_back(node(false,i,a[i].second));
    }

    sort(v.begin(),v.end(),comp);
    int cnt=0;
    int curIdx=-1;
    int curVal=-1;
    int closingVal=-1;
    for(int i=0;i<v.size();i++)
    {

        if(v[i].isB)
        {
            if(curIdx==-1)
            {
                used[v[i].idx]=1;
                curIdx=v[i].idx;
                curVal=v[i].val;
                closingVal=a[v[i].idx].second;
            }
        }
        else{
            if(v[i].idx!=curIdx)
            {
                cnt++;
                used[v[i].idx]=1;
            }
            else{
                while(curIdx<n)
                {
                    curIdx++;
                    if(curIdx<n && !used[curIdx])
                    {
                        used[curIdx]=1;
                        curIdx=curIdx;
                        curVal=a[curIdx].first;
                        closingVal=a[curIdx].second;
                        break;
                    }
                }
            }
        }
        //cout<<curIdx<<endl;
        if(curIdx>=n)
            break;
    }
    cout<<cnt<<endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< pair <int,int> > vect;
vector<int> arr ;
vector<int> arr1 ;
long long a,b,n;
int nb=0;
cin>>n;
cin>> a ;
cin>> b ;
arr.push_back(a);
arr1.push_back(b);
vect.push_back( make_pair(arr[0],arr1[0]) );
for (int i=1;i<n ; i++){
	do{
	cin>> a ;
	cin>> b;
       }while(a==arr[i-1]|| b==arr1[i-1]);
	arr.push_back(a);
	arr1.push_back(b);
	vect.push_back( make_pair(arr[i],arr1[i]) );
}

sort(vect.begin(), vect.end());
if (n==1){
printf (0);
}
else{
	int max=vect[0].second;
	for(int i=1;i<n;i++){
	if(vect[i].second<max)
		nb++;
	else
		max=vect[i].second;
				}
		}

cout<< nb;
return 0;
}


#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< pair <int,int> > vect;
vector<int> arr ;
vector<int> arr1 ;
long long a,b,n;
int nb=0;
cin>>n;
cin>> a ;
cin>> b ;
arr.push_back(a);
arr1.push_back(b);
for (int i=1;i<n ; i++){
	do{
	cin>> a ;
	cin>> b;
       }while(a==arr[i-1]|| b==arr1[i-1]);
	arr.push_back(a);
	arr1.push_back(b);
}
for (int i=0; i<=n; i++)
vect.push_back( make_pair(arr[i],arr1[i]) );
sort(vect.begin(), vect.end());
if (n==1){
printf (0);
}
else{
	for(int i=n-1;i>=1;i--){
		for(int j=i-1;j>=0;j--){
				if (vect[i].second<vect[j].second){
					nb++;
					break;
				}
		}

}
}
cout<< nb;
return 0;
}


#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< pair <int,int> > vect;
vector<int> arr ;
vector<int> arr1 ;
long long a,b,n;
int nb=0;
cin>>n;
cin>> a ;
cin>> b ;
arr.push_back(a);
arr1.push_back(b);
vect.push_back( make_pair(arr[0],arr1[0]) );
for (int i=1;i<n ; i++){
	do{
	cin>> a ;
	cin>> b;
       }while(a==arr[i-1]|| b==arr1[i-1]);
	arr.push_back(a);
	arr1.push_back(b);
	vect.push_back( make_pair(arr[i],arr1[i]) );
}

sort(vect.begin(), vect.end());
if (n==1){
printf (0);
}
else{
	for(int i=n-1;i>=1;i--){
		for(int j=i-1;j>=0;j--){
				if (vect[i].second<vect[j].second){
					nb++;
					break;
				}
		}
}
}
cout<< nb;
return 0;
}


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

using namespace std;

int main()
{

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

for(int i=n-1;i>0;i--)
for(int j=1;j<n;j++){
    if(a[i]>a[i-j]&&b[i]<b[i-j]){
        count++;
        break;}

}

cout<<count<<"\n";

}


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

using namespace std;

int main()
{

int n,j=1;
cin>>n;
long long int count=0,l=0,i;
pair<int,int>a[n];
for(int i=0;i<n;i++){
    cin>>a[i].first>>a[i].second;
    }
sort(a,a+n);

l=0;
for(i=0;i<n;i++)
   if(l>a[i].second)
    count++;
		else l=a[i].second;

cout<<count;

}


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

using namespace std;

int main()
{

int n,j=1;
cin>>n;
long long int a[n],b[n],count=0,k,l;
for(int i=0;i<n;i++){
    cin>>a[i]>>b[i];
    }
k=a[n-1];
l=b[n-1];

for(int i=n-1;i>=0;i--){
        k=a[i],l=b[i];
for(j=n-1;j>=0;j--)

    if(k>a[j]&&l<b[j]){
        count++;

        break;}

}
cout<<count;

}


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

using namespace std;

int main()
{

int n,j=1;
cin>>n;
long long int a[n],b[n],count=0;
for(int i=0;i<n;i++){
    cin>>a[i]>>b[i];
    }

for(int i=n-1;i>=0;i--){
for(j=1;j<=i;j++)

    if(a[i]>a[i-j]&&b[i]<b[i-j]){
        count++;

        break;}

}
cout<<count;

}


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

using namespace std;

int main()
{

int n,j=1;
cin>>n;
long long int a[n],b[n],count=0;
for(int i=0;i<n;i++){
    cin>>a[i]>>b[i];
    }

for(int i=n-1;i>0;i--){
for(j=1;j<=i;j++)

    if(a[i]>a[i-j]&&b[i]<b[i-j]){
        count++;

        break;}

}
cout<<count;

}


#include <bits/stdc++.h>

using namespace std;

long long n,a,b,res,ab;
vector <pair<long long,long long> >arr;
stack <long long>pila;
bool usa[100005];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a>>b;
        arr.push_back(make_pair(a,i*10+1));
        arr.push_back(make_pair(b,i*10+2));
    }
    sort(arr.begin(),arr.end());
    /*for(int i=0;i<arr.size();i++){
        if(arr[i].second%10==1)pila.push(arr[i].second/10);
        else{
            if(!pila.empty()){
                while(!pila.empty() && pila.top()!=arr[i].second/10)pila.pop();
                if(!pila.empty()){
                    if(pila.size()!=1)res++;
                    pila.pop();
                }
            }
        }
    }*/
    for(int i=0;i<arr.size();i++){
        if(arr[i].second%10==1)ab++;
        else{
            if(ab>=2)res++;
            ab--;
        }
    }
    cout<<res;
}


#include <bits/stdc++.h>

using namespace std;

long long n,a,b,res,ab;
vector <pair<long long,long long> >arr;
queue <long long>ext;
bool usa[100005];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a>>b;
        arr.push_back(make_pair(a,i*10+1));
        arr.push_back(make_pair(b,i*10+2));
    }
    sort(arr.begin(),arr.end());
    /*for(int i=0;i<arr.size();i++){
        if(arr[i].second%10==1)pila.push(arr[i].second/10);
        else{
            if(!pila.empty()){
                while(!pila.empty() && pila.top()!=arr[i].second/10)pila.pop();
                if(!pila.empty()){
                    if(pila.size()!=1)res++;
                    pila.pop();
                }
            }
        }
    }*/
    for(int i=0;i<arr.size();i++){
        if(arr[i].second%10==1)ext.push(arr[i].second/10);
        else{
            if(arr[i].second/10==ext.front()){
                ext.pop();
                while(usa[ext.front()] && !ext.empty())ext.pop();
            }
            else {
                usa[arr[i].second/10]=true;
                res++;
            }
        }
    }
    cout<<res;
}


#include <bits/stdc++.h>

using namespace std;

long long n,a,b,res,ab;
vector <pair<long long,long long> >arr;
queue <long long>ext;
bool usa[100005];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a>>b;
        arr.push_back(make_pair(a,i*10+1));
        arr.push_back(make_pair(b,i*10+2));
    }
    sort(arr.begin(),arr.end());
    /*for(int i=0;i<arr.size();i++){
        if(arr[i].second%10==1)pila.push(arr[i].second/10);
        else{
            if(!pila.empty()){
                while(!pila.empty() && pila.top()!=arr[i].second/10)pila.pop();
                if(!pila.empty()){
                    if(pila.size()!=1)res++;
                    pila.pop();
                }
            }
        }
    }*/
    for(int i=0;i<arr.size();i++){
        if(arr[i].second%10==1)ext.push(arr[i].second/10);
        else{
            if(arr[i].second/10==ext.front()){
                ext.pop();
                while(!ext.empty()&&usa[ext.front()])ext.pop();
            }
            else {
                usa[arr[i].second/10]=true;
                res++;
            }
        }
    }
    cout<<res;
}


#include <bits/stdc++.h>

using namespace std;

long long n,a,b,res,ab;
vector <pair<long long,long long> >arr;
queue <long long>ext;
bool usa[100005];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a>>b;
        arr.push_back(make_pair(a,i*10+1));
        arr.push_back(make_pair(b,i*10+2));
    }
    sort(arr.begin(),arr.end());
    /*for(int i=0;i<arr.size();i++){
        if(arr[i].second%10==1)pila.push(arr[i].second/10);
        else{
            if(!pila.empty()){
                while(!pila.empty() && pila.top()!=arr[i].second/10)pila.pop();
                if(!pila.empty()){
                    if(pila.size()!=1)res++;
                    pila.pop();
                }
            }
        }
    }*/
    for(int i=0;i<arr.size();i++){
        if(arr[i].second%10==1)ext.push(arr[i].second/10);
        else{
            if(arr[i].second/10==ext.front())ext.pop();
            else res++;
        }
    }
    cout<<res;
}


#include <iostream>
using namespace std;
int main()
{
    int n,i,a[1000],b[1000];

    scanf("%d",&n);
int indmax=0,max;
max=b[indmax]-a[indmax];
for(int i=0;i<n;i++)
{
    scanf("%d",&a[i]);
scanf("%d",&b[i]);
}

for(int i=0;i<n;i++)
{
if((b[i]-a[i])>=max)
{
indmax=i;
max=b[i]-a[i];
}
}

int nb=0;
for(i=0;i<n;i++)
if(a[indmax]<=a[i] && b[indmax]>=b[i])
nb++;

cout << nb-1;

return 0;
}


#include <iostream>

using namespace std;

int c,d,i,n,m,k,x,j;
pair<int,int>tab[1000000];
main(){
	cin>>n;
	for(i=0;i<n;i++)	scanf("%d",&tab[i].first);
scanf("%d",&tab[i].second);
	sort(tab,tab+n);
	for(i=0;i<n;i++){
		if(tab[i].second<c)k++;
else c=tab[i].second;
}
	cout<<k;
}


#include <iostream>
#include <algorithm>

using namespace std;
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
int main()
{

pair<int, int> tab[1000000];
pair<int, int> vect[1000000];
int n,i,j;
scanf("%d", &n);

for (i = 0; i < n; i++)
{
scanf("%d%d", &tab[i].first, &tab[i].second);
vect[i].first=tab[i].first;
vect[i].second=tab[i].second;
}
sort(tab, tab+n );
sort(vect, vect+n, sortbysec);
int min=tab[0].first,max=vect[n-1].second;

if(tab[0].second<vect[0].first){
min=vect[0].first;
}

if(tab[0].second>=max){

max=tab[0].second;
}
int nb=0;
for(i=0;i<n;i++)
if(tab[i].first>=min && tab[i].second<=max) nb++;
printf("%d ",nb-1);
return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int c,d,i,n,m,k,x,j;
pair<int,int>tab[1000000];
main(){
	cin>>n;
	for(i=0;i<n;i++)	scanf("%d",&tab[i].first);
scanf("%d",&tab[i].second);
	sort(tab,tab+n);
	for(i=0;i<n;i++){
		if(tab[i].second<c)k++;
else c=tab[i].second;
}
	cout<<k;
}


#include <iostream>
using namespace std;
int main()
{
    int n,i,a[1000],b[1000];

    scanf("%d",&n);
int indmax=0,max;
max=b[indmax]-a[indmax];
for(int i=0;i<n;i++)
{
    scanf("%d",&a[i]);
scanf("%d",&b[i]);
}
int d;
for(int i=0;i<n;i++)
{ d=b[i]-a[i];
if(d>=max)
{
indmax=i;
max=b[i]-a[i];
}
}

int nb=0;
for(i=0;i<n;i++)
if(a[indmax]<=a[i] && b[indmax]>=b[i])
nb++;

cout << nb-1;

return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int c,i,n,m,k,j;
pair<int,int>tab[1000000];
main(){
	cin>>n;
	for(i=0;i<n;i++){	scanf("%d",&tab[i].first);
scanf("%d",&tab[i].second);
}
	sort(tab,tab+n);
	for(i=0;i<n;i++){
		if(tab[i].second<c)k++;
else c=tab[i].second;
}
	cout<<k;
}


#include <iostream>
using namespace std;
int main()
{
    int n,i,a[1000],b[1000];

    scanf("%d",&n);
int indmax=0,max;
max=b[indmax]-a[indmax];
for(int i=0;i<n;i++)
{
    scanf("%d",&a[i]);
scanf("%d",&b[i]);

if((b[i]-a[i])>=max)
{
indmax=i;
max=b[i]-a[i];
}
}

int nb=0;
for(i=0;i<n;i++)
if(a[indmax]<=a[i] && b[indmax]>=b[i])
nb++;

cout << nb-1;

return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define RI(x) scanf("%d",&x)
#define RII(x,y) scanf("%d%d",&x,&y)
#define DRI(x) int x;RI(x)
#define DRII(x,y) int x,y;RII(x,y)
#define PI(x) printf("%d\n",x)
#define PIS(x) printf("%d ",x)
#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define min3(a,b,c) min(a, min(b,c))
#define max3(a,b,c) max(a, max(b,c))
#define sz(a) ((int)a.size())
#define clr(x,a) memset(x, a, sizeof x)
#define REP(i,n) for(int i=0; i<n; ++i)
#define rep(i,a,b) for(int i=a; i<b; ++i)
#define repp(i,a,b) for(int i=a ; i>=b ; --i)
const int MAX=10000010;
const double EPS = 1e-10;
const double PI = 2*asin(1.0);
const ll mod=1e9+7;
//inline int cmp(double x, double y = 0, double tol = EPS){return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;}

struct cmp
{
	bool operator ()(const pii& a,const pii& b)
	{
		if(b.F>=a.F && b.S<=a.S)return false;
		else
		{
			if(a.F==b.F)return a.S>b.S;

			return a.F<b.F;
		}
	}
};

int main()
{
	DRI(n);

	set <pii, cmp> pontos;

	REP(i, n)
	{
		DRII(a, b);

		pontos.insert(mp(a, b));
	}

	PI(n-sz(pontos));
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define RI(x) scanf("%d",&x)
#define RII(x,y) scanf("%d%d",&x,&y)
#define DRI(x) int x;RI(x)
#define DRII(x,y) int x,y;RII(x,y)
#define PI(x) printf("%d\n",x)
#define PIS(x) printf("%d ",x)
#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define min3(a,b,c) min(a, min(b,c))
#define max3(a,b,c) max(a, max(b,c))
#define sz(a) ((int)a.size())
#define clr(x,a) memset(x, a, sizeof x)
#define REP(i,n) for(int i=0; i<n; ++i)
#define rep(i,a,b) for(int i=a; i<b; ++i)
#define repp(i,a,b) for(int i=a ; i>=b ; --i)
const int MAX=10000010;
const double EPS = 1e-10;
const double PI = 2*asin(1.0);
const ll mod=1e9+7;
inline int cmp(double x, double y = 0, double tol = EPS){return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;}

struct dados
{
	int inicio, fim;
};

bool operator <(dados a, dados b)
{
	if(a.inicio==b.inicio) return a.fim>b.fim;

	return a.inicio<b.inicio;
}

int main()
{
	DRI(n);

	vector <dados> pontos;

	REP(i, n)
	{
		dados aux;

		RII(aux.inicio,aux.fim);

		pontos.pb(aux);
	}

	sort(all(pontos));
	multimap <int, int> intervalos;
	multimap <int, int>::iterator it;
	bool ins;
	int cont=0;

	REP(i, sz(pontos))
	{
		it=intervalos.begin();
		ins=false;

		while(it!=intervalos.end() && it->F<=pontos[i].inicio)
		{
			if(it->S>=pontos[i].fim)
			{
				ins=true;
				cont++;
				break;
			}
			it++;
		}

		if(!ins)intervalos.insert(mp(pontos[i].inicio, pontos[i].fim));
	}

	PI(cont);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define RI(x) scanf("%d",&x)
#define RII(x,y) scanf("%d%d",&x,&y)
#define DRI(x) int x;RI(x)
#define DRII(x,y) int x,y;RII(x,y)
#define PI(x) printf("%d\n",x)
#define PIS(x) printf("%d ",x)
#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define min3(a,b,c) min(a, min(b,c))
#define max3(a,b,c) max(a, max(b,c))
#define sz(a) ((int)a.size())
#define clr(x,a) memset(x, a, sizeof x)
#define REP(i,n) for(int i=0; i<n; ++i)
#define rep(i,a,b) for(int i=a; i<b; ++i)
#define repp(i,a,b) for(int i=a ; i>=b ; --i)
const int MAX=10000010;
const double EPS = 1e-10;
const double PI = 2*asin(1.0);
const ll mod=1e9+7;
inline int cmp(double x, double y = 0, double tol = EPS){return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;}

struct dados
{
	int inicio, fim;
};

bool operator <(dados a, dados b)
{
	if(a.inicio==b.inicio) return a.fim>b.fim;

	return a.inicio<b.inicio;
}

int main()
{
	DRI(n);

	vector <dados> pontos;

	REP(i, n)
	{
		dados aux;

		cin>>aux.inicio>>aux.fim;

		pontos.pb(aux);
	}

	sort(all(pontos));
	multimap <int, int> intervalos;
	multimap <int, int>::iterator it;
	bool ins;
	int cont=0;

	REP(i, sz(pontos))
	{
		it=intervalos.begin();
		ins=false;

		while(it!=intervalos.end() && it->F<=pontos[i].inicio)
		{
			if(it->S>=pontos[i].fim)
			{
				ins=true;
				cont++;
				break;
			}
		}

		if(!ins)intervalos.insert(mp(pontos[i].inicio, pontos[i].fim));
	}

	cout<<cont<<endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define RI(x) scanf("%d",&x)
#define RII(x,y) scanf("%d%d",&x,&y)
#define DRI(x) int x;RI(x)
#define DRII(x,y) int x,y;RII(x,y)
#define PI(x) printf("%d\n",x)
#define PIS(x) printf("%d ",x)
#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define min3(a,b,c) min(a, min(b,c))
#define max3(a,b,c) max(a, max(b,c))
#define sz(a) ((int)a.size())
#define clr(x,a) memset(x, a, sizeof x)
#define REP(i,n) for(int i=0; i<n; ++i)
#define rep(i,a,b) for(int i=a; i<b; ++i)
#define repp(i,a,b) for(int i=a ; i>=b ; --i)
const int MAX=10000010;
const double EPS = 1e-10;
const double PI = 2*asin(1.0);
const ll mod=1e9+7;
inline int cmp(double x, double y = 0, double tol = EPS){return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;}

struct dados
{
	int inicio, fim;
};

bool operator <(dados a, dados b)
{
	if(a.inicio==b.inicio) return a.fim>b.fim;

	return a.inicio<b.inicio;
}

int main()
{
	DRI(n);

	vector <dados> pontos;

	REP(i, n)
	{
		dados aux;

		cin>>aux.inicio>>aux.fim;

		pontos.pb(aux);
	}

	sort(all(pontos));
	multimap <int, int> intervalos;
	multimap <int, int>::iterator it;
	bool ins;
	int cont=0;

	REP(i, sz(pontos))
	{
		it=intervalos.begin();
		ins=false;

		while(it!=intervalos.end() && it->F<=pontos[i].inicio)
		{
			if(it->S>=pontos[i].fim)
			{
				ins=true;
				cont++;
				break;
			}
			it++;
		}

		if(!ins)intervalos.insert(mp(pontos[i].inicio, pontos[i].fim));
	}

	cout<<cont<<endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define RI(x) scanf("%d",&x)
#define RII(x,y) scanf("%d%d",&x,&y)
#define DRI(x) int x;RI(x)
#define DRII(x,y) int x,y;RII(x,y)
#define PI(x) printf("%d\n",x)
#define PIS(x) printf("%d ",x)
#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define min3(a,b,c) min(a, min(b,c))
#define max3(a,b,c) max(a, max(b,c))
#define sz(a) ((int)a.size())
#define clr(x,a) memset(x, a, sizeof x)
#define REP(i,n) for(int i=0; i<n; ++i)
#define rep(i,a,b) for(int i=a; i<b; ++i)
#define repp(i,a,b) for(int i=a ; i>=b ; --i)
const int MAX=10000010;
const double EPS = 1e-10;
const double PI = 2*asin(1.0);
const ll mod=1e9+7;
inline int cmp(double x, double y = 0, double tol = EPS){return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;}

struct dados
{
	int inicio, fim;
};

bool operator <(dados a, dados b)
{
	if(a.inicio==b.inicio)return a.fim>b.fim;

	return a.inicio<b.inicio;
}

int main()
{
	DRI(n);

	vector <dados> pontos;

	REP(i, n)
	{
		dados aux;

		RII(aux.inicio, aux.fim);

		pontos.pb(aux);
	}

	sort(all(pontos));
	int maior=pontos[0].fim;
	int cont=0;

	rep(i,1,n)
	{
		if(pontos[i].fim<=maior)cont++;
		else maior=pontos[i].fim;
	}

	PI(cont);
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
        pair <long long,long long> arr[100005];
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>arr[i].first>>arr[i].second;
        }
        sort(arr,arr+n);
        long long sum=0;
        long long finish=0;
        for(int i=0;i<n;i++)
        {
            if(arr[i].second>finish)
                finish=arr[i].second;

            else
                sum++;
        }
        cout<<sum;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

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

	sort(b.begin(), b.end());
	vector<long long int> val(n);
	for (int i = 0; i < n; ++i) {
		val[i] = (i + 1) * 1ll * (n - i) * a[i];
		//val[i].second = i;
	}
	sort(val.begin(), val.end(), greater<int> ());
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		ans = (ans + (val[i] % MOD * 1ll * b[i]) % MOD) % MOD;
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

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

	sort(b.begin(), b.end());
	vector<long long int> val(n);
	for (int i = 0; i < n; ++i) {
		val[i].first = (i + 1) * 1ll * (n - i) * a[i];
		//val[i].second = i;
	}
	sort(val.begin(), val.end(), greater<int> ());
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		ans = (ans + (val[i] % MOD * 1ll * b[i]) % MOD) % MOD;
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

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

	sort(b.begin(), b.end());
	vector<pair<long long, int>> val(n);
	for (int i = 0; i < n; ++i) {
		val[i].first = (i + 1) * 1ll * (n - i) * a[i];
		val[i].second = i;
	}
	sort(val.begin(), val.end(),greater<int>());
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		ans = (ans + (val[i].first % MOD * 1ll * b[i]) % MOD) % MOD;
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

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

	sort(b.begin(), b.end());
	vector<pair<long long, int>> val(n);
	for (int i = 0; i < n; ++i) {
		val[i].first = (i + 1) * 1ll * (n - i) * a[i];
		val[i].second = i;
	}
	sort(val.rbegin(), val.rend());
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		ans = (ans + (val[i].first % MOD * 1ll * b[i]) % MOD) % MOD;
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool func(long long int a, long long int b)
{
    return a>b;
}

const int MOD = 998244353;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

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

	//sort(b.begin(), b.end(),greater<int> ());
	sort(b.begin(),b.end());
	vector<long long int> val(n);
	for (int i = 0; i < n; ++i) {
		val[i] = (i + 1) * 1ll * (n - i) * a[i];
		//val[i].second = i;
	}
	//sort(val.begin(), val.end(), greater<int> ());
	sort(val.begin(), val.end(), func;
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		ans = (ans + (val[i] % MOD * 1ll * b[i]) % MOD) % MOD;
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244353;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

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

	//sort(b.begin(), b.end(),greater<int> ());
	sort(b.begin(),b.end());
	vector<long long int> val(n);
	for (int i = 0; i < n; ++i) {
		val[i] = (i + 1) * 1ll * (n - i) * a[i];
		//val[i].second = i;
	}
	//sort(val.begin(), val.end(), greater<int> ());
	sort(val.begin(), val.end(), greater<int> ());
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		ans = (ans + (val[i] % MOD * 1ll * b[i]) % MOD) % MOD;
	}
	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
bool func(long long int a, long long int b)
{
    return a>b;
}

const int MOD = 998244353;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
#endif

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

	//sort(b.begin(), b.end(),greater<int> ());
	sort(b.begin(),b.end());
	vector<long long int> val(n);
	for (int i = 0; i < n; ++i) {
		val[i] = (i + 1) * 1ll * (n - i) * a[i];
		//val[i].second = i;
	}
	//sort(val.begin(), val.end(), greater<int> ());
	sort(val.begin(), val.end(), func);
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		ans = (ans + (val[i] % MOD * 1ll * b[i]) % MOD) % MOD;
	}
	cout << ans << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.14159265

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    else
        return x * power(x, y / 2) * power(x, y / 2);
}

int32_t main()
{
	int q=1;
//	cin>>q;
	int Q=1;

	while(q--)
	{

	   int n,m;
	   m=998244353 ;
	   cin>>n;

	   int a[n],b[n];

	   vector<pair<int,int> >v;

	   for(int i=0; i<n; i++)
	   {
	   	cin>>a[i];
	   	v.push_back({a[i]*(i+1)*(n-i),i});

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

	   sort(v.begin(),v.end());

	   int d[n];

	   sort(b,b+n);
	   reverse(b,b+n);

	   for(int i=0; i<n; i++)
	   d[v[i].second]=b[i];

	   int s=0;

	   for(int i=0; i<n; i++)
{
	   s=(s%m+(v[i].first%m*d[i]%m)%m)%m ;
	   j++;
}

	   cout<<s%m;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.14159265

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    else
        return x * power(x, y / 2) * power(x, y / 2);
}

int32_t main()
{
	int q=1;
//	cin>>q;
	int Q=1;

	while(q--)
	{

	   int n,m;
	   m=998244353 ;
	   cin>>n;

	   int a[n],b[n];

	   vector<pair<int,int> >v;

	   for(int i=0; i<n; i++)
	   {
	   	cin>>a[i];
	   	v.push_back({a[i],i});

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

	   sort(v.begin(),v.end());

	   int d[n];

	   sort(b,b+n);
	   reverse(b,b+n);

	   for(int i=0; i<n; i++)
	   d[v[i].second]=b[i];

	   int s=0;
	   int j=1;
	   for(int i=n-1; i>=0; i--)
{
	   s=(s%m+(a[i]%m*d[i]%m*(i+1)%m*j%m)%m)%m;
	   j++;
}

	   cout<<s%m;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.14159265

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    else
        return x * power(x, y / 2) * power(x, y / 2);
}

int32_t main()
{
	int q=1;
//	cin>>q;
	int Q=1;

	while(q--)
	{

	   int n,m;
	   m=998244353 ;
	   cin>>n;

	   int a[n],b[n];

	   vector<pair<int,int> >v;

	   for(int i=0; i<n; i++)
	   {
	   	cin>>a[i];
	   	v.push_back({a[i]*(i+1)*(n-i),i});

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

	   sort(v.begin(),v.end());

	   int d[n];

	   sort(b,b+n);
	   reverse(b,b+n);

	   for(int i=0; i<n; i++)
	   d[v[i].second]=b[i];

	   int s=0;

	   for(int i=0; i<n; i++)
{
	   s=(s%m+(v[i].first%m*d[i]%m)%m)%m ;

}

	   cout<<s%m;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.14159265

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    else
        return x * power(x, y / 2) * power(x, y / 2);
}

int32_t main()
{
	int q=1;
//	cin>>q;
	int Q=1;

	while(q--)
	{

	   int n,m;
	   m=998244353 ;
	   cin>>n;

	   int a[n],b[n];

	   vector<pair<int,int> >v;

	   for(int i=0; i<n; i++)
	   {
	   	cin>>a[i];
	   	v.push_back({a[i],i});
	   	a[i]=a[i]*(i+1)*(n-i);

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

	   sort(v.begin(),v.end());

	   int d[n];

	   sort(b,b+n);
	   reverse(b,b+n);

	   for(int i=0; i<n; i++)
	   d[v[i].second]=b[i];

	   int s=0;

	   for(int i=0; i<n; i++)
{
	   s=(s%m+(a[i]%m*d[i]%m)%m)%m ;

}

	   cout<<s%m;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.14159265

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    else
        return x * power(x, y / 2) * power(x, y / 2);
}

int32_t main()
{
	int q=1;
//	cin>>q;
	int Q=1;

	while(q--)
	{

	   int n,m;
	   m=998244353 ;
	   cin>>n;

	   int a[n],b[n];

	   vector<pair<int,int> >v;

	   for(int i=0; i<n; i++)
	   {
	   	cin>>a[i];
	   	v.push_back({a[i],i});

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

	   sort(v.begin(),v.end());

	   int d[n];

	   sort(b,b+n);
	   reverse(b,b+n);

	   for(int i=0; i<n; i++)
	   d[v[i].second]=b[i];

	   int s=0;
	   int j=1;
	   for(int i=0; i<n; i++)
{
	   s=(s%m+(a[i]*d[i]*(n-i)*j)%m)%m;
	   j++;
}

	   cout<<s%m;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.14159265

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    else
        return x * power(x, y / 2) * power(x, y / 2);
}

int32_t main()
{
	int q=1;
//	cin>>q;
	int Q=1;

	while(q--)
	{

	   int n,m;
	   m=998244353 ;
	   cin>>n;

	   int a[n],b[n];

	   vector<pair<int,int> >v;

	   for(int i=0; i<n; i++)
	   {
	   	cin>>a[i];
	   	v.push_back({a[i],i});

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

	   sort(v.begin(),v.end());

	   int d[n];

	   sort(b,b+n);
	   reverse(b,b+n);

	   for(int i=0; i<n; i++)
	   d[v[i].second]=b[i];

	   int j=1;

	   for(int i=0; i<n; i++)
	   {
	   	a[i]=a[i]*j*(n-i);
	   	j++;
	   }

	   int s=0;

	   for(int i=0; i<n; i++)
{
	   s=(s%m+(a[i]%m*d[i]%m)%m)%m ;
	   j++;
}

	   cout<<s%m;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.14159265

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    else
        return x * power(x, y / 2) * power(x, y / 2);
}

int32_t main()
{
	int q=1;
//	cin>>q;
	int Q=1;

	while(q--)
	{

	   int n,m;
	   m=998244353 ;
	   cin>>n;

	   int a[n],b[n];

	   vector<pair<int,int> >v;

	   for(int i=0; i<n; i++)
	   {
	   	cin>>a[i];
	   	v.push_back({a[i],i});

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

	   sort(v.begin(),v.end());

	   int d[n];

	   sort(b,b+n);
	   reverse(b,b+n);

	   for(int i=0; i<n; i++)
	   d[v[i].second]=b[i];

	   int s=0;
	   int j=1;
	   for(int i=0; i<n; i++)
{
	   s=(s%m+(a[i]%m*d[i]%m*(n-i)%m*j%m)%m)%m;
	   j++;
}

	   cout<<s%m;

	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PI 3.14159265

int power(int x, unsigned int y)
{
    if (y == 0)
        return 1;
    else if (y % 2 == 0)
        return power(x, y / 2) * power(x, y / 2);
    else
        return x * power(x, y / 2) * power(x, y / 2);
}

int32_t main()
{
	int q=1;
//	cin>>q;
	int Q=1;

	while(q--)
	{

	   int n,m;
	   m=998244353 ;
	   cin>>n;

	   int a[n],b[n];

	   vector<pair<int,int> >v;

	   for(int i=0; i<n; i++)
	   {
	   	cin>>a[i];
	   	v.push_back({a[i],i});

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

	   sort(v.begin(),v.end());

	   int d[n];

	   sort(b,b+n);
	   reverse(b,b+n);

	   for(int i=0; i<n; i++)
	   d[v[i].second]=b[i];

	   int j=1;

	   for(int i=0; i<n; i++)
	   a[i]=a[i]*j*(n-i);

	   int s=0;

	   for(int i=0; i<n; i++)
{
	   s=(s%m+(a[i]%m*d[i]%m)%m)%m ;
	   j++;
}

	   cout<<s%m;

	}
}


#include <bits/stdc++.h>
#define lli long long int
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main() {
    lli x=998244353;
    lli n;
    cin>>n;
    lli a[n];
    lli b[n];
    rep(i,n)
    cin>>a[i];
    rep(i,n)
    cin>>b[i];
    lli val[n];
    rep(i,n)
    {
        val[i]=a[i]*(i+1)*(n-i);
    }
    sort(val,val+n,greater<int>());
    sort(b,b+n);
    lli ans=0;
    rep(i,n)
    {
        ans+=((b[i])*(val[i]%x))%x;
        ans%=x;
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define lli long long int
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main() {
    lli x=998244353;
    lli n;
    cin>>n;
    lli a[n];
    lli b[n];
    rep(i,n)
    cin>>a[i];
    rep(i,n)
    cin>>b[i];
    lli val[n];
     sort(b,b+n);
    rep(i,n)
    {
        val[i]=a[i]*(i+1)*(n-i);
    }
    sort(val,val+n,greater<int>());

    lli ans=0;
    rep(i,n)
    {
        ans=(ans+((val[i]%x)*(b[i]))%x)%mod;
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define lli long long int
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main() {
    lli x=998244353;
    lli n;
    cin>>n;
    lli a[n];
    lli b[n];
    rep(i,n)
    cin>>a[i];
    rep(i,n)
    cin>>b[i];
    lli val[n];
    rep(i,n)
    {
        val[i]=a[i]*(i+1)*(n-(i+1)+1);
    }
    sort(val,val+n);
    sort(b,b+n,greater<int>());
    lli ans=0;
    rep(i,n)
    {
        ans+=((b[i]%x)*(val[i]%x))%x;
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define lli long long int
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main() {
    lli x=998244353;
    lli n;
    cin>>n;
    lli a[n];
    lli b[n];
    rep(i,n)
    cin>>a[i];
    rep(i,n)
    cin>>b[i];
    lli val[n];
     sort(b,b+n);
    rep(i,n)
    {
        val[i]=a[i]*(i+1)*(n-i);
    }
    sort(val,val+n,greater<int>());

    lli ans=0;
    rep(i,n)
    {
        ans=(ans+((val[i]%x)*b[i])%x)%x;
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define lli long long int
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main() {
    lli x=998244353;
    lli n;
    cin>>n;
    lli a[n];
    lli b[n];
    rep(i,n)
    cin>>a[i];
    rep(i,n)
    cin>>b[i];
    lli val[n];
    rep(i,n)
    {
        val[i]=a[i]*(i+1)*(n-i);
    }
    sort(val,val+n,greater<int>());
    sort(b,b+n);
    lli ans=0;
    rep(i,n)
    {
        ans+=((b[i]%x)*(val[i]%x))%x;
        ans%=x;
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define lli long long int
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main() {
    lli x=998244353;
    lli n;
    cin>>n;
    lli a[n];
    lli b[n];
    rep(i,n)
    cin>>a[i];
    rep(i,n)
    cin>>b[i];
    lli val[n];
    rep(i,n)
    {
        val[i]=a[i]*(i+1)*(n-i);
    }
    sort(val,val+n,greater<int>());
    sort(b,b+n);
    lli ans=0;
    rep(i,n)
    {
        ans=(ans+(b[i]*val[i])%x)%x;
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define lli long long int
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main() {
    int x=998244353;
    int n;
    cin>>n;
    int a[n];
    int b[n];
    rep(i,n)
    cin>>a[i];
    rep(i,n)
    cin>>b[i];
    int val[n];
    rep(i,n)
    {
        val[i]=a[i]*(i+1)*(n-(i+1)+1);
    }
    sort(val,val+n);
    sort(b,b+n,greater<int>());
    lli ans=0;
    rep(i,n)
    {
        ans+=((b[i]%x)*(val[i]%x))%x;
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define lli long long int
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main() {
    lli x=998244353;
    lli n;
    cin>>n;
    lli a[n];
    lli b[n];
    rep(i,n)
    {
        cin>>a[i];
        a[i]=a[i]*(i+1)*(n-i);
    }
    rep(i,n)
    cin>>b[i];

    sort(a,a+n);
    sort(b,b+n,greater<int>());
    lli ans=0;
    rep(i,n)
    {
        ans+=((b[i]%x)*(a[i]%x));
        ans%=x;
    }
    cout<<ans<<endl;
}


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

#define MOD 998244353
#define ll long long int
#define F(a,n) for(int i=0;i<n;i++){cin>>a[i];}

bool compare(ll a,ll b){
	return a>b;
}

int main(){

	int n;
	cin>>n;
	ll *a = new ll[n];
	ll *b = new ll[n];

	F(a,n);
	F(b,n);

	for(int i=0;i<n;i++){
		a[i] = a[i]*(i+1)*(n-i);
	}

	sort(a,a+n);
	sort(b,b+n,compare);

	ll sum = 0;

	for(int i=0;i<n;i++){
		sum += (a[i]%MOD)*(b[i]%MOD);
		sum%=MOD;
	}
	cout<<sum<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define M 998244353
#define ll long long int
#define F(a,n) for(int i=0;i<n;i++){cin>>a[i];}
bool compare(ll a,ll b)
{
	return a>b;
}
 int main()
{
	int n;cin>>n;
	ll *a = new ll[n];
	ll *b = new ll[n];
	F(a,n);
	F(b,n);
	for(int i=0;i<n;i++)
	{
		a[i]=(i+1)*(n-i)*a[i];//Count the occurences of a number
	}
	sort(a,a+n);
	sort(b,b+n,compare);
	ll sum=0;
	for(int i=0;i<n;i++)
	{
		sum+=(a[i]%M)*(b[i]%M);
		sum%=M;
	}
cout<<sum<<endl;
return 0;

}


#include<bits/stdc++.h>
#define ll long long int
#define M 998244353
#define F(a,n) for(int i=0;i<n;i++){cin>>a[i];}
using namespace std;
bool compare(ll a,ll b)
{
	return a>b;
}
 int main()
{
	int n;
	ll *a = new ll[n];
	ll *b = new ll[n];
	F(a,n);
	F(b,n);
	for(int i=0;i<n;i++)
	{
		a[i]=(i+1)*(n-i)*a[i];//Count the occurences of a number
	}
	sort(a,a+n);
	sort(b,b+n,compare);
	ll sum=0;
	for(int i=0;i<n;i++)
	{
		sum+=(a[i]%M)*(b[i]%M);
		sum%=M;
	}
cout<<sum<<endl;
return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define M 998244353
#define ll long long int
#define F(a,n) for(int i=0;i<n;i++){cin>>a[i];}
bool compare(ll a,ll b)
{
	return a>b;
}
 int main()
{
	int n;cin>>n;
	ll *a = new ll[n+1];
	ll *b = new ll[n+1];
	F(a,n);
	F(b,n);
	for(int i=0;i<n;i++)
	{
		a[i]=a[i]*(i+1)*(n-i);//Count the occurences of a number
	}
	sort(a,a+n);
	sort(b,b+n,compare);
	ll sum=0;
	for(int i=0;i<n;i++)
	{
		sum+=(a[i]%M)*(b[i]%M);
		sum%=M;
	}
cout<<sum<<endl;
return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define M 998244353
#define ll long long int
#define F(a,n) for(int i=0;i<n;i++){cin>>a[i];}
bool compare(ll a,ll b)
{
	return a>b;
}
 int main()
{
	int n;cin>>n;
	ll *a = new ll[n];
	ll *b = new ll[n];
	F(a,n);
	F(b,n);
	for(int i=0;i<n;i++)
	{
		a[i]=a[i]*(i+1)*(n-i);//Count the occurences of a number
	}
	sort(a,a+n);
	sort(b,b+n,compare);
	ll sum=0;
	for(int i=0;i<n;i++)
	{
		sum+=(a[i]%M)*(b[i]%M);
		sum%=M;
	}
cout<<sum<<endl;
return 0;

}


// Problem link :
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <cmath>
#include <functional>
#include <deque>
#include <bitset>
#include <climits>
#include <cstdio>
#include <list>
#include <iomanip>
using namespace std;

#define ll long long int
#define mp make_pair
#define pb push_back
#define fi first
#define si second
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define F(a,n) for(int i=0;i<n;i++){cin>>a[i];}
#define F1(a,n) for(int i=1;i<=n;i++){cin>>a[i];}
#define P(a,n) for(int i=0;i<n;i++){cout<<a[i]<<' ';}cout<<endl;
#define P1(a,n) for(int i=1;i<=n;i++){cout<<a[i]<<' ';}cout<<endl;
#define NF(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>a[i][j];}}
#define NF1(a,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cin>>a[i][j];}}
#define PNF(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++){cout<<a[i][j]<<' ';}cout<<endl;}cout<<endl;
#define PNF1(a,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cout<<a[i][j]<<' ';}cout<<endl;}cout<<endl;
#define AS 200001
#define mod 998244353
int main()
{
fastIO
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll n;
cin>>n;
ll *a = new ll[n];
ll *b = new ll[n];
ll *c = new ll[n];
F(a,n);
F(b,n);
for(ll i=0;i<n;i++)
{
	a[i]=a[i]*(i+1)*(n-i);
}
sort(a,a+n);//ascending order
sort(b,b+n,[&](ll a,ll b){return a>b;});

ll sum = 0;
//we can't do it this way because we can't change. Therfore we need occurence of every number at its original position

// for(ll i=0;i<n;i++)
// {
// sum+=(i+1)*(n-i)*a[i]*b[i];
// }
for(ll i=0;i<n;i++)
{
	sum+=(a[i]%mod)*(b[i]%mod);

}
cout<<sum%mod;
return 0;
}


// Problem link :
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <cmath>
#include <functional>
#include <deque>
#include <bitset>
#include <climits>
#include <cstdio>
#include <list>
#include <iomanip>
using namespace std;

#define ll long long int
#define mp make_pair
#define pb push_back
#define fi first
#define si second
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define F(a,n) for(int i=0;i<n;i++){cin>>a[i];}
#define F1(a,n) for(int i=1;i<=n;i++){cin>>a[i];}
#define P(a,n) for(int i=0;i<n;i++){cout<<a[i]<<' ';}cout<<endl;
#define P1(a,n) for(int i=1;i<=n;i++){cout<<a[i]<<' ';}cout<<endl;
#define NF(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>a[i][j];}}
#define NF1(a,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cin>>a[i][j];}}
#define PNF(a,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++){cout<<a[i][j]<<' ';}cout<<endl;}cout<<endl;
#define PNF1(a,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cout<<a[i][j]<<' ';}cout<<endl;}cout<<endl;
#define AS 200001
#define mod 998244353
int main()
{
fastIO
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll n;
cin>>n;
ll *a = new ll[n];
ll *b = new ll[n];
ll *c = new ll[n];
F(a,n);
F(b,n);
for(ll i=0;i<n;i++)
{
	a[i]=a[i]*(i+1)*(n-i);
}
sort(a,a+n);//ascending order
sort(b,b+n,[&](ll a,ll b){return a>b;});

ll sum = 0;
//we can't do it this way because we can't change. Therfore we need occurence of every number at its original position

// for(ll i=0;i<n;i++)
// {
// sum+=(i+1)*(n-i)*a[i]*b[i];
// }
for(ll i=0;i<n;i++)
{
	sum+=(a[i]%mod)*(b[i]%mod);
	sum%=mod;
}
cout<<sum%mod;
return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define M 998244353
#define F(a,n) for(int i=0;i<n;i++){cin>>a[i];}
using namespace std;
bool compare(ll a,ll b)
{
	return a>b;
}
 int main()
{
	int n;cin>>n;
	ll *a = new ll[n];
	ll *b = new ll[n];
	F(a,n);
	F(b,n);
	for(int i=0;i<n;i++)
	{
		a[i]=(i+1)*(n-i)*a[i];//Count the occurences of a number
	}
	sort(a,a+n);
	sort(b,b+n,compare);
	ll sum=0;
	for(int i=0;i<n;i++)
	{
		sum+=(a[i]%M)*(b[i]%M);
		sum%=M;
	}
cout<<sum<<endl;
return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

constexpr ll mod =  998244353;

int main(){
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  vector<pair<ll,int>> d(n);
  rep(i,n) cin >> a[i];
  rep(i,n) cin >> b[i];

  for (int i = 0; i < n; i++) {
    ll r = (n - i);
    ll l = (i + 1);
    d[i].first = a[i] * l * r;
    d[i].second = i;
  }
  sort(d.begin(), d.end());
  sort(b.begin(), b.end(), greater<ll>());
  ll res = 0;
  for (int i = 0; i < n; i++) {
    (res += d[i].first % mod * b[i] % mod) %= mod;
  }
  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

constexpr ll mod =  998244353;

int main(){
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  vector<pair<ll,int>> d(n);
  rep(i,n) cin >> a[i];
  rep(i,n) cin >> b[i];

  for (int i = 0; i < n; i++) {
    ll r = (n - i);
    ll l = (i + 1);
    d[i].first = a[i] * l * r;
    d[i].second = i;
  }
  sort(d.begin(), d.end());
  sort(b.begin(), b.end(), greater<ll>());
  ll res = 0;
  for (int i = 0; i < n; i++) {
    (res += d[i].first % mod * b[i] % mod) % mod;
  }
  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

constexpr ll mod =  998244353;

int main(){
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  vector<pair<ll,int>> d(n);
  rep(i,n) cin >> a[i];
  rep(i,n) cin >> b[i];

  for (int i = 0; i < n; i++) {
    ll r = (n - i);
    ll l = (ll)(n - i) * i;
    d[i].first = a[i] * (l + r);
    d[i].second = i;
  }
  sort(d.begin(), d.end());
  sort(b.begin(), b.end(), greater<ll>());
  ll res = 0;
  for (int i = 0; i < n; i++) {
    (res += d[i].first % mod * b[i] % mod) %= mod;
  }
  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

constexpr ll mod =  998244353;

int main(){
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  vector<P> idx(n);
  rep(i,n) cin >> a[i], idx[i].first = a[i], idx[i].second = i;
  rep(i,n) cin >> b[i];
  vector<ll> tmp(n);
  sort(idx.begin(), idx.end());
  sort(b.begin(), b.end(), greater<ll>());
  for (int i = 0; i < n; i++) {
    tmp[idx[i].second] = b[i];
  }
  vector<ll> d(n);
  for (int i = 0; i < n; i++) d[i] = (a[i] * tmp[i]) % mod;
  ll res = 0;

  for (int i = 0; i < n; i++) {
    ll l = (n - i) * i;
    ll r = (n - i);
    (res += d[i] * (l + r) % mod) %= mod;
  }
  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

constexpr ll mod =  998244353;

int main(){
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  vector<pair<ll,int>> d(n);
  rep(i,n) cin >> a[i];
  rep(i,n) cin >> b[i];

  for (int i = 0; i < n; i++) {
    ll r = (n - i);
    ll l = (n - i) * i;
    d[i].first = a[i] * (l + r);
    d[i].second = i;
  }
  sort(d.begin(), d.end());
  sort(b.begin(), b.end(), greater<ll>());
  ll res = 0;
  for (int i = 0; i < n; i++) {
    (res += d[i].first * b[i] % mod) % mod;
  }
  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

constexpr ll mod =  998244353;

int main(){
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  vector<pair<ll,int>> d(n);
  rep(i,n) cin >> a[i];
  rep(i,n) cin >> b[i];

  for (int i = 0; i < n; i++) {
    ll r = (n - i);
    ll l = (n - i) * i;
    d[i].first = a[i] * (l + r);
    d[i].second = i;
  }
  sort(d.begin(), d.end());
  sort(b.begin(), b.end(), greater<ll>());
  ll res = 0;
  for (int i = 0; i < n; i++) {
    (res += d[i].first % mod * b[i] % mod) %= mod;
  }
  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

constexpr ll mod =  998244353;

int main(){
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  vector<P> idx(n);
  rep(i,n) cin >> a[i], idx[i].first = a[i], idx[i].second = i;
  rep(i,n) cin >> b[i];
  vector<ll> tmp(n);
  sort(idx.begin(), idx.end());
  sort(b.begin(), b.end(), greater<ll>());
  for (int i = 0; i < n; i++) {
    tmp[idx[i].second] = b[i];
  }
  vector<ll> d(n);
  for (int i = 0; i < n; i++) d[i] = (a[i] * tmp[i]) % mod;
  ll res = 0;

  for (int i = 0; i < n; i++) {
    ll l = (ll)(n - i) * i;
    ll r = (n - i);
    (res += d[i] * (l + r) % mod) %= mod;
  }
  cout << res << endl;
  return 0;
}


/*input
5
1 8 7 2 4
9 7 2 9 3

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 998244353;
#define PI 3.14159265
#define endl '\n'
#define debug(x) cout << #x << " = " << x << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
    int x = 1 % mod;
    while(b){
        if(b & 1) x = mul(x, a);
        a = mul(a, a);
        b >>= 1;
    }
    return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

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

  int n;
  cin >> n;
  ll v;
  vector <ll> vec1, vec2;
  ll time = 0;
  for (int i = 0; i < n; i++) {
    cin >> v;
    time = ((i + 1) * (n - i));
    vec1.pb(v * time);
  }
  for (int i = 0; i < n; i++) {
    cin >> v;
    vec2.pb(v);
  }
  sort(all(vec1));
  sort(allr(vec2));
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans = ((ans % mod) + ((vec1[i] % mod) * 1ll * (vec2[i] % mod)) % mod) % mod;
    if (ans >= mod) {
      ans %= mod;
    }
    if (ans < 0) {
      ans += mod;
    }
    ans %= mod;
  }
  ans %= mod;
  if (ans < 0) {
    ans += mod;
  }
  cout << ans % mod << endl;
  return 0;
}


/*input
5
1 8 7 2 4
9 7 2 9 3

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 998244353;
#define PI 3.14159265
#define endl '\n'
#define debug(x) cout << #x << " = " << x << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
    int x = 1 % mod;
    while(b){
        if(b & 1) x = mul(x, a);
        a = mul(a, a);
        b >>= 1;
    }
    return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

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

  int n;
  cin >> n;
  ll v;
  vector <ll> vec1, vec2;
  ll time = 0;
  for (int i = 0; i < n; i++) {
    cin >> v;
    time = ((i + 1) * 1ll * (n - i));
    vec1.pb(v * time);
  }
  for (int i = 0; i < n; i++) {
    cin >> v;
    vec2.pb(v);
  }
  sort(all(vec1));
  sort(allr(vec2));
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans = add(ans, mul(vec1[i], vec2[i]));
    if (ans < 0) ans += mod;
    ans %= mod;
  }
  if (ans < 0) ans += mod;
  ans %= mod;
  cout << ans << endl;
  return 0;
}


/*input
5
1 8 7 2 4
9 7 2 9 3

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 998244353;
#define PI 3.14159265
#define endl '\n'
#define debug(x) cout << #x << " = " << x << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
    int x = 1 % mod;
    while(b){
        if(b & 1) x = mul(x, a);
        a = mul(a, a);
        b >>= 1;
    }
    return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

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

  int n;
  cin >> n;
  ll v;
  vector <ll> vec1, vec2;
  ll time = 0;
  for (int i = 0; i < n; i++) {
    cin >> v;
    time = ((i + 1) * (n - i));
    vec1.pb(v * time);
  }
  for (int i = 0; i < n; i++) {
    cin >> v;
    vec2.pb(v);
  }
  sort(all(vec1));
  sort(allr(vec2));
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans = add(ans, mul(vec1[i], vec2[i]));
  }
  cout << ans << endl;
  return 0;
}


/*input
5
1 8 7 2 4
9 7 2 9 3

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 998244353;
#define PI 3.14159265
#define endl '\n'
#define debug(x) cout << #x << " = " << x << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
    int x = 1 % mod;
    while(b){
        if(b & 1) x = mul(x, a);
        a = mul(a, a);
        b >>= 1;
    }
    return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

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

  int n;
  cin >> n;
  ll v;
  vector <ll> vec1, vec2;
  ll time = 0;
  for (int i = 0; i < n; i++) {
    cin >> v;
    time = ((i + 1) * (n - i));
    vec1.pb(v * time);
  }
  for (int i = 0; i < n; i++) {
    cin >> v;
    vec2.pb(v);
  }
  sort(all(vec1));
  sort(allr(vec2));
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans = add(ans, mul(vec1[i], vec2[i]));
  }
  ans %= mod;
  cout << ans << endl;
  return 0;
}


/*input
5
1 8 7 2 4
9 7 2 9 3

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 998244353;
#define PI 3.14159265
#define endl '\n'
#define debug(x) cout << #x << " = " << x << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
    int x = 1 % mod;
    while(b){
        if(b & 1) x = mul(x, a);
        a = mul(a, a);
        b >>= 1;
    }
    return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

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

  int n;
  cin >> n;
  ll v;
  vector <ll> vec1, vec2;
  ll time = 0;
  for (int i = 0; i < n; i++) {
    cin >> v;
    time = ((i + 1) * (n - i));
    vec1.pb((v * time));
  }
  for (int i = 0; i < n; i++) {
    cin >> v;
    vec2.pb(v);
  }
  sort(all(vec1));
  sort(allr(vec2));
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    vec1[i] %= mod;
    vec2[i] %= mod;
    if (vec1[i] < 0) vec1[i] += mod;
    if (vec2[i] < 0) vec2[i] += mod;
    ll t = ((vec1[i] % mod) * 1ll * (vec2[i] % mod)) % mod;
    ans = ans + t;
    if (ans >= mod) {
      ans %= mod;
    }
    if (ans < 0) {
      ans += mod;
    }
    ans %= mod;
  }
  ans %= mod;
  if (ans < 0) {
    ans += mod;
  }
  cout << ans % mod << endl;
  return 0;
}


/*input
5
1 8 7 2 4
9 7 2 9 3

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 998244353;
#define PI 3.14159265
#define endl '\n'
#define debug(x) cout << #x << " = " << x << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
    int x = 1 % mod;
    while(b){
        if(b & 1) x = mul(x, a);
        a = mul(a, a);
        b >>= 1;
    }
    return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

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

  int n;
  cin >> n;
  int v;
  vector <int> vec1, vec2;
  int time = 0;
  for (int i = 0; i < n; i++) {
    cin >> v;
    time = ((i + 1) * (n - i));
    time = mul(i + 1, n - i);
    vec1.pb(mul(v, time));
  }
  for (int i = 0; i < n; i++) {
    cin >> v;
    vec2.pb(v);
  }
  sort(all(vec1));
  sort(allr(vec2));
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = add(ans, mul(vec1[i], vec2[i]));
  }
  cout << ans << endl;
  return 0;
}


/*input
5
1 8 7 2 4
9 7 2 9 3

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
ll mod = 998244353;
#define PI 3.14159265
#define endl '\n'
#define debug(x) cout << #x << " = " << x << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
    int x = 1 % mod;
    while(b){
        if(b & 1) x = mul(x, a);
        a = mul(a, a);
        b >>= 1;
    }
    return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

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

  int n;
  cin >> n;
  ll v;
  vector <ll> vec1, vec2;
  ll time = 0;
  for (int i = 0; i < n; i++) {
    cin >> v;
    time = ((i + 1) * 1ll * (n - i));
    //debug(time);
    vec1.pb((v * time));
  }
  for (int i = 0; i < n; i++) {
    cin >> v;
    vec2.pb(v);
  }
  sort(all(vec1));
  sort(allr(vec2));
  // for (auto it : vec1) {
  //   cout << it << " ";
  // }
  // cout << endl;
  // for (auto it : vec2) {
  //   cout << it << " ";
  // }
  // cout << endl;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    vec1[i] %= mod;
    vec2[i] %= mod;
    if (vec1[i] < 0) vec1[i] += mod;
    if (vec2[i] < 0) vec2[i] += mod;
    ll t = ((vec1[i] % mod) * 1ll * (vec2[i] % mod)) % mod;
    if (t < 0) {
      t += mod;
    }
    t %= mod;
    ans = ans + t;
    if (ans >= mod) {
      ans %= mod;
    }
    if (ans < 0) {
      ans += mod;
    }
    ans %= mod;
  }
  ans %= mod;
  if (ans < 0) {
    ans += mod;
  }
  cout << ans % mod << endl;
  return 0;
}


/*input
5
1 8 7 2 4
9 7 2 9 3

*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll , ll> PLL;
typedef long double ld;

#define pb push_back
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
int mod = 998244353;
#define PI 3.14159265
#define endl '\n'
#define debug(x) cout << #x << " = " << x << endl;

template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(int x, int y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(int x, int y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % mod;}
inline int powr(int a, ll b){
    int x = 1 % mod;
    while(b){
        if(b & 1) x = mul(x, a);
        a = mul(a, a);
        b >>= 1;
    }
    return x;
}
inline int inv(int a){ return powr(a, mod - 2);}

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

  int n;
  cin >> n;
  ll v;
  vector <ll> vec1, vec2;
  ll time = 0;
  for (int i = 0; i < n; i++) {
    cin >> v;
    time = ((i + 1) * (n - i));
    vec1.pb(v * time);
  }
  for (int i = 0; i < n; i++) {
    cin >> v;
    vec2.pb(v);
  }
  sort(all(vec1));
  sort(allr(vec2));
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans = (ans % mod + ((vec1[i] % mod) * (vec2[i] % mod)) % mod) % mod;
    if (ans >= mod) {
      ans %= mod;
    }
    if (ans < 0) {
      ans += mod;
    }
    ans %= mod;
  }
  ans %= mod;
  cout << ans << endl;
  return 0;
}