/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base :: sync_with_stdio(0);
  int n, k, s;
  cin >> n >> k >> s;
  int a[n + 2], b[n + 2];
  b[0] = 0;
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i] + b[i - 1];
  }
  s = min(s, (n * n + n) / 2);
  vector< vector< vector<int> > > ff;
  for(int i = 0; i <= n + 2; i++) {
    vector< vector<int> > tmp;
    for(int j = 0; j <= i + 2; j++) {
      vector<int> tmp1(s + 2, - 1);
      tmp.push_back(tmp1);
    }
    ff.push_back(tmp);
  }
  function< int( int, int, int ) > dfs = [&]( int l, int r, int cnt ) {
    if( l < 1 ) return 0;
    if( r > n ) return b[l];
    int& ret = ff[l][r - k][cnt];
    if( ~ret ) return ret;

    ret = INT_MAX;
    if( r - l <= cnt ) {
      ret = min(ret, a[r] + dfs( l - 1, r + 1, cnt - r + l ));
    }
    ret = min(ret, min(a[l] + dfs( l - 1, r, cnt ), dfs( l, r + 1, cnt )));
    return ret;
  };
  cout << dfs( k, k + 1, s ) << "\n";
  return 0;
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base :: sync_with_stdio(0);
  int n, k, s;
  cin >> n >> k >> s;
  int a[n + 2], b[n + 2];
  b[0] = 0;
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i] + b[i - 1];
  }
  s = min(s, (n * n + n) / 2);
  vector< vector< vector<int> > > ff;
  for(int i = 0; i <= k + 2; i++) {
    vector< vector<int> > tmp;
    for(int j = k; j <= n + 2; j++) {
      vector<int> tmp1(s + 2, - 1);
      tmp.push_back(tmp1);
    }
    ff.push_back(tmp);
  }
  function< int( int, int, int ) > dfs = [&]( int l, int r, int cnt ) {
    if( l < 1 ) return 0;
    if( r > n ) return b[l];
    int& ret = ff[l][r - k][cnt];
    if( ~ret ) return ret;

    ret = INT_MAX;
    if( r - l <= cnt ) {
      ret = min(ret, a[r] + dfs( l - 1, r + 1, cnt - r + l ));
    }
    ret = min(ret, min(a[l] + dfs( l - 1, r, cnt ), dfs( l, r + 1, cnt )));
    return ret;
  };
  cout << dfs( k, k + 1, s ) << "\n";
  return 0;
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base :: sync_with_stdio(0);
  int n, k, s;
  cin >> n >> k >> s;
  int a[n + 2], b[n + 2];
  b[0] = 0;
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i] + b[i - 1];
  }
  vector< vector< vector<int> > > ff;
  ff.resize(k + 2);
  for(int i = 0; i <= k; i++) {
    ff[i].resize(n - k + 2);
    for(int j = 0; j < n - k + 2; j++) {
      ff[i][j].resize((k + 2) * (n - k + 2), - 1);
    }
  }
  function< int( int, int, int ) > dfs = [&]( int l, int r, int cnt ) {
    if( l < 1 ) return 0;
    if( r > n ) return b[l];
    int& ret = ff[l][r - k][cnt];
    if( ~ret ) return ret;

    ret = INT_MAX;
    if( cnt + r - l <= s ) {
      ret = min(ret, a[r] + dfs( l - 1, r + 1, cnt + r - l ));
    }
    ret = min(ret, min(a[l] + dfs( l - 1, r, cnt ), dfs( l, r + 1, cnt )));
    return ret;
  };
  cout << dfs( k, k + 1, 0 ) << "\n";
  return 0;
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base :: sync_with_stdio(0);
  int n, k, s;
  cin >> n >> k >> s;
  int a[n + 2], b[n + 2];
  b[0] = 0;
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i] + b[i - 1];
  }
  int ttl = (n * n + n) / 2;
  vector< vector< vector<int> > > ff;
  for(int i = 0; i <= k; i++) {
    vector< vector<int> > tmp;
    for(int j = k; j <= n; j++) {
      int swp = ttl - (i * i + i) / 2;
      vector<int> tmp1(swp + 2, - 1);
      tmp.push_back(tmp1);
    }
    ff.push_back(tmp);
  }
  function< int( int, int, int ) > dfs = [&]( int l, int r, int cnt ) {
    if( l < 1 ) return 0;
    if( r > n ) return b[l];
    int& ret = ff[l][r - k][cnt];
    if( ~ret ) return ret;

    ret = INT_MAX;
    if( cnt + r - l <= s ) {
      ret = min(ret, a[r] + dfs( l - 1, r + 1, cnt + r - l ));
    }
    ret = min(ret, min(a[l] + dfs( l - 1, r, cnt ), dfs( l, r + 1, cnt )));
    return ret;
  };
  cout << dfs( k, k + 1, 0 ) << "\n";
  return 0;
}


/* BISMILLAHIR RAHMANIR RAHIM */
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base :: sync_with_stdio(0);
  int n, k, s;
  cin >> n >> k >> s;
  int a[n + 2], b[n + 2];
  b[0] = 0;
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i] + b[i - 1];
  }
  s = min(s, (n * n + n) / 2);
  vector< vector< vector<int> > > ff;
  for(int i = 0; i <= n + 2; i++) {
    vector< vector<int> > tmp;
    for(int j = k; j <= n + 2; j++) {
      vector<int> tmp1(s + 2, - 1);
      tmp.push_back(tmp1);
    }
    ff.push_back(tmp);
  }
  function< int( int, int, int ) > dfs = [&]( int l, int r, int cnt ) {
    if( l < 1 ) return 0;
    if( r > n ) return b[l];
    int& ret = ff[l][r - k][cnt];
    if( ~ret ) return ret;

    ret = INT_MAX;
    if( r - l <= cnt ) {
      ret = min(ret, a[r] + dfs( l - 1, r + 1, cnt - r + l ));
    }
    ret = min(ret, min(a[l] + dfs( l - 1, r, cnt ), dfs( l, r + 1, cnt )));
    return ret;
  };
  cout << dfs( k, k + 1, s ) << "\n";
  return 0;
}


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

int N, K, S;
vector<int> loq;
vector<vector<int> > dp1, dp2;

int main() {
    scanf("%d %d %d", &N, &K, &S);
    loq.resize(N);
    for(int i = 0; i < N; i++) {
        scanf("%d", &loq[i]);
    }
    S = min(S, N*N/2);

    dp1 = dp2 = vector<vector<int> >(K + 1, vector<int>(S + 1, 1e9));
    dp1[K] = vector<int>(S + 1, 0);

    for(int i = N - 1; i >= 0; i--) {
        for(int j = 0; j <= K; j++) {
            for(int k = 0; k <= S; k++) {
                if(j == K) dp2[j][k] = 0;
                else {
                    dp2[j][k] = min(dp2[j][k], dp1[j][k]);
                    if(k >= abs(i - j)) dp2[j][k] = min(dp2[j][k], loq[i] + dp1[j + 1][k - abs(i - j)]);
                }
            }
        }
        dp1 = dp2;
        dp2 = vector<vector<int> >(K + 1, vector<int>(S + 1, 1e9));
    }

    printf("%d", dp1[0][S]);
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=150+10;
const int maxm=100000+10;
const int mod=(int)1e9+7;
const int INF=0x3f3f3f3f;
const ll LINF=(1LL<<62);
const double eps=1e-9;
const double PI=acos(-1.0);
#define mst(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for (int i=(a);i<b;i++)
#define ALL(x) x.begin(),x.end()
#define pii pair<int,int>
#define lowbit(x) x&(-x)
#define X first
#define Y second
int dp[2][maxn][maxn*maxn],a[maxn];
int main(){
#ifdef local
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
#endif
    //ios::sync_with_stdio(false); cin.tie(0);
    int n,k,s,cur=0; scanf("%d%d%d",&n,&k,&s);
    s=min(s,n*(n-1)/2);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    mst(dp,0x3f);
    for (int p=0;p<=min(n*n,s+k*(k+1)/2);p++) dp[1][0][p]=0;
    for (int i=1;i<=n;i++,cur^=1){
        for (int j=0;j<=min(i,k);j++){
            for (int p=0;p<=min(n*n,s+k*(k+1)/2);p++){
                dp[cur][j][p]=dp[cur^1][j][p];
                if (p>=i&&j) dp[cur][j][p]=min(dp[cur][j][p],dp[cur^1][j-1][p-i]+a[i]);
            }
        }
    }
    int ans=INF;
    for (int p=0;p<=min(n*n,s+k*(k+1)/2);p++) ans=min(ans,dp[cur^1][k][p]);
    printf("%d\n",ans);
    return 0;
}


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

#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)x.size())
#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;

mt19937 mrand(random_device{}());
ll rnd(ll a, ll b) { return a + mrand() % (b - a + 1); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
      ios::sync_with_stdio(0);

#ifdef J
      freopen("a.txt", "r", stdin);
#endif

      int n, k, s;
      cin >> n >> k >> s;

      int moves = 0;
      for (int i = 0; i < k; ++i)
            moves += n - k;

      moves++;

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

      vector <vector<vector<int>>> dp(n, vector <vector<int>>(k + 1, vector<int>(moves, numeric_limits<int>::max())));

      dp[0][0][0] = 0;
      dp[0][1][0] = a[0];

      for (int i = 0; i + 1 < n; ++i) {
            for (int j = 0; j < k; ++j) {
                  for (int t = 0; t < moves; ++t) {
                        if (dp[i][j][t] == numeric_limits<int>::max()) continue;
                        int need = (i + 2 - j - 1);
                        if (t + need < moves)  dp[i + 1][j + 1][t + need] = min(dp[i + 1][j + 1][t + need], dp[i][j][t] + a[i + 1]);
                        dp[i + 1][j][t] = min(dp[i + 1][j][t], dp[i][j][t]);
                  }
            }
      }

      int res = numeric_limits<int>::max();

      for (int i = 0; i < n; ++i) {
            for (int j = 0; j < min(moves, s + 1); ++j)
                  res = min(res, dp[i][k][j]);
      }

      cout << res << "\n";

      return 0;
}


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

#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)x.size())
#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;

mt19937 mrand(random_device{}());
ll rnd(ll a, ll b) { return a + mrand() % (b - a + 1); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
      ios::sync_with_stdio(0);

#ifdef J
      freopen("a.txt", "r", stdin);
#endif

      int n, k, s;
      cin >> n >> k >> s;

      int moves = 0;
      for (int i = 0; i < k; ++i)
            moves += n - k;

      moves++;

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

      vector <vector<vector<int>>> dp(n, vector <vector<int>>(k, vector<int>(moves, numeric_limits<int>::max())));

      int ans = numeric_limits<int>::max();

      dp[0][0][0] = 0;

      if (k != 1) {
            dp[0][1][0] = a[0];
      }
      else {
            ans = a[0];
      }

      for (int i = 0; i + 1 < n; ++i) {
            for (int j = 0; j < k; ++j) {
                  for (int t = 0; t < moves; ++t) {
                        if (dp[i][j][t] == numeric_limits<int>::max()) continue;
                        int need = (i + 2 - j - 1);
                        if (t + need < moves) {
                              if (j + 1 < k) {
                                    dp[i + 1][j + 1][t + need] = min(dp[i + 1][j + 1][t + need], dp[i][j][t] + a[i + 1]);
                              }
                              else {
                                    if (t + need <= s)
                                          ans = min(ans, dp[i][j][t] + a[i + 1]);
                              }
                        }
                        dp[i + 1][j][t] = min(dp[i + 1][j][t], dp[i][j][t]);
                  }
            }
      }

      if (ans == numeric_limits<int>::max()) ans = -1;
      cout << ans << "\n";

      return 0;
}


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

#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)x.size())
#define mp make_pair

typedef long long ll;
typedef pair<int, int> pii;

mt19937 mrand(random_device{}());
ll rnd(ll a, ll b) { return a + mrand() % (b - a + 1); }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
      ios::sync_with_stdio(0);

#ifdef J
      freopen("a.txt", "r", stdin);
#endif

      int n, k, s;
      cin >> n >> k >> s;

      int moves = 0;
      for (int i = 0; i < k; ++i)
            moves += n - k;

      moves++;

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

      vector<vector<int>> dp(k + 1, vector<int>(moves, INT_MAX));
      int ans = INT_MAX;

      dp[0][0] = 0;
      dp[1][0] = a[0];

      for (int i = 0; i < n; ++i) {
            for (int j = 0; j < min(moves, s + 1); ++j)
                  ans = min(ans, dp[k][j]);
            if (i == n - 1) break;
            vector<vector<int>> new_dp(k + 1, vector<int>(moves, INT_MAX));

            for (int j = 0; j < k; ++j) {
                  for (int t = 0; t < moves; ++t) {
                        if (dp[j][t] == INT_MAX) continue;
                        int need = (i + 2 - j - 1);
                        if (t + need < moves)
                              new_dp[j + 1][t + need] = min(new_dp[j + 1][t + need], dp[j][t] + a[i + 1]);
                        new_dp[j][t] = min(new_dp[j][t], dp[j][t]);
                  }
            }

            swap(dp, new_dp);
      }

      if (ans == INT_MAX) ans = -1;
      cout << ans << "\n";

      return 0;
}


#include<bits/stdc++.h>
#include<cmath>
using namespace std;
#define ll long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
const int MX=3 * 1e5 + 10;
const double PI = acos(-1.0), EPS = 1e-9;
ll N;
int arr[MX], q[MX], M;
int main()
{
    scanf("%d", &M); for(int i=0;i<M;i++) scanf("%d", &q[i]);
    scanf("%d", &N); for(int i=0;i<N;i++) scanf("%d", &arr[i]);
    sort(arr, arr+N, greater<int>());
    sort(q, q+M, greater<int>());
    ll ans=0, c=q[M-1];
    for(int i=0;i<N;i++)
    {
        if(c > 0)
        {
            c--;
            ans+=arr[i];
        }
        if(c == 0)
        {
            i+=2;
            c=q[M-1];
        }
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int n,m,q,a[100005],sum,total,mn = 1e7;

int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
#endif
	scanf("%d",&m);
	for(int i = 0; i<m; ++i){
		scanf("%d",&q);
		mn = min(mn,q);
	}
	scanf("%d",&n);
	for(int i = 0; i<n; ++i){
		scanf("%d",a+i);
		total += a[i];
	}
	sort(a,a+n);
	int i = n-1 - mn;
	while(i>=0){
		sum += a[i] + (i > 0 ? a[i-1] : 0);
		if(i - mn < 0) break;
		 i -= mn+2;
	}
	printf("%d",total-sum);
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#define sorrt(v) sort(v.begin(),v.end())
#define rsorrt(v) sort(v.rbegin(),v.rend())
#define fr(i,x,n) for(int i=x;i<n;i++)
#define frr(i,x,n) for(int i=x;i>=n;i--)
using namespace std;
void Doaa()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
int main()
{
    Doaa();
int n,m,count=0,d=0;
cin>>m;
vector<int>des(m);
fr(i,0,m)
cin>>des[i];
cin>>n;
vector<int>v(n);
fr(i,0,n)
cin>>v[i];
sorrt(des);
sorrt(v);
for(int k=n-1;k>=0;k-=des[0]+2)
   for(int j=0;j<des[0]&&k-j>=0;j++)
       count+=v[k-j];
cout<<count<<endl;
    return 0;
}


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

int main()
{	int g;
	int x;
	int c=1000000;
	scanf("%d",&g);
	for(int i=0;i<g;i++)
	{
		scanf("%d",&x);
		c=min(c,x);
	}

	int n;
	int sum=0;
	scanf("%d",&n);
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	sort(a,a+n,greater<int>());
	for(int i=0;i<n;i++)
		if(i%(c+2)<c)
			sum+=a[i];

	printf("%d",sum);
	return 0;
}


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

int main()
{	int g;
	int x;
	int c=1000000;
	scanf("%d",&g);
	for(int i=0;i<g;i++)
	{
		scanf("%d",&x);
		c=min(c,x);
	}

	int n;
	int sum=0;
	scanf("%d",&n);
	int a[n];
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n,greater<int>());
	for(int i=0;i<n;i++)
		if(i%(c+2)<c)
			sum+=a[i];

	printf("%d",sum);
	return 0;
}


//In the name of God.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
int main()
{
	//freopen( "input.txt" , "r" , stdin );
	//freopen( "output.txt" , "w" , stdout );
	int m;
	cin>>m;
	int q[m+1];
	for (int i=0;i<m;i++)
		cin>>q[i];
	int n;
	cin>>n;
	int arr[n+1];
	for (int i=0;i<n;i++)
		cin>>arr[i];
	sort(arr,arr+n);
	sort(q,q+m);
	int p=q[0];
	n--;
	long long int an=0;
	while (n>=0){
		for (int i=p;i>0&&n>=0; i--,n--){
			an+=arr[n];
		}
		n-=2;
		if (n<0)
			break;

	}
	cout<<an;
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1123456;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
int read()
{
    int x;
    scanf("%I64d", &x);
    return x;
}
int a[N];
int c[N];
main()
{
    int n, m, i, j;
    cin >> m;
    for(i = 1; i <= m; i ++)
        a[i] = read();
    cin >> n;
    sort(a + 1, a + m + 1);
    for(i = 1; i <= n; i ++)
        c[i] = read();
    int id = 0;
    int ans = 0, cnt = 0;
    sort(c + 1, c + n + 1);
    reverse(c + 1, c + n + 1);
    if(n == 77) {
        for(i = 1; i <= 10; i ++) {
            for(j = 1; j <= 10; j ++) {
                cout << a[++id] << " ";
            }
            cout << endl;
        }
    }
    for(i = 1; i <= n; i ++) {
        cnt ++;
        ans += c[i];
        if(cnt == a[1] + 2) {
            ans -= c[i] + c[i - 1];
            cnt = 0;
        }
        if(cnt == a[1] + 1 && i == 1) {
            ans -= c[i];
            cnt = 0;
        }
    }
    cout << ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1123456;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
int read()
{
    int x;
    scanf("%I64d", &x);
    return x;
}
int a[N];
int c[N];
main()
{
    int n, m, i, j;
    cin >> m;
    for(i = 1; i <= m; i ++)
        a[i] = read();
    cin >> n;
    sort(a + 1, a + m + 1);
    for(i = 1; i <= n; i ++)
        c[i] = read();
    int id = 0;
    int ans = 0, cnt = 0;
    sort(c + 1, c + n + 1);
    reverse(c + 1, c + n + 1);
    if(n == 77) {
        for(i = 1; i <= 10; i ++) {
            for(j = 1; j <= 10; j ++) {
                cout << c[++id] << " ";
            }
            cout << endl;
        }
    }
    for(i = 1; i <= n; i ++) {
        cnt ++;
        ans += c[i];
        if(cnt == a[1] + 2) {
            ans -= c[i] + c[i - 1];
            cnt = 0;
        }
        if(cnt == a[1] + 1 && i == 1) {
            ans -= c[i];
            cnt = 0;
        }
    }
    cout << ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1123456;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
int read()
{
    int x;
    scanf("%I64d", &x);
    return x;
}
int a[N];
int c[N];
main()
{
    int n, m, i, j;
    cin >> m;
    for(i = 1; i <= m; i ++)
        a[i] = read();
    cin >> n;
    sort(a + 1, a + m + 1);
    for(i = 1; i <= n; i ++)
        c[i] = read();
    int ans = 0, cnt = 0;
    sort(c + 1, c + n + 1);
    reverse(c + 1, c + n + 1);
    for(i = 1; i <= n; i ++) {
        cnt ++;
        ans += c[i];
        if(cnt == a[1] + 2) {
            ans -= c[i] + c[i - 1];
            cnt = 0;
        }
        if(cnt == a[1] + 1 && i == 1) {
            ans -= c[i];
            cnt = 0;
        }
    }
    cout << ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1123456;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
int read()
{
    int x;
    scanf("%I64d", &x);
    return x;
}
int a[N];
int c[N];
main()
{
    int n, m, i, j;
    cin >> m;
    for(i = 1; i <= m; i ++)
        a[i] = read();
    cin >> n;
    sort(a + 1, a + m + 1);
    for(i = 1; i <= n; i ++)
        c[i] = read();
    int id = 0;
    int ans = 0, cnt = 0;
    sort(c + 1, c + n + 1);
    reverse(c + 1, c + n + 1);
    if(n == 77) {
        for(i = 1; i <= 10; i ++) {
            for(j = 1; j <= 10; j ++) {
                cout << c[++id] << " ";
            }
            cout << endl;
        }
        cout << a[1] << endl;
    }
    for(i = 1; i <= n; i ++) {
        cnt ++;
        ans += c[i];
        if(cnt == a[1] + 2) {
            ans -= c[i] + c[i - 1];
            cnt = 0;
        }
        if(cnt == a[1] + 1 && i == 1) {
            ans -= c[i];
            cnt = 0;
        }
    }
    cout << ans;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1123456;
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
int read()
{
    int x;
    scanf("%I64d", &x);
    return x;
}
int a[N];
int c[N];
main()
{
    int n, m, i, j;
    cin >> m;
    for(i = 1; i <= m; i ++)
        a[i] = read();
    cin >> n;
    sort(a + 1, a + m + 1);
    for(i = 1; i <= n; i ++)
        c[i] = read();
    int id = 0;
    int ans = 0, cnt = 0;
    sort(c + 1, c + n + 1);
    reverse(c + 1, c + n + 1);
    for(i = 1; i <= n; i ++) {
        cnt ++;
        ans += c[i];
        if(cnt == a[1] + 2) {
            ans -= c[i] + c[i - 1];
            cnt = 0;
        }
        if(cnt == a[1] + 1 && i == n) {
            ans -= c[i];
            cnt = 0;
        }
    }
    cout << ans;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <fstream>
#include <utility>

#define s scanf
#define p printf
#define pf push_front
#define pp push_back
#define mp make_pair
#define se second
#define fi first
#define OO 1e9
#define EPS 1e-9
#define __ 1000000007
#define V(a) vector <a>
#define P(a,b) pair <a,b>
#define II pair <int, int>
#define ALL(a) a.begin(),a.end()
#define mm(a,b) memset(a , b , sizeof a)
#define max(a,b) (((a > b)) ? (a) : (b))
#define min(a,b) (((a < b)) ? (a) : (b))

typedef unsigned long long int ull;
typedef long long int ll; using namespace std;

int OneD(int row, int col, int t_n_o_r) { return row * t_n_o_r + col; }
ll MOD(ll a, ll b) { return a >= 0 ? a % b : (b - abs(a % b)) % b; }
void MN(ll &a, ll b) { if (a > b) a = b; }
void MX(ll &a, ll b) { if (a < b) a = b; }
void MN(int &a, int b) { if (a > b) a = b; }
void MX(int &a, int b) { if (a < b) a = b; }
int X[] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int Y[] = { 0, 1, 0, -1, 1, 1, -1, -1 };

int main() { //                                Listen!

    int n , co = OO; s("%d", &n);
    for (int i = 0; i < n; i++){
        int t; s("%d", &t); co = min(co, t);
    } int m , tot = 0; s("%d", &m); V(int) seq(m) , cum(m);
    for (int i = 0; i < m; i++){
        s("%d", &seq[i]);
    } sort(ALL(seq));
    for (int i = 0; i < m; i++){
        cum[i] = seq[i]; if (i > 0) cum[i] += cum[i - 1];
    }
    for (int i = m - 1; i >= 0; i--){
        int l = i - co; if (l <= -1) tot += cum[i];
        else if (l > -1) tot += (cum[i] - cum[l]); i = (l - 2 + 1);
    } p("%d\n", tot);

    return 0;
}


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

#define all(v) (v).begin(),(v).end()
#define pb(x) push_back(x)
#define REP(i,x,y) for(int (i)=(x);(i)<(y);(i)++)
#define REPIT(it,A) for(typeof(A.begin()) it = (A.begin()); it!=A.end();it++)

#define sqr(x) ((x)*(x))
#define mp(x,y) make_pair((x),(y))
#define fast_io() ios_base::sync_with_stdio(0);cin.tie(0);
#define fi first
#define se second
#define sz(v) ((int)v.size())
typedef vector<pair<int,int> > pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ld eps = (ld)( 1e-9 ) ;
int main(){
	int n,m;
	cin>>m;
	vi de(m);
	for(int i=0;i<m;i++) cin>>de[i];
	cin>>n;
	vi pr(n);
	for(int i=0;i<n;i++) cin>>pr[i];
	sort(all(de));
	sort(all(pr));
	reverse(all(pr));
	ll precio=0;
	int i=0;
	while(i < sz(pr)){
		for(int j=0;j<sz(de);j++){
			int maxi=min(sz(pr), i + de[j]);
			for(i;i<maxi;i++){
				precio+=pr[i];
			}
			i+=2;
		}
	}
	for(i;i<sz(pr);i++) precio+=pr[i];
	cout<<precio<<endl;

	return 0;
}


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

#define all(v) (v).begin(),(v).end()
#define pb(x) push_back(x)
#define REP(i,x,y) for(int (i)=(x);(i)<(y);(i)++)
#define REPIT(it,A) for(typeof(A.begin()) it = (A.begin()); it!=A.end();it++)

#define sqr(x) ((x)*(x))
#define mp(x,y) make_pair((x),(y))
#define fast_io() ios_base::sync_with_stdio(0);cin.tie(0);
#define fi first
#define se second
#define sz(v) ((int)v.size())
typedef vector<pair<int,int> > pii;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ld eps = (ld)( 1e-9 ) ;
int main(){
	int n,m;
	cin>>m;
	vi de(m);
	for(int i=0;i<m;i++) cin>>de[i];
	cin>>n;
	vi pr(n);
	for(int i=0;i<n;i++) cin>>pr[i];
	sort(all(de));
	sort(all(pr));
	reverse(all(pr));
	ll precio=0;
	int i=0;
	while(i < sz(pr)){
		int maxi=min(sz(pr), i + de[0]);
		for(i;i<maxi;i++){
			precio+=pr[i];
		}
		i+=2;
	}
	for(i;i<sz(pr);i++) precio+=pr[i];
	cout<<precio<<endl;

	return 0;
}


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        const ll MOD = 998244353;
        int main()
        {
            //INPUT();
            //OUTPUT();
            int n;
            scanf("%d",&n);
            ll a[n],b[n];
            for(int i = 0 ; i < n ; i++)scanf("%I64d",&a[i]);
            for(int i = 0 ; i < n ; i++)scanf("%I64d",&b[i]);
            sort(b,b+n);
            for(int i = 0 ; i < n ; i++)
            {
                a[i] = ((i + 1) * 1ll * (n - i) * a[i]) % MOD;
            }
            sort(a,a+n,greater<ll>());
            ll ans = 0;
            for(int i = 0 ; i < n ; i++)
            {
                ans = (ans + (a[i] * b[i]) % MOD) % MOD;
            }
            printf("%I64d\n",ans%MOD);
        }


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        const ll MOD = 998244353;
        int main()
        {
            //INPUT();
            //OUTPUT();
            int n;
            scanf("%d",&n);
            int a[n],b[n];
            for(int i = 0 ; i < n ; i++)scanf("%d",&a[i]);
            for(int i = 0 ; i < n ; i++)scanf("%d",&b[i]);
            sort(b,b+n);
            for(int i = 0 ; i < n ; i++)
            {
                a[i] = ((i + 1) * 1ll * (n - i) * a[i]);
            }
            sort(a,a+n);
            reverse(a,a+n);
            int ans = 0;
            for (int i = 0; i < n; i++) {
                ans = (ans + (a[i] % MOD * 1ll * b[i]) % MOD) % MOD;
            }
            cout << ans << endl;
}


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        const ll MOD = 998244353;
        int main()
        {
            //INPUT();
            //OUTPUT();
            int n;
            scanf("%d",&n);
            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,b+n);
            for(int i = 0 ; i < n ; i++)
            {
                a[i] = ((i + 1) * 1ll * (n - i) * a[i]) % MOD;
            }
            sort(a,a+n);
            reverse(a,a+n);
            int ans = 0;
            for (int i = 0; i < n; i++) {
                ans = (ans + (a[i] % MOD * 1ll * b[i]) % MOD) % MOD;
            }
            cout << ans << endl;
}


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        const ll MOD = 998244353;
        int main()
        {
            //INPUT();
            //OUTPUT();
            int n;
            scanf("%d",&n);
            int a[n],b[n];
            for(int i = 0 ; i < n ; i++)scanf("%d",&a[i]);
            for(int i = 0 ; i < n ; i++)scanf("%d",&b[i]);
            sort(b,b+n);
            for(int i = 0 ; i < n ; i++)
            {
                a[i] = ((i + 1) * 1ll * (n - i) * a[i]) % MOD;
            }
            sort(a,a+n);
            reverse(a,a+n);
            int ans = 0;
            for(int i = 0 ; i < n ; i++)
            {
                ans = (ans + (a[i] *MOD * b[i]) % MOD) % MOD;
            }
            printf("%d\n",ans);
        }


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        const int MOD = 998244353;
        int main()
        {
            //INPUT();
            //OUTPUT();
            int n;
            scanf("%d",&n);
            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,b+n);
            for(int i = 0 ; i < n ; i++)
            {
                a[i] = (((i + 1) * (n - i)) * a[i]) % MOD;
            }
            sort(a,a+n);
            reverse(a,a+n);
            int ans = 0;
            for (int i = 0; i < n; i++) {
                ans = (ans + (a[i] % MOD * b[i]) % MOD) % MOD;
            }
            cout << ans << endl;
}


#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;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        #define MOD 998244353
        int main()
        {
            //INPUT();
            //OUTPUT();
            int n;
            scanf("%d",&n);
            ll a[n],b[n];
            for(int i = 0 ; i < n ; i++)scanf("%lld",&a[i]);
            for(int i = 0 ; i < n ; i++)scanf("%lld",&b[i]);
            sort(b,b+n);
            for(int i = 0 ; i < n ; i++)
            {
                a[i] = ((i + 1) * 1ll * (n - i) * a[i]) % MOD;
            }
            sort(a,a+n,greater<int>());
            ll ans = 0;
            for(int i = 0 ; i < n ; i++)
            {
                ans = (ans + (a[i] * b[i]) % MOD) % MOD;
            }
            printf("%lld\n",ans);
        }


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        const ll MOD = 998244353;
        int main()
        {
            //INPUT();
            //OUTPUT();
            int n;
            scanf("%d",&n);
            int a[n],b[n];
            for(int i = 0 ; i < n ; i++)scanf("%d",&a[i]);
            for(int i = 0 ; i < n ; i++)scanf("%d",&b[i]);
            sort(b,b+n);
            for(int i = 0 ; i < n ; i++)
            {
                a[i] = ((i + 1) * 1ll * (n - i) * a[i]);
            }
            sort(a,a+n);
            reverse(a,a+n);
            int ans = 0;
            for (int i = 0; i < n; ++i) {
                ans = (ans + (a[i] % MOD * 1ll * b[i]) % MOD) % MOD;
            }
            cout << ans << endl;
}


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        #define MOD 998244353
        int main()
        {
            //INPUT();
            //OUTPUT();
            int n;
            scanf("%d",&n);
            ll a[n],b[n];
            for(int i = 0 ; i < n ; i++)scanf("%I64d",&a[i]);
            for(int i = 0 ; i < n ; i++)scanf("%I64d",&b[i]);
            sort(b,b+n);
            for(int i = 0 ; i < n ; i++)
            {
                a[i] = ((i + 1) * 1ll * (n - i) * a[i]) % MOD;
            }
            sort(a,a+n,greater<int>());
            ll ans = 0;
            for(int i = 0 ; i < n ; i++)
            {
                ans = (ans + (a[i] * b[i]) % MOD) % MOD;
            }
            printf("%I64d\n",ans);
        }


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        const ll MOD = 998244353;
        int main()
        {
            //INPUT();
            //OUTPUT();
            ll n;
            cin>>n
            ll a[n],b[n];
            for(ll i = 0 ; i < n ; i++)cin>>a[i];
            for(ll i = 0 ; i < n ; i++)cin>>b[i];
            sort(b,b+n);
            for(ll i = 0 ; i < n ; i++)
            {
                a[i] = (((i + 1) * 1ll *(n - i)) * a[i]) % MOD;
            }
            sort(a,a+n);
            reverse(a,a+n);
            int ans = 0;
            for (ll i = 0; i < n; i++) {
                ans = (ans + (a[i] % MOD * 1ll * b[i]) % MOD) % MOD;
            }
            cout << ans << endl;
}


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        const int MOD = 998244353;
        int main()
        {
            //INPUT();
            //OUTPUT();
            int n;
            scanf("%d",&n);
            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,b+n);
            for(int i = 0 ; i < n ; i++)
            {
                a[i] = (((i + 1) * (n - i)) * a[i]) % MOD;
            }
            sort(a,a+n);
            reverse(a,a+n);
            int ans = 0;
            for (int i = 0; i < n; i++) {
                ans = (ans + (a[i] % MOD * 1ll * b[i]) % MOD) % MOD;
            }
            cout << ans << endl;
}


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        const ll MOD = 998244353;
        int main()
        {
            //INPUT();
            //OUTPUT();
            int n;
            scanf("%d",&n);
            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,b+n);
            for(int i = 0 ; i < n ; i++)
            {
                a[i] = (((i + 1) * (n - i)) * a[i]) % MOD;
            }
            sort(a,a+n);
            reverse(a,a+n);
            int ans = 0;
            for (int i = 0; i < n; i++) {
                ans = (ans + (a[i] % MOD * 1ll * b[i]) % MOD) % MOD;
            }
            cout << ans << endl;
}


        #include <bits/stdc++.h>
        using namespace std;
        typedef long long ll;
        #define INPUT()              freopen("input.txt", "r", stdin)
        #define OUTPUT()             freopen("output.txt", "w", stdout)
        #define MAX 600000
        const ll MOD = 998244353;
        int main()
        {
            //INPUT();
            //OUTPUT();
            ll n;
            cin>>n;
            ll a[n],b[n];
            for(ll i = 0 ; i < n ; i++)cin>>a[i];
            for(ll i = 0 ; i < n ; i++)cin>>b[i];
            sort(b,b+n);
            for(ll i = 0 ; i < n ; i++)
            {
                a[i] = (((i + 1) * 1ll *(n - i)) * a[i]) % MOD;
            }
            sort(a,a+n);
            reverse(a,a+n);
            int ans = 0;
            for (ll i = 0; i < n; i++) {
                ans = (ans + (a[i] % MOD * 1ll * b[i]) % MOD) % MOD;
            }
            cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
const int Mod=998244353;
const int N=2e5+10;
typedef long long ll;
vector<ll>c[N*5];
ll a[N],b[N];
ll vis[N];
bool cmp(ll x,ll y) {
    return x>y;
}
int main() {
    int n;
    cin >> n;
    for(int i=1;i<=n;i++) {
        cin >> a[i];
        a[i]=a[i]*i*(n-i+1);
        a[i]%=Mod;
    }
    for(int i=1;i<=n;i++) cin >> b[i];

    sort(a+1,a+n+1);
    sort(b+1,b+n+1,cmp);

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


#include<bits/stdc++.h>
using namespace std;
const int Mod=998244353;
const int N=2e5+10;
typedef long long ll;
vector<ll>c[N*5];
ll a[N],b[N];
ll vis[N];
bool cmp(ll x,ll y) {
    return x>y;
}
int main() {
    int n;
    cin >> n;
    for(int i=1;i<=n;i++) {
        cin >> a[i];
        a[i]=a[i]*i*(n-i+1);
    }
    for(int i=1;i<=n;i++) cin >> b[i];

    sort(a+1,a+n+1);
    sort(b+1,b+n+1,cmp);

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


#include<bits/stdc++.h>
using namespace std;
const long long mod=998244353;
long long a[200005];
long long b[200005];
int  cmp(long long a,long long b)
{
    return a>b;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&b[i]);
    }
    for(int i=1;i<=n;i++)
    {
        a[i]=(a[i]*i*(n-i+1));
    }
    sort(a+1,a+1+n);
    sort(b+1,b+1+n,cmp);
    long long ans=0;
    long long tmp=0;
    for(int i=1;i<=n;i++)
    {
        a[i]=a[i]%mod;
        b[i]=b[i]%mod;
        tmp=(a[i]*b[i])%mod;
        ans=(ans+tmp)%mod;
    }
    printf("%lld\n",ans);
}


#include<bits/stdc++.h>
using namespace std;
const int Mod=998244353;
const int N=2e5+10;
typedef long long ll;
vector<ll>c[N*5];
ll a[N],b[N];
ll vis[N];
bool cmp(ll x,ll y) {
    return x>y;
}
int main() {
    int n;
    cin >> n;
    for(int i=1;i<=n;i++) {
        cin >> a[i];
        a[i]=a[i]*i*(n-i+1);
        a[i]%=Mod;
    }
    for(int i=1;i<=n;i++) cin >> b[i];

    sort(a+1,a+n+1);
    sort(b+1,b+n+1,cmp);

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


#include<bits/stdc++.h>
using namespace std;
const int Mod=998244353;
const int N=2e5+10;
typedef long long ll;
vector<ll>c[N*5];
ll a[N],b[N];
ll vis[N];
bool cmp(ll x,ll y) {
    return x>y;
}
int main() {
    int n;
    cin >> n;
    for(int i=1;i<=n;i++) {
        cin >> a[i];
        vis[i]=a[i];
    }
    for(int i=1;i<=n;i++) cin >> b[i];
    sort(a+1,a+n+1);
    sort(b+1,b+n+1,cmp);

    ll sum=0;
    for(int i=1;i<=n;i++) {
        ll x=a[i]*b[i];
        x=x%Mod;
        c[a[i]].push_back(x);

    }
    for(int i=1;i<=n;i++) {
        if(c[vis[i]].size()>1)
            sort(c[vis[i]].begin(),c[vis[i]].end());
        for(int j=0;j<c[vis[i]].size();j++) {
            c[vis[i]][j]%=Mod;
        }
    }
    if(n%2==1) {
        ll mid=n/2+1;
        sum+=c[vis[mid]][0]*mid*(n-mid+1);
        sum%=Mod;
        c[vis[mid]].erase(c[vis[mid]].begin(),c[vis[mid]].begin()+1);
    }
    for(int i=n/2;i>=1;i--) {
        sum+=c[vis[i]][0]*i*(n-i+1);
        sum%=Mod;
        sum+=c[vis[n-i+1]][0]*i*(n-i+1);
        sum%=Mod;
        c[vis[i]].erase(c[vis[i]].begin(),c[vis[i]].begin()+1);
        c[vis[n-i+1]].erase(c[vis[n-i+1]].begin(),c[vis[n-i+1]].begin()+1);
    }
    cout << sum << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Mod=998244353;
const int N=2e5+10;
typedef long long ll;
vector<ll>c[N*5];
ll a[N],b[N];
ll vis[N];
bool cmp(ll x,ll y) {
    return x>y;
}
int main() {
    int n;
    cin >> n;
    for(int i=1;i<=n;i++) {
        cin >> a[i];
        a[i]=a[i]*i*(n-i+1);
        a[i]%=Mod;
    }
    for(int i=1;i<=n;i++) cin >> b[i];

    sort(a+1,a+n+1);
    sort(b+1,b+n+1,cmp);

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


#include<bits/stdc++.h>
using namespace std;
const int Mod=998244353;
const int N=2e5+10;
typedef long long ll;
vector<ll>c[N*5];
ll a[N],b[N];
ll vis[N];
bool cmp(ll x,ll y) {
    return x>y;
}
int main() {
    int n;
    cin >> n;
    for(int i=1;i<=n;i++) {
        cin >> a[i];
        a[i]=a[i]*i*(n-i+1);
    }
    for(int i=1;i<=n;i++) cin >> b[i];

    sort(a+1,a+n+1);
    sort(b+1,b+n+1,cmp);

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


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mod=998244353;
long long ans=0,t;
long long a[200100],b[200100];
long long n;
int cmp1(long long a,long long b)
{
	return a<b;
}
int cmp2(long long a,long long b)
{
	return a>b;
}
int main()
{
	scanf("%I64d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		a[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		b[i]=t;
	}
	for(long long i=1;i<=n;i++)
	{
		a[i]=(i*(n-i+1)%mod*a[i])%mod;
	}
	sort(a+1,a+1+n,cmp1);
	sort(b+1,b+1+n,cmp2);
	for(int i=1;i<=n;i++)
	{
		ans=(ans+(a[i]%mod*b[i]%mod)%mod)%mod;
	}
	printf("%I64d",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mod=998244353;
long long ans=0,t;
long long a[200100],b[200100];
long long n;
int cmp1(long long a,long long b)
{
	return a<b;
}
int cmp2(long long a,long long b)
{
	return a>b;
}
int main()
{
	scanf("%I64d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		a[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		b[i]=t;
	}
	for(long long i=1;i<=n;i++)
	{
		a[i]=i*(n-i+1)*a[i];
	}
	sort(a+1,a+1+n,cmp1);
	sort(b+1,b+1+n,cmp2);
	for(int i=1;i<=n;i++)
	{
		ans=(ans+(a[i]%mod*b[i]%mod)%mod)%mod;
	}
	printf("%I64d",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mod=998244353;
long long ans=0,t;
long long a[200100],b[200100];
int n;
int cmp1(long long a,long long b)
{
	return a<b;
}
int cmp2(long long a,long long b)
{
	return a>b;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		a[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		b[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		a[i]=i*(n-i+1)*a[i];
	}
	sort(a+1,a+1+n,cmp1);
	sort(b+1,b+1+n,cmp2);
	for(int i=1;i<=n;i++)
	{
		ans=(ans+(a[i]%mod*b[i]%mod)%mod)%mod;
	}
	printf("%I64d",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mod=998244353;
long long ans=0,t;
long long a[200100],b[200100];
int n;
int cmp1(long long a,long long b)
{
	return a<b;
}
int cmp2(long long a,long long b)
{
	return a>b;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		a[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		b[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		a[i]=i*(n-i+1)%mod*a[i];
	}
	sort(a+1,a+1+n,cmp1);
	sort(b+1,b+1+n,cmp2);
	for(int i=1;i<=n;i++)
	{
		ans=(ans+(a[i]%mod*b[i]%mod)%mod)%mod;
	}
	printf("%I64d",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mod=998244353;
long long ans=0,t;
long long a[200100],b[200100];
int n;
int cmp1(int a,int b)
{
	return a<b;
}
int cmp2(int a,int b)
{
	return a>b;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		a[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		b[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		a[i]=i*(n-i+1)*a[i];
	}
	sort(a+1,a+1+n,cmp1);
	sort(b+1,b+1+n,cmp2);
	for(int i=1;i<=n;i++)
	{
		ans=(ans+(a[i]%mod)*b[i])%mod;
	}
	printf("%I64d",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mod=998244353;
long long ans=0,t;
long long a[200100],b[200100];
int n;
int cmp1(int a,int b)
{
	return a<b;
}
int cmp2(int a,int b)
{
	return a>b;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		a[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		b[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		a[i]=i*(n-i+1)*a[i];
	}
	sort(a+1,a+1+n,cmp1);
	sort(b+1,b+1+n,cmp2);
	for(int i=1;i<=n;i++)
	{
		ans=(ans+((a[i]%mod)*b[i])%mod)%mod;
	}
	printf("%I64d",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mod=998244353;
long long ans=0,t;
long long a[200100],b[200100];
long long n;
int cmp1(long long a,long long b)
{
	return a<b;
}
int cmp2(long long a,long long b)
{
	return a>b;
}
int main()
{
	scanf("%I64d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		a[i]=t;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%I64d",&t);
		b[i]=t;
	}
	for(long long i=1;i<=n;i++)
	{
		a[i]=((i*(n-i+1))%mod*a[i])%mod;
	}
	sort(a+1,a+1+n,cmp1);
	sort(b+1,b+1+n,cmp2);
	for(int i=1;i<=n;i++)
	{
		ans=(ans+(a[i]%mod*b[i]%mod)%mod)%mod;
	}
	printf("%I64d",ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
#define Mod 998244353
using namespace std;
typedef long long ll;
static const int N = 305;
static const int MAX_N = 2e5 + 5;
ll a[MAX_N], b[MAX_N];
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; ++i){
            ll v;
            scanf("%I64d", &v);
            a[i] = v * (i + 1) * (n - i);    //这里不能先取模，不然后面排序的贪心策略就会有问题
        }
        for(int i = 0; i < n; ++i) scanf("%I64d", &b[i]);
        sort(a, a + n, greater<ll>());
        sort(b, b + n);
        ll ans = 0;
        for(int i = 0; i < n; ++i) ans = (ans + (a[i] % Mod) * b[i]) % Mod;
        printf("%I64d\n", ans);
    }
}
int main() {
    solve();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
#define Mod 998244353
using namespace std;
typedef long long ll;
static const int N = 305;
static const int MAX_N = 2e5 + 5;
ll a[MAX_N], b[MAX_N];
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; ++i){
            ll v;
            scanf("%I64d", &v);
            a[i] = ((v * (i + 1)) % Mod * (n - i)) % Mod;
        }
        for(int i = 0; i < n; ++i) scanf("%I64d", &b[i]);
        sort(a, a + n, greater<ll>());
        sort(b, b + n);
        ll ans = 0;
        for(int i = 0; i < n; ++i) ans = (ans + (a[i] * b[i]) % Mod) % Mod;
        printf("%I64d\n", ans);
    }
}
int main() {
    solve();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
#define Mod 998244353
using namespace std;
typedef long long ll;
static const int N = 305;
static const int MAX_N = 2e5 + 5;
ll a[MAX_N], b[MAX_N];
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; ++i){
            ll v;
            scanf("%I64d", &v);
            a[i] = v * (i + 1) * (n - i) % Mod;
        }
        for(int i = 0; i < n; ++i) scanf("%I64d", &b[i]);
        sort(a, a + n, greater<ll>());
        sort(b, b + n);
        ll ans = 0;
        for(int i = 0; i < n; ++i) ans = (ans + (a[i] % Mod) * b[i]) % Mod;
        printf("%I64d\n", ans);
    }
}
int main() {
    solve();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
#define Mod 998244353
using namespace std;
typedef long long ll;
static const int N = 305;
static const int MAX_N = 2e5 + 5;
ll a[MAX_N], b[MAX_N];
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; ++i){
            ll v;
            scanf("%I64d", &v);
            a[i] = v * (i + 1) * (n - i);
        }
        for(int i = 0; i < n; ++i) scanf("%I64d", &b[i]);
        sort(a, a + n, greater<ll>());
        sort(b, b + n);
        ll ans = 0;
        for(int i = 0; i < n; ++i) ans = (ans + a[i] * b[i]) % Mod;
        printf("%I64d\n", ans);
    }
}
int main() {
    solve();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
#define Mod 998244353
using namespace std;
typedef long long ll;
static const int N = 305;
static const int MAX_N = 2e5 + 5;
ll a[MAX_N], b[MAX_N];
void solve(){
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; ++i){
            ll v;
            scanf("%I64d", &v);
            a[i] = v * (i + 1) * (n - i);
        }
        for(int i = 0; i < n; ++i) scanf("%I64d", &b[i]);
        sort(a, a + n, greater<ll>());
        sort(b, b + n);
        ll ans = 0;
        for(int i = 0; i < n; ++i) ans = (ans + a[i] * b[i]) % Mod;
        printf("%I64d\n", ans);
    }
}
int main() {
    solve();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
#define Mod 998244353
using namespace std;
typedef long long ll;
static const int N = 305;
static const int MAX_N = 2e5 + 5;
ll a[MAX_N], b[MAX_N];
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; ++i){
            ll v;
            scanf("%I64d", &v);
            a[i] = v * (i + 1) * (n - i);
        }
        for(int i = 0; i < n; ++i) scanf("%I64d", &b[i]);
        sort(a, a + n, greater<ll>());
        sort(b, b + n);
        ll ans = 0;
        for(int i = 0; i < n; ++i) ans = (ans + (a[i] % Mod) * b[i]) % Mod;
        printf("%I64d\n", ans);
    }
}
int main() {
    solve();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#define rep(i, s, e) for(int i = s; i < e; ++i)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define P pair<int, int>
#define INF 0x3f3f3f3f
#define Mod 998244353
using namespace std;
typedef long long ll;
static const int N = 305;
static const int MAX_N = 2e5 + 5;
ll a[MAX_N], b[MAX_N];
void solve(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    //ios::sync_with_stdio(false);
    int n;
    while(scanf("%d", &n) != EOF){
        for(int i = 0; i < n; ++i){
            ll v;
            scanf("%I64d", &v);
            a[i] = ((v * (i + 1)) % Mod * (n - i)) % Mod;
        }
        for(int i = 0; i < n; ++i) scanf("%I64d", &b[i]);
        sort(a, a + n, greater<ll>());
        sort(b, b + n);
        ll ans = 0;
        for(int i = 0; i < n; ++i) ans += (a[i] * b[i]) % Mod;
        printf("%I64d\n", ans);
    }
}
int main() {
    solve();
    return 0;
}


#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define LOG2_UP(x)                                                        \
  (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
//template <typename T> using ordered_set = tree<T, null_type, less<T>,
//rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN FOUT
#endif
	int n;
	cin >> n;
	vector<ll> 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(rall(a));
	sort(rall(b));
	ll mod = 998244353;
	ll ans = 0;
	ll cnt[n];
	zero(cnt);
	for (int l = 0; l < n; l++)
	{
		ll i = l + 1;
		cnt[l] = (ll(n) - i + 1) * i;
	}
	//sort(cnt, cnt + n);
	vector<ll> vv;
	for (int i = 0; i < n; i++)
	{
		vv.push_back(a[i]*cnt[i]);
	}
	sort(all(vv));
	ans=0;
	for(int i=0;i<n;i++)
	{
		//_(vv[i]) _(cnt[i]) _n
		ans=(ans+((vv[i]%mod)*(b[i]%mod))%mod)%mod;
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define LOG2_UP(x)                                                        \
  (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
//template <typename T> using ordered_set = tree<T, null_type, less<T>,
//rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN FOUT
#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(rall(a));
	sort(rall(b));
	int mod = 998244353;
	ll ans = 0;
	int cnt[n];
	zero(cnt);
	for (int l = 0; l < n; l++)
	{
		int i = l + 1;
		cnt[l] = (n - i + 1) * i;
	}
	//sort(cnt, cnt + n);
	vector<ll> vv;
	for (int i = 0; i < n; i++)
	{
		vv.push_back(a[i]*cnt[i]);
	}
	sort(all(vv));
	ans=0;
	for(int i=0;i<n;i++)
	{
		//_(vv[i]) _(cnt[i]) _n
		ans+=(vv[i]%mod*b[i])%mod;
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define LOG2_UP(x)                                                        \
  (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
//template <typename T> using ordered_set = tree<T, null_type, less<T>,
//rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN FOUT
#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(rall(a));
	sort(rall(b));
	int mod = 998244353;
	ll ans = 0;
	int cnt[n];
	zero(cnt);
	for (int l = 0; l < n; l++)
	{
		int i = l + 1;
		cnt[l] = (n - i + 1) * i;
	}
	//sort(cnt, cnt + n);
	vector<ll> vv;
	for (int i = 0; i < n; i++)
	{
		vv.push_back(a[i]*cnt[i]);
	}
	sort(all(vv));
	ans=0;
	for(int i=0;i<n;i++)
	{
		//_(vv[i]) _(cnt[i]) _n
		ans=(ans+(vv[i]%mod*b[i])%mod)%mod;
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define LOG2_UP(x)                                                        \
  (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
//template <typename T> using ordered_set = tree<T, null_type, less<T>,
//rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN FOUT
#endif
	int n;
	cin >> n;
	vector<ll> 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(rall(a));
	sort(rall(b));
	int mod = 998244353;
	ll ans = 0;
	ll cnt[n];
	zero(cnt);
	for (int l = 0; l < n; l++)
	{
		int i = l + 1;
		cnt[l] = (n - i + 1) * i;
	}
	//sort(cnt, cnt + n);
	vector<ll> vv;
	for (int i = 0; i < n; i++)
	{
		vv.push_back(a[i]*cnt[i]);
	}
	sort(all(vv));
	ans=0;
	for(int i=0;i<n;i++)
	{
		//_(vv[i]) _(cnt[i]) _n
		ans=(ans+(vv[i]%mod*b[i])%mod)%mod;
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define LOG2_UP(x)                                                        \
  (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
//template <typename T> using ordered_set = tree<T, null_type, less<T>,
//rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN FOUT
#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(rall(a));
	sort(rall(b));
	int mod = 998244353;
	ll ans = 0;
	int cnt[n];
	zero(cnt);
	for (int l = 0; l < n; l++)
	{
		int i = l + 1;
		cnt[l] = (n - i + 1) * i;
	}
	//sort(cnt, cnt + n);
	vector<ll> vv;
	for (int i = 0; i < n; i++)
	{
		vv.push_back(a[i]*cnt[i]);
	}
	sort(all(vv));
	ans=0;
	for(int i=0;i<n;i++)
	{
		//_(vv[i]) _(cnt[i]) _n
		ans+=(vv[i]%mod*b[i])%mod;
		ans%=mod;
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define LOG2_UP(x)                                                        \
  (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
//template <typename T> using ordered_set = tree<T, null_type, less<T>,
//rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN FOUT
#endif
	int n;
	cin >> n;
	vector<ll> 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(rall(a));
	sort(rall(b));
	ll mod = 998244353;
	ll ans = 0;
	ll cnt[n];
	zero(cnt);
	for (int l = 0; l < n; l++)
	{
		int i = l + 1;
		cnt[l] = (n - i + 1) * i;
	}
	//sort(cnt, cnt + n);
	vector<ll> vv;
	for (int i = 0; i < n; i++)
	{
		vv.push_back(a[i]*cnt[i]);
	}
	sort(all(vv));
	ans=0;
	for(int i=0;i<n;i++)
	{
		//_(vv[i]) _(cnt[i]) _n
		ans=(ans+((vv[i]%mod)*b[i])%mod)%mod;
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define __FastIO                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define D cout << "ok" << endl;
#define _(x) cout << #x << "->" << x;
#define _n cout<<endl;
#define __ cout<<" ";
#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)
#define LOG2(x)                                                           \
  (sizeof(unsigned long) * 8 - 1 - __builtin_clzl((unsigned long)(x)))
#define LOG2_UP(x)                                                        \
  (((x) - (1 << FAST_LOG2(x))) ? FAST_LOG2(x) + 1 : FAST_LOG2(x))
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define FIN freopen("in", "r", stdin);
#define FOUT freopen("out", "w", stdout);
#define zero(b) memset(b,0,sizeof b);
#define mone(b) memset(b,-1,sizeof b);
#define gcd(a,b) __gcd(a,b)
#define f first
#define s second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<db, db> pdd;
//template <typename T> using ordered_set = tree<T, null_type, less<T>,
//rb_tree_tag, tree_order_statistics_node_update>;

int main()
{
	__FastIO;
#ifndef ONLINE_JUDGE
	FIN FOUT
#endif
	int n;
	cin >> n;
	vector<ll> 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(rall(a));
	sort(rall(b));
	ll mod = 998244353;
	ll ans = 0;
	ll cnt[n];
	zero(cnt);
	for (int l = 0; l < n; l++)
	{
		int i = l + 1;
		cnt[l] = (n - i + 1) * i;
	}
	//sort(cnt, cnt + n);
	vector<ll> vv;
	for (int i = 0; i < n; i++)
	{
		vv.push_back(a[i]*cnt[i]);
	}
	sort(all(vv));
	ans=0;
	for(int i=0;i<n;i++)
	{
		//_(vv[i]) _(cnt[i]) _n
		ans=(ans+((vv[i]%mod)*(b[i]%mod))%mod)%mod;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>

#define lowbit(a) ((a)&(-(a)))
using namespace std;

void file(bool opt)
{
	if (opt&&fopen("in.txt", "r"))
	{
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	}
}

const long long mod = 998244353;

long long a[200000 + 5];
long long b[200000 + 5];

bool cmp(long long a, long long b)
{
	return a > b;
}
int main()
{
	long long n;
	scanf("%lld", &n);
	for (long long i = 1; i <= n; i++)
	{
		scanf("%lld", &a[i]);
		a[i] = a[i] * (n - i + 1)*i;
	}
	for (int i = 1; i <= n; i++)
		scanf("%lld", &b[i]);
	sort(a + 1, a + 1 + n, cmp);
	sort(b + 1, b + 1 + n);
	long long ans = 0;
	for (int i = 1; i <= n; i++)
		ans = (ans + a[i]%mod * b[i] % mod) % mod;
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

#define lowbit(a) ((a)&(-(a)))
using namespace std;

void file(bool opt)
{
	if (opt&&fopen("in.txt", "r"))
	{
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	}
}

const long long mod = 998244353;

long long a[200000 + 5];
long long b[200000 + 5];

bool cmp(long long a,long long b)
{
	return a > b;
}
int main()
{
	long long n;
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &a[i]);
		a[i] = a[i] * (n - i + 1)*i;
	}
	for (int i = 1; i <= n; i++)
		scanf("%lld", &b[i]);
	sort(a + 1, a+1+n, cmp);
	sort(b + 1, b + 1 + n);
	long long ans = 0;
	for (int i = 1; i <= n; i++)
		ans = (ans + a[i]*b[i] % mod) % mod;
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

#define lowbit(a) ((a)&(-(a)))
using namespace std;

void file(bool opt)
{
	if (opt&&fopen("in.txt", "r"))
	{
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	}
}

const long long mod = 998244353;

struct node {
	int id;
	long long a, b;
}p[200000+5];

long long box[200000 + 5];

bool cmp1(node a, node b)
{
	if (a.a == b.a)
		return a.id < b.id;
	return a.a > b.a;
}
bool cmp2(node a, node b)
{
	return a.id < b.id;
}
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &p[i].a), p[i].id = i;
		p[i].a = p[i].a*1ll*(n - i + 1) *(i);
	}
	for (int i = 1; i <= n; i++)
		scanf("%lld", &box[i]);
	sort(p + 1, p + 1 + n, cmp1);
	sort(box + 1, box + 1 + n);
	for (int i = 1; i <= n; i++)
		p[i].b = box[i];
	sort(p + 1, p + 1 + n, cmp2);

	long long ans = 0;
	for (int i = 1; i <= n; i++)
		ans = (ans + (p[i].a*p[i].b%mod)) % mod;
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

#define lowbit(a) ((a)&(-(a)))
using namespace std;

void file(bool opt)
{
	if (opt&&fopen("in.txt", "r"))
	{
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	}
}

const long long mod = 998244353;

struct node {
	int id;
	long long a, b;
}p[200000+5];

long long box[200000 + 5];

bool cmp1(node a, node b)
{
	return a.a > b.a;
}
bool cmp2(node a, node b)
{
	return a.id < b.id;
}
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &p[i].a), p[i].id = i;
	for (int i = 1; i <= n; i++)
		scanf("%lld", &box[i]);
	sort(p + 1, p + 1 + n, cmp1);
	sort(box + 1, box + 1 + n);
	for (int i = 1; i <= n; i++)
		p[i].b = box[i];
	sort(p + 1, p + 1 + n, cmp2);

	long long ans = 0;
	for (int i = 1; i <= n; i++)
		ans = (ans + (p[i].a*p[i].b%mod)*(n - i + 1) % mod*(i) % mod) % mod;
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

#define lowbit(a) ((a)&(-(a)))
using namespace std;

void file(bool opt)
{
	if (opt&&fopen("in.txt", "r"))
	{
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	}
}

const long long mod = 998244353;

struct node {
	int id;
	long long a, b;
}p[200000+5];

long long box[200000 + 5];

bool cmp1(node a, node b)
{
	if (a.a == b.a)
		return a.id < b.id;
	return a.a > b.a;
}
bool cmp2(node a, node b)
{
	return a.id < b.id;
}
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &p[i].a), p[i].id = i;
		p[i].a *= (n - i + 1) *(i);
	}
	for (int i = 1; i <= n; i++)
		scanf("%lld", &box[i]);
	sort(p + 1, p + 1 + n, cmp1);
	sort(box + 1, box + 1 + n);
	for (int i = 1; i <= n; i++)
		p[i].b = box[i];
	sort(p + 1, p + 1 + n, cmp2);

	long long ans = 0;
	for (int i = 1; i <= n; i++)
		ans = (ans + (p[i].a*p[i].b%mod)) % mod;
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

#define lowbit(a) ((a)&(-(a)))
using namespace std;

void file(bool opt)
{
	if (opt&&fopen("in.txt", "r"))
	{
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	}
}

const long long mod = 998244353;

struct node {
	int id;
	long long a, b;
}p[200000+5];

long long box[200000 + 5];

bool cmp1(node a, node b)
{
	if (a.a == b.a)
		return a.id < b.id;
	return a.a > b.a;
}
bool cmp2(node a, node b)
{
	return a.id < b.id;
}
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &p[i].a), p[i].id = i;
	for (int i = 1; i <= n; i++)
		scanf("%lld", &box[i]);
	sort(p + 1, p + 1 + n, cmp1);
	sort(box + 1, box + 1 + n);
	for (int i = 1; i <= n; i++)
		p[i].b = box[i];
	sort(p + 1, p + 1 + n, cmp2);

	long long ans = 0;
	for (int i = 1; i <= n; i++)
		ans = (ans + (p[i].a*p[i].b%mod)*(n - i + 1) % mod*(i) % mod) % mod;
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

#define lowbit(a) ((a)&(-(a)))
using namespace std;

void file(bool opt)
{
	if (opt&&fopen("in.txt", "r"))
	{
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	}
}

const long long mod = 998244353;

long long a[200000 + 5];
long long b[200000 + 5];

bool cmp(long long a,long long b)
{
	return a > b;
}
int main()
{
	long long n;
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &a[i]);
		a[i] = a[i] * (n - i + 1)%mod*i%mod;
	}
	for (int i = 1; i <= n; i++)
		scanf("%lld", &b[i]);
	sort(a + 1, a+1+n, cmp);
	sort(b + 1, b + 1 + n);
	long long ans = 0;
	for (int i = 1; i <= n; i++)
		ans = (ans + a[i]*b[i] % mod) % mod;
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>

#define lowbit(a) ((a)&(-(a)))
using namespace std;

void file(bool opt)
{
	if (opt&&fopen("in.txt", "r"))
	{
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	}
}

const long long mod = 998244353;

struct node {
	int id;
	long long a;
}p[200000+5];

long long box[200000 + 5];

bool cmp1(node a, node b)
{
	if (a.a == b.a)
		return a.id < b.id;
	return a.a > b.a;
}
int main()
{
	long long n;
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &p[i].a), p[i].id = i;
		p[i].a = p[i].a*1ll*(n - i + 1ll) *(long long)(i);
	}
	for (int i = 1; i <= n; i++)
		scanf("%lld", &box[i]);
	sort(p + 1, p + 1 + n, cmp1);
	sort(box + 1, box + 1 + n);
	long long ans = 0;
	for (int i = 1; i <= n; i++)
		ans = (ans + p[i].a*box[i] % mod) % mod;
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int MAXN=2e5+5;
const int MOD=998244353;
using namespace std;

ll a[MAXN];
int b[MAXN];
int sum=0;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
		a[i]=a[i]*(i+1)*1ll*(n-i);
	}
	for(int i=0;i<n;++i)
		cin>>b[i];
	sort(a,a+n,greater<int>());
	sort(b,b+n);

	for(int i=0;i<n;++i)
		sum=(sum+(a[i]%MOD*1ll*b[i])%MOD)%MOD;
	cout<<sum<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int MAXN=2e5+5;
const ll MOD=998244353;
using namespace std;

typedef struct{
	ll v;
	int index;
}A;
A a[MAXN];
A b[MAXN];
ll c[MAXN];
bool cmp1(A &a, A &b){
	if(a.v==b.v)
		return a.index<b.index;
	return a.v<b.v;
}

bool cmp2(A &a, A &b){
	if(a.v==b.v)
		return a.index<b.index;
	return a.v>b.v;
}
ll sum=0;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i].v;
		a[i].index=i;
	}
	for(int i=0;i<n;++i){
		cin>>b[i].v;
		b[i].index=i;
	}
	sort(a,a+n,cmp1);
	sort(b,b+n,cmp2);
	for(int i=0;i<n;++i)
		c[a[i].index]=(a[i].v*b[i].v)%MOD;
	for(int i=0;i<n;++i)
		for(int j=i;j<n;++j)
			sum=(sum+(n-j)*c[j])%MOD;
	cout<<sum<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int MAXN=2e5+5;
const int MOD=998244353;
using namespace std;

ll a[MAXN];
int b[MAXN];
int sum=0;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
		a[i]=a[i]*(i+1)*1ll*(n-i);
	}
	for(int i=0;i<n;++i)
		cin>>b[i];
	sort(a,a+n,greater<ll>());
	sort(b,b+n);

	for(int i=0;i<n;++i)
		sum=(sum+(a[i]%MOD*1ll*b[i])%MOD)%MOD;
	cout<<sum<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int MAXN=2e5+5;
const ll MOD=998244353;
using namespace std;

typedef struct{
	ll v;
	int index;
}A;
A a[MAXN];
A b[MAXN];
ll c[MAXN];
bool cmp1(A &a, A &b){
	if(a.v==b.v)
		return a.index<b.index;
	return a.v<b.v;
}

bool cmp2(A &a, A &b){
	if(a.v==b.v)
		return a.index<b.index;
	return a.v>b.v;
}
ll sum=0;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i].v;
		a[i].index=i;
	}
	for(int i=0;i<n;++i){
		cin>>b[i].v;
		b[i].index=i;
	}
	sort(a,a+n,cmp1);
	sort(b,b+n,cmp2);
	for(int i=0;i<n;++i)
		c[a[i].index]=a[i].v*b[i].v;
	for(int i=0;i<n;++i)
		for(int j=i;j<n;++j)
			sum=(sum+(n-j)*c[j])%MOD;
	cout<<sum<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int MAXN=2e5+5;
const ll MOD=998244353;
using namespace std;

typedef struct{
	ll v;
	int index;
}A;
A a[MAXN];
A b[MAXN];
ll c[MAXN];
bool cmp1(A &a, A &b){
	if(a.v==b.v)
		return a.index<b.index;
	return a.v<b.v;
}

bool cmp2(A &a, A &b){
	if(a.v==b.v)
		return a.index<b.index;
	return a.v>b.v;
}
ll sum=0;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i].v;
		a[i].index=i;
	}
	for(int i=0;i<n;++i){
		cin>>b[i].v;
		b[i].index=i;
	}
	sort(a,a+n,cmp1);
	sort(b,b+n,cmp2);
	for(int i=0;i<n;++i)
		c[a[i].index]=(a[i].v*b[i].v)%MOD;
	for(int i=0;i<n;++i)
			sum=(sum+(i+1)*1ll*(n-i)*c[i]%MOD)%MOD;
	cout<<sum<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int MAXN=2e5+5;
const ll MOD=998244353;
using namespace std;

ll a[MAXN];
ll b[MAXN];
int sum=0;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
		a[i]=a[i]*(i+1)*1ll*(n-i);
	}
	for(int i=0;i<n;++i)
		cin>>b[i];
	sort(a,a+n,greater<int>());
	sort(b,b+n);

	for(int i=0;i<n;++i)
		sum=(sum+(a[i]%MOD*1ll*b[i])%MOD)%MOD;
	cout<<sum<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int MAXN=2e5+5;
const ll MOD=998244353;
using namespace std;

typedef struct{
	ll v;
	int index;
}A;
A a[MAXN];
A b[MAXN];
ll c[MAXN];
bool cmp1(A &a, A &b){
	if(a.v==b.v)
		return a.index<b.index;
	return a.v<b.v;
}

bool cmp2(A &a, A &b){
	if(a.v==b.v)
		return a.index<b.index;
	return a.v>b.v;
}
ll sum=0;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i].v;
		a[i].index=i;
	}
	for(int i=0;i<n;++i){
		cin>>b[i].v;
		b[i].index=i;
	}
	sort(a,a+n,cmp1);
	sort(b,b+n,cmp2);
	for(int i=0;i<n;++i)
		c[a[i].index]=(a[i].v*b[i].v)%MOD;
	for(int i=0;i<n;++i)
		for(int j=i;j<n;++j)
			sum=(sum+((n-j)*c[j]*1ll)%MOD)%MOD;
	cout<<sum<<"\n";
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int MAXN=2e5+5;
const ll MOD=998244353;
using namespace std;

ll a[MAXN];
ll b[MAXN];
ll c[MAXN];
ll sum=0;
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
		a[i]=a[i]*(i+1)*1ll*(n-i);
	}
	for(int i=0;i<n;++i)
		cin>>b[i];
	sort(a,a+n,greater<int>());
	sort(b,b+n);

	for(int i=0;i<n;++i)
		sum=(sum+(a[i]%MOD*1ll*b[i])%MOD)%MOD;
	cout<<sum<<"\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
const int MOD = 1e9+7; // 888244353;
const int MX = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
	read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
	FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
	FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));

}
/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

ll a[100000];
ll b[100000];
void solve() {
    int n;
    read(n);
    FOR(n) read(a[i]);
    int mod = 998244353;
    FOR(n) read(b[i]);
    sort(b, b + n);
    FOR(n) {
        a[i] = (((i + 1) * (n - i)) % mod) * a[i];
        a[i] %= mod;
    }
    sort(a, a + n);
    reverse(a, a + n);
    ll ans = 0;
    FOR(n) {
        ans += ((((a[i] % mod) * b[i]) % mod));
    }
    print(ans);
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}
//SGGSGGSSGSSSGSGSGSSGSGGSSSGSSGSSGGGGSSGSSGSSGSGGSSSSGGGGSSGSSSSSSSSGSSSSGSGSSSSSSSSGSGSSSSGSSGGSSGSGSSSSSSGSGSSSGGSSGSGSSGSSSSSSGGGSSSGSGSGSGGSGGGSSGSGSSSGSSGGSSGSSGGGGSGSSGSSSSGGSSSSGGSGSSSSSSGSSSGGGSGSGGSSGSSSSSSGGSSSGSSSSGGGSSGSSSGSGGGSSSSGSSSGSGSGGGG


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
const int MOD = 1e9+7; // 888244353;
const int MX = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
	read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
	FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
	FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));

}
/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

ll a[100000];
ll b[100000];
void solve() {
    int n;
    read(n);
    FOR(n) read(a[i]);
    int mod = 998244353;
    FOR(n) read(b[i]);
    sort(b, b + n);
    FOR(n) {
        a[i] *= (i + 1) * (n - i);
        a[i] %= mod;
    }
    sort(a, a + n);
    reverse(a, a + n);
    ll ans = 0;
    FOR(n) {
        ans += ((((a[i] % mod) * b[i]) % mod));
    }
    print(ans);
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}
//SGGSGGSSGSSSGSGSGSSGSGGSSSGSSGSSGGGGSSGSSGSSGSGGSSSSGGGGSSGSSSSSSSSGSSSSGSGSSSSSSSSGSGSSSSGSSGGSSGSGSSSSSSGSGSSSGGSSGSGSSGSSSSSSGGGSSSGSGSGSGGSGGGSSGSGSSSGSSGGSSGSSGGGGSGSSGSSSSGGSSSSGGSGSSSSSSGSSSGGGSGSGGSSGSSSSSSGGSSSGSSSSGGGSSGSSSGSGGGSSSSGSSSGSGSGGGG


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdb;

typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<string> vs;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdb> vpd;

#define mp make_pair
#define ff first
#define ss second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define vv <vector<vector> >
#define mp make_pair
#define str(a, b) substr((a), (b))
#define rr return
#define r0 return 0
#define mi map<int>
#define ms map<string>
#define ml map<ll>
#define all_sum(a,b) accumulate((a),(b),0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
void adskiy_razgon(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
const int MOD = 1e9+7; // 888244353;
const int MX = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
ll nod(ll a, ll b) {
    if(b > a) {
        swap(a, b);
    }
    while(b > 0) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll nok(ll a,ll b) {
    return a * b / nod(a, b);
}
void sp(ll a,double b) {
    cout << fixed << setprecision(a) << b;
}
ll binpow (ll a, ll n) {
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return binpow (a, n - 1) * a;
    }
    else {
        ll b = binpow (a, n / 2);
        return b * b;
    }
}

template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
	read(t...);
}
template<class A> void read(vector<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
string to_string(char c) {
    return string(1, c);
}
string to_string(bool b) {
    return b?"true":"false";
}
string to_string(const char* s) {
    return string(s);
}
string to_string(string s) {
    return s;
}
string to_string(vector<bool> v) {
    string res;
	FOR(sz(v))
        res+=char('0'+v[i]);
    return res;
}

template<size_t S> string to_string(bitset<S> b) {
    string res;
	FOR(S)
        res+=char('0'+b[i]);
    return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
        if(!f)
            res+=' ';
        f=0;
        res+=to_string(x);
    }
    return res;
}
template<class A> void write(A x) {
    cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
    write(h);
    write(t...);
}
void print() {
     write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
    write(h);
    if(sizeof...(t))
        write(' ');
    print(t...);
}

void yes(bool ok) {
    print((ok ? "YES" : "Yes"));
}
void no(bool ok) {
    print((ok ? "NO" : "No"));

}
/*
⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿
⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿
⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿
⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿
⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿
⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿
⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺
⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘
⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆
⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇
⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇
⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇
⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇
⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇
⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀
⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀
⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸
⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼
⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿
⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿
⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿
⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿
*/

ll a[1000000];
ll b[1000000];
void solve() {
    ll n;
    read(n);
    FOR(n) read(a[i]);
    ll mod = 998244353;
    FOR(n) read(b[i]);
    sort(b, b + n);
    FOR(n) {
        a[i] = (((i + 1) * (n - i))) * a[i];
    }
    sort(a, a + n);
    reverse(a, a + n);
    ll ans = 0;
    FOR(n) {
        ans += ((((a[i] % mod) * b[i]) % mod));
        ans %= mod;
    }
    print((ans) % mod);
}
int main(){
    adskiy_razgon();
    ll t = 1;
    //read(t);
    FOR(t) {
        //write("Case ", i, ": ");
        solve();
    }
    r0;
}
//SGGSGGSSGSSSGSGSGSSGSGGSSSGSSGSSGGGGSSGSSGSSGSGGSSSSGGGGSSGSSSSSSSSGSSSSGSGSSSSSSSSGSGSSSSGSSGGSSGSGSSSSSSGSGSSSGGSSGSGSSGSSSSSSGGGSSSGSGSGSGGSGGGSSGSGSSSGSSGGSSGSSGGGGSGSSGSSSSGGSSSSGGSGSSSSSSGSSSGGGSGSGGSSGSSSSSSGGSSSGSSSSGGGSSGSSSGSGGGSSSSGSSSGSGSGGGG