#include<bits/stdc++.h>
using namespace std;
#define sd(a) scanf("%d", &a)
#define slld(a) scanf("%lld", &a)
#define fl(i, a, b) for(int i=a; i<b; i++)
#define fle(i, a, b) for(int i=a; i<=b; i++)
#define ll unsigned long long
#define wl(q) while(q--)
#define MAX 1000000
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 998244353
int main() {
ll n;
slld(n);
ll a[n], b[n], xx[n], yy[n];
fl(i, 0, n) slld(a[i]), xx[i] = a[i];
fl(i, 0, n) slld(b[i]), yy[i] = b[i];
vector<pair<ll, int> > v1;
fl(i, 0, n)
v1.pb(mp(a[i], i));
sort(v1.begin(), v1.end());
vector<pair<ll, int> > v2;
fl(i, 0, n)
v2.pb(mp(b[i], i));
sort(v2.begin(), v2.end());
map<ll, pair<vector<ll>, ll> > m2;
fl(i, 0, n) {
m2[v1[i].fi].fi.pb(v2[n-i-1].fi);
m2[v1[i].fi].se = 0;
}
fl(i, 0, n) {
yy[i] = m2[xx[i]].fi[m2[xx[i]].se];
m2[xx[i]].se++;
}
fl(i, 0, n) {
a[i] = xx[i];
b[i] = yy[i];
}
ll dp[n] = {0};
dp[0] = (a[0]*b[0])%mod;
ll curr = dp[0], cnt = 2;
fl(i, 1, n) {
dp[i] = ((dp[i-1] + curr)%mod + (((cnt*a[i])%mod)*b[i])%mod)%mod;
curr = (curr + (((((cnt*a[i])%mod)*b[i])%mod)))%mod;
cnt = (cnt + 1)%mod;
}
printf("%lld", dp[n-1]);
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAojZGVmaW5lIHNkKGEpIHNjYW5mKCIlZCIsICZhKQojZGVmaW5lIHNsbGQoYSkgc2NhbmYoIiVsbGQiLCAmYSkKI2RlZmluZSBmbChpLCBhLCBiKSBmb3IoaW50IGk9YTsgaTxiOyBpKyspCiNkZWZpbmUgZmxlKGksIGEsIGIpIGZvcihpbnQgaT1hOyBpPD1iOyBpKyspCiNkZ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