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

const long long N = 2e5 + 5;
long long n ;
typedef pair<long long, long long> PII;
PII f[N];
vector <long long> alls;
long long tr[N];

long long lowbit(long long x)
{
	return x & -x;
}

void add(long long x, long long c)
{
	for(long long i = x; i <= n; i += lowbit(i))
		tr[i] += c;
}

long long ask(long long x)
{
	long long res = 0;
	for(long long i = x; i; i -= lowbit(i))
		res += tr[i];
	return res;
}

bool cmp (PII a,PII b) {
	if(a.first == b.first) return a.second > b.second;
	else return a.first < b.first;
}

long long find (long long x) {
	long long l = 0,r = alls.size()-1;
	while(l < r){
		long long mid = l + r >> 1;
		if(alls[mid] >= x) r = mid;
		else l = mid + 1; 
	}
	return l+1;
}

int main(){
//	ios_base::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);
	cin>>n;
	for (long long i = 1 ; i <= n ; i ++)
		cin>>f[i].first;
	for (long long i = 1 ; i <= n ; i ++)
		cin>>f[i].second;
	sort(f+1 ,f+n+1 ,cmp);
	for(long long i=1;i<=n;i++){
		alls.push_back(f[i].second);
	}
	sort(alls.begin() ,alls.end());
	alls.erase(unique(alls.begin(),alls.end()),alls.end());
	long long ans=0;
	for(long long i = 1;i <= n;i ++){
		long long cnt = 1;
		while(i<n && f[i].first == f[i + 1].first && f[i].second == f[i + 1].second){
			cnt ++;
			i ++;
		}
		long long idx = find(f[i].second);
		long long r = alls.size();
		long long big = ask(r) - ask(idx - 1);
		ans+=1ll*big*cnt+cnt*cnt*1ll;
		add(idx,cnt);
	}
	cout<<ans;
	return 0;
}
```language