#include <bits/stdc++.h>
using namespace std;
int pr[1000001];
bool prime() {
  pr[0] = 1;
  pr[1] = 1;
  for (long long i = 2; i <= sqrt(100000); i++) {
    if (pr[i] == 0) {
      for (long long j = i * i; j <= 1000000; j += i) {
        pr[j] = 1;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  long long a[n];
  long long b[n];
  vector<pair<long long, long long> > v1;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    long long o = a[i] * (i + 1) * (n - i);
    v1.push_back(make_pair(o, i));
  }
  vector<long long> v2;
  for (long long j = 0; j < n; j++) {
    cin >> b[j];
    v2.push_back(b[j]);
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  int d[n + 1];
  int h = 0;
  reverse(v2.begin(), v2.end());
  for (int i = 0; i < v1.size(); i++) {
    d[v1[i].second] = v2[h];
    h++;
  }
  long long mod = 998244353;
  long long y = 0;
  long long r = 0;
  for (int i = 0; i < n; i++) {
    long long o = v1[i].first;
    long long o1 = v2[i];
    o = o % mod;
    r = (r + (o * o1) % mod) % mod;
  }
  cout << r;
}
