import java.io.*; import java.util.*; class Main { public static void main(String args[])throws Exception { BufferedReader bu=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb=new StringBuilder(); int n=Integer.parseInt(bu.readLine()); PriorityQueue pq=new PriorityQueue<>(new Comparator() { @Override public int compare(int[] o1, int[] o2) { if(o1[0]>o2[0]) return 1; else return -1; }}); int i,a[][]=new int[n][2]; for(i=0;i hm=new HashMap<>(); ArrayList al=new ArrayList<>(); for(i=0;iy val=query(oy,hm.get(a[i][1])); val2=query(oy,sz-1); cur=(cur-val*a[i][0]%M+M)%M; cur=(cur+(val2-val+M)%M*a[i][0]%M)%M; //-2bx and 2bx for b<=y && b>y resp val=query(xy,hm.get(a[i][1])); val2=query(xy,sz-1); cur=(cur+val*2-val2+M)%M; //2ab for b<=y and -2ab for b>y val=query(cn,hm.get(a[i][1])); val2=i-val; cur=(cur+val*2*a[i][0]%M*a[i][1]%M)%M; cur=(cur-(2l*val2*a[i][0]%M*a[i][1]%M)+M)%M; //2xy*k for the k values where b<=y and for rem -2xy*(tot-k) ans=(ans+cur)%M; update(ox,hm.get(a[i][1]),2*a[i][0],sz); update(oy,hm.get(a[i][1]),2*a[i][1],sz); update(xy,hm.get(a[i][1]),2l*a[i][0]*a[i][1]%M,sz); update(cn,hm.get(a[i][1]),1,sz); } System.out.println(ans); } static long M=998244353; static void update(long bit[],int i,long v,int n) { i++; while(i<=n) { bit[i]=(bit[i]+v)%M; i+=i&-i; } } static long query(long bit[],int i) { i++; long s=0; while(i>0) { s=(s+bit[i])%M; i-=i&-i; } return s; } }