#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e5 + 5;
const ll mod = 1e9;
ll n, X[NMAX], Y[NMAX], a[NMAX], w[NMAX];
vector<pair<ll, ll>> v;
vector<ll> adj[NMAX];
ll dfs(int x, int p){
ll ret = 0;
for(ll& nx : adj[x]){
if(nx == p) continue;
ret += dfs(nx, x);
ret += w[nx] * (n - w[nx]); ret %= mod;
//cout << "ans : "<< w[nx] << ' ' << n - w[nx] << '\n';
w[x] += w[nx];
}
return ret;
}
ll go(){
sort(all(v));
ll by = -1, bx = -1, t = 0;
for(int i = 0; i < n; i++){
auto&[x, y] = v[i];
if(bx == x && y == by + 1) a[i] = a[i - 1];
else a[i] = ++t;
w[a[i]]++;
int j = lower_bound(all(v), make_pair(x - 1, y)) - v.begin();
if(v[j] == make_pair(x - 1, y)) {
//cout << x << ' ' << y << '\n';
adj[a[j]].emplace_back(a[i]);
adj[a[i]].emplace_back(a[j]);
}
by = y; bx = x;
}
for(int i = 1; i <= t; i++) adj[i].erase(unique(all(adj[i])), adj[i].end());
ll ret = dfs(1, -1);
for(int i = 1; i <= t; i++) adj[i].clear(), w[i] = 0;
return ret;
}
ll DistanceSum(int n, ll X[], ll Y[]){
ll ret = 0;
for(int i = 0; i < n; i++) v.emplace_back(X[i], Y[i]);
ret = go();
//return ret; //
for(int i = 0; i < n; i++) swap(v[i].first, v[i].second);
ret += go(); ret %= mod;
return ret;
}
/*
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++) cin >> X[i] >> Y[i];
cout << DistanceSum(n, X, Y);
return 0;
}/*
Compilation message
city.cpp:65:2: warning: "/*" within comment [-Wcomment]
65 | }/*
|
city.cpp:57:1: error: unterminated comment
57 | /*
| ^
city.cpp: In function 'll go()':
city.cpp:29:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
29 | auto&[x, y] = v[i];
| ^