You are given two arrays $a$ and $b$, both of length $n$.

Let's define a function $f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i$.

Your task is to reorder the elements (choose an arbitrary order of elements) of the array $b$ to minimize the value of $\sum\limits_{1 \le l \le r \le n} f(l, r)$. Since the answer can be very large, you have to print it modulo $998244353$. Note that you should minimize the answer but not its remainder.


-----Input-----

The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of elements in $a$ and $b$.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the $i$-th element of $a$.

The third line of the input contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_j \le 10^6$), where $b_j$ is the $j$-th element of $b$.


-----Output-----

Print one integer — the minimum possible value of $\sum\limits_{1 \le l \le r \le n} f(l, r)$ after rearranging elements of $b$, taken modulo $998244353$. Note that you should minimize the answer but not its remainder.


-----Examples-----
Input
5
1 8 7 2 4
9 7 2 9 3

Output
646

Input
1
1000000
1000000

Output
757402647

Input
2
1 3
4 2

Output
20

Difficulty: MEDIUM_HARD

Solutions:
import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = 1
	for _ in range(tc):
		n = ri()
		a = ria()
		b = ria()
		b = sorted(b)[::-1]
		f = []
		for i in range(n):
			f.append((n - i) * (i + 1) * a[i])
		f = sorted(f)
		ans = 0
		for i in range(n):
			ans += f[i] * b[i] % 998244353
			ans = ans % 998244353
		print(ans)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


def gen(n):
	a = []
	if n % 2 == 1:
		k = n - 2
		a = [n]
		while k > 0:
			a.append(a[-1] + k)
			k -= 2
		a = a + a[:n // 2][::-1]
	else:
		k = n - 2
		a = [n]
		while k > 0:
			a.append(a[-1] + k)
			k -= 2
		a = a + a[:n // 2][::-1]
	return a

def f(a, b):
	t = gen(len(a))
	for i in range(len(a)):
		a[i] = a[i] * t[i]
	a = sorted(a, reverse=True)
	b = sorted(b)
	ans = 0
	for i in range(len(a)):
		ans = (ans + a[i] * b[i]) % 998244353
	return ans
n = input()
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
print(f(a, b))


modulus = 998244353
length = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(length):
	a[i] = a[i] * (i + 1) * (length - i)
a.sort()
b.sort(reverse=True)
res = 0
for i in range(length):
	res = (res + a[i] % modulus * b[i] % modulus) % modulus
print(res)


import os
import sys
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
n = Int()
a = array()
b = array()
b.sort(reverse=True)
for i in range(n):
	here = (i + 1) * (n - i)
	a[i] = a[i] * here
a.sort()
ans = 0
for i in range(n):
	ans = (ans + a[i] * b[i]) % M
print(ans)


import math
import time
from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
from queue import PriorityQueue
import sys
MOD = 998244353
n = int(input())
a = list(map(int, stdin.readline().split()))
b = list(map(int, stdin.readline().split()))
b.sort()
for i in range(n):
	a[i] *= (i + 1) * (n - i)
a.sort(reverse=True)
ans = 0
for i in range(n):
	ans += b[i] * a[i] % MOD
print(ans % MOD)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(n):
	A[i] *= (i + 1) * (n - i)
A.sort()
B.sort(reverse=True)
C = []
cnt = 0
M = 998244353
for i in range(n):
	cnt += A[i] * B[i]
	cnt %= M
print(cnt)


n = int(input())
a = [int(i) for i in input().split()]
b = sorted([int(i) for i in input().split()])[::-1]
allv = n * (n + 1) // 2
for i in range(n):
	a[i] *= allv - i * (i + 1) // 2 - (n - 1 - i) * (n - i) // 2
a = sorted(a)
ans = 0
for i in range(n):
	ans += a[i] * b[i]
print(ans % 998244353)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for i in range(n):
	c.append(a[i] * (n - i) * (i + 1))
c.sort()
b.sort(reverse=True)
ans = 0
for i in range(n):
	ans = (ans + c[i] * b[i]) % 998244353
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
newa = [a[i - 1] * i * (n - i + 1) for i in range(1, n + 1)]
newa.sort()
b.sort(reverse=True)
mod = 998244353
t = 0
for i in range(n):
	t += newa[i] * b[i]
	t %= mod
print(t)


from math import sqrt
import sys
mod = 998244353
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [a[i] * (i + 1) * (n - i) for i in range(n)]
a.sort()
b.sort(reverse=True)
res = 0
for i in range(n):
	res = (res + a[i] * b[i]) % mod
print(res)


(n, c) = (int(input()), 0)
a = [int(e) for e in input().split()]
b = sorted([int(e) for e in input().split()], reverse=True)
ans = 0
for i in range(n):
	c += n - 2 * i
	a[i] *= c
a.sort()
for i in range(n):
	ans = (ans + a[i] * b[i]) % 998244353
print(ans)


import sys
DEBUG = False
if DEBUG:
	inf = open('input.txt')
else:
	inf = sys.stdin
N = int(inf.readline().strip())
A = list(map(int, inf.readline().strip().split(' ')))
B = list(map(int, inf.readline().strip().split(' ')))
MOD = 998244353
for (i, v) in enumerate(A):
	multiplier = (i + 1) * (N - i)
	A[i] = A[i] * multiplier
A = sorted(A)
B = sorted(B, reverse=True)
s = 0
for (a, b) in zip(A, B):
	s = (s + a * b % MOD) % MOD
print(s)


n = int(input())
print(sum((ai % 998244353 * bi for (ai, bi) in zip(sorted((x * (i + 1) * (n - i) for (i, x) in enumerate(map(int, input().split())))), sorted(map(int, input().split()), reverse=True)))) % 998244353)


n = int(input())
(*a,) = map(int, input().split())
b = sorted(map(int, input().split()))
a = [a[i] * (i + 1) * (n - i) for i in range(n)]
a.sort()
ans = 0
for i in range(n):
	ans += a[i] * b[n - i - 1]
print(ans % 998244353)


from sys import stdin
input = stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mod = 998244353
b.sort()
cnt = []
for i in range(1, n + 1):
	c = i * (n - i + 1)
	cnt.append(c * a[i - 1])
cnt.sort(reverse=True)
ans = 0
for i in range(n):
	ans += b[i] * cnt[i]
	ans %= mod
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
start = 1
end = n
for i in range(n):
	a[i] *= start * end
	start += 1
	end -= 1
a.sort()
b.sort(reverse=True)
res = 0
for (aa, bb) in zip(a, b):
	res += aa * bb
print(res % 998244353)


n = int(input())
v = [int(x) for x in input().split(' ')]
w = [int(x) for x in input().split(' ')]
for i in range(n):
	v[i] *= (i + 1) * (n - i)
v.sort()
w.sort()
w.reverse()
ans = 0
for i in range(n):
	ans += v[i] * w[i]
print(ans % 998244353)


n = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
mod = 998244353
ans = 0
for i in range(n):
	right = n - i - 1
	left = i
	A[i] = (1 + right) * (1 + left) * A[i]
newB = [[B[i], i] for i in range(n)]
newA = [[A[i], i] for i in range(n)]
sortedB = sorted(newB, reverse=True)
sortedA = sorted(newA)
for i in range(n):
	B[sortedA[i][1]] = sortedB[i]
B = [B[i][0] for i in range(n)]
pref_prod = [1]
pair_prods = []
for i in range(n + 1):
	pdt = pref_prod[-1] * A[i - 1] % mod
	pref_prod.append(pdt)
	if i < n:
		pair_prods.append(A[i] * B[i])
ans = 0
for i in range(n):
	ans %= mod
	ans += pair_prods[i] % mod
print(ans % mod)


from sys import stdin
from sys import stdout
MOD = 998244353
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
b = list(map(int, stdin.readline().split()))
val = [0] * n
b.sort(reverse=True)
for i in range(n):
	val[i] = a[i] * (i + 1) * (n - i)
val.sort()
ans = 0
for i in range(n):
	ans += val[i] * b[i]
	ans %= MOD
print(ans)


N = int(input())
A = [int(a) for a in input().split()]
B = sorted([int(a) for a in input().split()])[::-1]
AA = []
for i in range(N):
	AA.append(A[i] * (i + 1) * (N - i))
AA = sorted(AA)
print(sum([B[i] * AA[i] for i in range(N)]) % 998244353)


n = int(input())
a = []
b = []
s = input().split(' ')
for i in range(n):
	a.append(int(s[i]))
s = input().split(' ')
for i in range(n):
	b.append(int(s[i]))
l = 1
r = n
for i in range(n):
	a[i] = l * r * a[i]
	l += 1
	r -= 1
a.sort()
b.sort()
b.reverse()
ans = 0
mod = 998244353
for i in range(n):
	ans = (ans + a[i] * b[i] % mod) % mod
print(ans)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
mod = 998244353
AX = []
for i in range(n):
	AX.append(A[i] * (i + 1) * (n - i))
AX.sort()
B.sort(reverse=True)
ANS = 0
for i in range(n):
	ANS = (ANS + AX[i] * B[i]) % mod
print(ANS)


mod = 998244353
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
	counts = (i + 1) * (n - i)
	a[i] *= counts
a_order_to_index = sorted(range(n), key=lambda i: a[i])
a_index_to_order = [0] * n
for (order, index) in enumerate(a_order_to_index):
	a_index_to_order[index] = order
b_order_to_index = sorted(range(n), key=lambda i: b[i])
total = 0
for i in range(n):
	cur_a = a[i]
	a_order = a_index_to_order[i]
	b_order = n - 1 - a_order
	cur_b = b[b_order_to_index[b_order]]
	total += cur_a * cur_b
	total %= mod
print(total)


MOD = 998244353

def get_modified(num, pos, n):
	x = min(pos, n - pos + 1)
	return num * x * (n - x + 1)

def solve(a, b):
	n = len(a)
	for (i, num) in enumerate(a):
		a[i] = get_modified(num, i + 1, n)
	a.sort()
	b.sort(reverse=True)
	return sum((num_a * num_b % MOD for (num_a, num_b) in zip(a, b))) % MOD

def main():
	n = int(input())
	a = [int(tok) for tok in input().split()]
	b = [int(tok) for tok in input().split()]
	res = solve(a, b)
	print(res)
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = [a[i] * (i + 1) * (n - i) for i in range(n)]
x.sort()
b.sort(reverse=True)
ans = 0
MOD = 998244353
for i in range(n):
	ans += x[i] * b[i]
	ans %= MOD
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = []
sum1 = 0
for i in range(n):
	a[i] = a[i] * (i + 1) * (n - i)
a.sort()
b.sort(reverse=True)
for j in range(n):
	sum1 += a[j] * b[j]
print(sum1 % 998244353)


from sys import stdin
MOD = 998244353
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
b = sorted(list(map(int, stdin.readline().split())), reverse=True)
val = sorted([a[i] * (i + 1) * (n - i) for i in range(n)])
print(sum([vali * bi % MOD for (vali, bi) in zip(val, b)]) % MOD)


a = int(input())
b = sorted([(int(x) * i + int(x)) * (a - i) for (i, x) in enumerate(input().split())])
c = sorted([int(x) for x in input().split()], reverse=True)
it = 0
for i in range(a):
	it += b[i] * c[i]
	it = it % 998244353
print(it)


from sys import stdin, stdout, stderr
MOD = 998244353
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
b = sorted(map(int, stdin.readline().split()))
b.reverse()
a = [a[i] * (i + 1) * (n - i) for i in range(n)]
a.sort()
stdout.write('%d' % (sum([a[i] * b[i] for i in range(n)]) % MOD))


def solve(A, B, n):
	mod = 998244353
	A = [A[i] * (i + 1) * (n - i) for i in range(n)]
	A.sort()
	B.sort(reverse=True)
	f = [A[i] * B[i] % mod for i in range(n)]
	sm = sum(f) % mod
	return sm
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = solve(A, B, n)
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
	a[i] *= (i + 1) * (n - i)
a.sort()
b.sort(reverse=True)
ans = [x * y for (x, y) in zip(a, b)]
print(sum(ans) % 998244353)


import sys
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
	a[i] *= (i + 1) * (n - i)
a = sorted(a)
b = sorted(b)[::-1]
s = 0
for i in range(n):
	s = (s + a[i] * b[i]) % 998244353
print(s)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for i in range(n):
	c.append(a[i] * (i + 1) * (n - i))
b.sort(reverse=True)
c.sort()
d = [c[i] * b[i] for i in range(n)]
print(sum(d) % 998244353)


IN = input
rint = lambda : int(IN())
rmint = lambda : map(int, IN().split())
rlist = lambda : list(rmint())
n = rint()
a = rlist()
b = rlist()
b.sort()
b.reverse()
for i in range(n):
	a[i] *= (i + 1) * (n - i)
a.sort()
ans = 0
for i in range(n):
	ans += b[i] * a[i]
	ans %= 998244353
print(ans)


n = int(input())
a = list(map(int, input().split()))
aord = sorted([i for i in range(n)], key=lambda x: a[x] * (n - x) * (x + 1))
b = sorted(map(int, input().split()))[::-1]
new_b = [0] * n
for i in range(n):
	new_b[aord[i]] = b[i]
b = new_b
m = 998244353
c = [0] * n
for i in range(n):
	c[i] = a[i] * b[i] % m
ans = 0
for i in range(n):
	u = c[i] * (n - i) * (i + 1) % m
	ans = (ans + u) % m
print(ans)


from sys import stdin, stdout
for _ in range(1):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = list(map(int, stdin.readline().split()))
	aa = []
	for i in range(n):
		occ = (i + 1) * (n - i)
		aa += [occ * a[i]]
	aa.sort()
	b.sort(reverse=True)
	M = 998244353
	ans = 0
	for (a1, b1) in zip(aa, b):
		ans = (ans + a1 * b1) % M
	print(ans)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
for i in range(n):
	a[i] *= (i + 1) * (n - i)
a.sort()
b.sort()
a.reverse()
ans = 0
mod = 998244353
for i in range(n):
	ans += a[i] * b[i]
	ans %= mod
print(ans)


from sys import stdin, stdout
MOD = 998244353

def __starting_point():
	n = int(stdin.readline())
	A = [int(x) for x in stdin.readline().rstrip().split()]
	B = [int(x) for x in stdin.readline().rstrip().split()]
	for (i, a) in enumerate(A):
		A[i] *= (i + 1) * (n - i)
	A.sort(reverse=True)
	B.sort()
	res = 0
	for (a, b) in zip(A, B):
		res += a * b
		res %= MOD
	print(res)
__starting_point()


n = int(input())
IN = input().split()
A = [0] * n
for i in range(0, n):
	A[i] = int(IN[i])
IN = input().split()
B = [0] * n
for i in range(0, n):
	B[i] = int(IN[i])
for i in range(1, n + 1):
	A[i - 1] *= i * (n - i + 1)
A.sort()
B.sort()
ans = 0
mod = 998244353
for i in range(0, n):
	ans += B[i] * A[n - i - 1]
	ans %= mod
print(ans)


n = int(input())
a = [int(z) for z in input().split()]
b = [int(z) for z in input().split()]
c = []
for i in range(1, n + 1):
	times = i * (n - i) + i
	const = times * a[i - 1]
	c.append(const)
c.sort(reverse=True)
b.sort()
res = 0
for i in range(n):
	res += c[i] * b[i]
print(res % 998244353)


def main():
	R = lambda : list(map(int, input().split()))
	n = int(input())
	a = sorted([num * (idx + 1) * (n - idx) for (idx, num) in enumerate(R())])
	b = sorted(R(), reverse=True)
	print(sum((x * y for (x, y) in zip(a, b))) % 998244353)
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
bc = []
(i, j) = (1, len(a))
for k in range(len(a)):
	bc.append(i * j)
	i += 1
	j -= 1
c = []
for (aa, bb) in zip(a, bc):
	c.append(aa * bb)
c.sort()
b.sort(reverse=True)
d = []
for (aa, bb) in zip(c, b):
	d.append(aa * bb)
print(sum(d) % 998244353)


import sys
input = sys.stdin.readline
rInt = lambda : int(input())
mInt = lambda : map(int, input().split())
rLis = lambda : list(map(int, input().split()))
n = rInt()
(a, b) = (rLis(), rLis())
for i in range(n):
	a[i] *= (i + 1) * (n - i)
a.sort()
b.sort(reverse=True)
out = 0
for i in range(n):
	out += a[i] * b[i]
	out %= 998244353
print(out)


MOD = 998244353
n = int(input())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
for i in range(n):
	a[i] = a[i] * (n - i) * (i + 1)
a.sort(reverse=True)
b.sort()
res = 0
for i in range(n):
	a[i] %= MOD
	res += a[i] * b[i] % MOD
	res %= MOD
print(res)


n = int(input())
arr1 = [int(x) for x in input().split()]
arr2 = [int(x) for x in input().split()]
for i in range(n):
	arr1[i] = arr1[i] * (i + 1) * (n - i)
arr1.sort()
arr2.sort(reverse=True)
s = 0
for i in range(n):
	s += arr1[i] * arr2[i]
print(s % 998244353)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 998244353
n = int(input())
l = list(map(int, input().split()))
l1 = list(map(int, input().split()))
t = 1
t1 = n
for i in range(n):
	l[i] *= t * t1
	t += 1
	t1 -= 1
l.sort()
l1.sort(reverse=True)
su = 0
for i in range(n):
	sr = l[i] % mod * (l1[i] % mod) % mod
	su = (su % mod + sr) % mod
print(su)


def sum_functions(n, A, B):
	A.sort()
	B.sort(reverse=True)
	temp_sum = 0
	for i in range(n):
		A[i] = A[i] % 998244353
		temp_sum += A[i] * B[i]
	return temp_sum % 998244353
n = int(input())
lsA = list(map(int, input().split()))
lsB = list(map(int, input().split()))
for i in range(n):
	lsA[i] = lsA[i] * (i + 1) * (n - i)
print(sum_functions(n, lsA, lsB))


n = int(input())
a = list(map(int, input().split()))
a1 = sorted([a[i] * (n - i) * (i + 1) for i in range(n)])
b = sorted(list(map(int, input().split())), key=lambda x: -x)
ans = 0
c = 998244353
for i in range(n):
	ans = (ans + a1[i] * b[i]) % c
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
counter = 0
b.sort(reverse=True)
c = []
for i in range(n):
	c.append(a[i] * b[i])
tot = []
for i in range(n):
	x = min(i, n - i - 1) + 1
	tot.append((x * (x + 1) + (n - 2 * x) * x) * a[i])
tot.sort()
for i in range(n):
	counter += tot[i] * b[i]
print(counter % 998244353)


def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
n = ri()
a = rl()
b = rl()
b.sort(reverse=True)
A = [x * (i + 1) * (n - i) for (i, x) in enumerate(a)]
A.sort()
s = 0
for (ai, bi) in zip(A, b):
	s = (s + ai % 998244353 * bi) % 998244353
print(s)


n = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
for i in range(n):
	A[i] = A[i] * (i + 1) * (n - i)
A.sort()
B.sort(reverse=True)
(ans, mod) = (0, 998244353)
for i in range(n):
	ans = (ans + A[i] * B[i]) % mod
print('%d\n' % (ans % mod))


import operator
n = int(input())
a = list(map(int, input().strip().split()))
a = sorted([a[i] * (i + 1) * (n - i) for i in range(n)])
b = sorted(map(int, input().strip().split()), reverse=True)
answer = sum(map(operator.mul, a, b))
print(answer % 998244353)


n = int(input())
ar = list(map(int, input().split()))
br = list(map(int, input().split()))
for i in range(n):
	ar[i] *= (n - i) * (i + 1)
ar.sort()
br.sort(reverse=True)
ans = 0
for i in range(n):
	ans += ar[i] * br[i]
print(ans % 998244353)


import sys
input = sys.stdin.readline
mod = int(998244353)
n = int(input())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
for i in range(n):
	arr[i] = arr[i] * ((i + 1) * (n - i))
arr.sort()
brr.sort(reverse=True)
s = 0
for i in range(n):
	s = (s + arr[i] * brr[i]) % mod
print(s)


n = int(input())
(*a,) = map(int, input().split())
(*b,) = map(int, input().split())
coefs = []
for i in range(len(a)):
	coefs.append(a[i] * (i + 1) * (n - i))
coefs.sort()
b.sort(reverse=True)
mod = 998244353
ans = 0
for (x, y) in zip(coefs, b):
	ans += x * y % mod
	ans %= mod
print(ans)


mod = 998244353
n = int(input())
l = list(map(int, input().split()))
r = list(map(int, input().split()))
m = list(map(lambda i: (i + 1) * (n - i), (i for i in range(n))))
l = [l[i] * m[i] for i in range(n)]
l.sort()
r.sort(reverse=True)
s = sum((l[i] % mod * (r[i] % mod) % mod for i in range(n))) % mod
print(s)


import sys
input = sys.stdin.readline
from collections import deque
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
cnt = []
for i in range(n):
	left = i
	right = n - 1 - i
	cur = n * (n + 1) // 2
	cur -= left * (left + 1) // 2
	cur -= right * (right + 1) // 2
	cnt.append(cur)
c = [0] * n
for i in range(n):
	c[i] = a[i] * cnt[i]
c.sort(reverse=True)
b.sort()
ans = 0
mod = 998244353
for i in range(n):
	ans += c[i] * b[i]
	ans %= mod
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
	a[i] *= (i + 1) * (n - i)
a = sorted(a)
b.sort(reverse=True)
mod = 998244353
sum1 = 0
for i in range(n):
	sum1 += a[i] * b[i]
	sum1 = sum1 % mod
print(sum1)


from sys import stdin

def input():
	return stdin.readline()[:-1]
import math
MOD = 998244353
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
plot = [0] * n
plot[0] = n
increment = 2 * ((n + 1) // 2 - 1) - n % 2
for i in range(1, (n + 1) // 2 - n % 2 + 1):
	plot[i] = plot[i - 1] + increment
	increment -= 2
for i in range(n - 1, n // 2 - n % 2, -1):
	plot[i] = plot[n - i - 1]
for i in range(n):
	plot[i] *= a[i]
plot.sort(reverse=True)
b.sort()
output = 0
for i in range(n):
	output += plot[i] % MOD * (b[i] % MOD) % MOD
output = output % MOD
print(output)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from math import sqrt, pi, ceil, log, inf, gcd, floor
from itertools import combinations, permutations
from bisect import *
from fractions import Fraction
from heapq import *
from random import randint

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(map(int, input().split()), reverse=True)
	(ans, mod, c) = (0, 998244353, [])
	for i in range(n):
		c.append(a[i] * (i + 1) * (n - i))
	c.sort()
	ans = 0
	for i in range(n):
		ans = (ans + b[i] * c[i]) % mod
	print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


Mod = 998244353
n = int(input())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
b.sort(reverse=True)
val = [a[i - 1] * i * (n - i + 1) for i in range(1, n + 1)]
val.sort()
f = 0
for i in range(n):
	f += val[i] * b[i]
print(f % Mod)


n = int(input())
a = list(map(int, input().split()))
b = sorted(list(map(int, input().split())), reverse=True)
val = sorted([a[i] * (n - i) * (i + 1) for i in range(n)])
ans = 0
for i in range(n):
	ans += val[i] * b[i]
	ans %= 998244353
print(ans)


for _ in range(1):
	n = int(input())
	arr1 = [int(i) for i in input().split()]
	arr2 = [int(i) for i in input().split()]
	val = []
	for i in range(n):
		val.append(arr1[i] * (i + 1) * (n - i))
	val.sort()
	arr2.sort()
	su = 0
	for i in range(n):
		su += val[i] * arr2[n - i - 1]
	print(su % 998244353)


n = int(input())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
arr2.sort(reverse=True)
for i in range(1, n + 1):
	arr1[i - 1] = arr1[i - 1] * i * (n - i + 1)
arr1.sort()
sum = arr1[0] * arr2[0]
for i in range(1, n):
	sum += arr1[i] * arr2[i]
print(sum % 998244353)


ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
si = lambda : input()
n = ii()
a = sorted([x * (i + 1) * (n - i) for (i, x) in enumerate(li())])
b = sorted(li(), reverse=True)
print(sum((i * j for (i, j) in zip(a, b))) % 998244353)


mod = 998244353
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
	counts = (i + 1) * (n - i)
	a[i] *= counts
a.sort()
b.sort(reverse=True)
total = 0
for i in range(n):
	total += a[i] * b[i]
	total %= mod
print(total)


def minimizeLR(n, a, b):
	for i in range(0, n):
		a[i] = a[i] * (i + 1) * (n - i)
	min_vals = [a * b for (a, b) in zip(reversed(sorted(a)), sorted(b))]
	print(sum(min_vals) % 998244353)
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
minimizeLR(n, a, b)


n = int(input())
ar = list(map(int, input().split()))
br = list(map(int, input().split()))
MOD = 998244353
for i in range(n):
	ar[i] *= (i + 1) * (n - i)
ar = sorted(ar)
br = sorted(br)
ans = 0
for i in range(n):
	ans = (ans + ar[i] * br[n - i - 1]) % MOD
print(ans)


def go():
	MOD = 998244353
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	c = [None] * n
	b = sorted([int(i) for i in input().split(' ')], reverse=True)
	for i in range(n):
		a[i] = a[i] * (n - i) * (i + 1)
	a.sort()
	total = 0
	for i in range(n):
		total = (total + a[i] * b[i]) % MOD
	return total
print(go())


MOD = 998244353

def main():
	n = int(input())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	v = [a[i] * (i + 1) * (n - i) for i in range(len(a))]
	b.sort()
	b = b[::-1]
	v.sort()
	ans = 0
	for i in range(len(b)):
		ans += b[i] % MOD * (v[i] % MOD) % MOD
		ans %= MOD
	print(ans)
main()


from bisect import bisect_right as br
from bisect import bisect_left as bl
from collections import *
from itertools import *
import functools
import sys
from math import *
from decimal import *
from copy import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = 10 ** 6 + 10
MOD = 10 ** 9 + 7

def isprime(n):
	n = abs(int(n))
	if n < 2:
		return False
	if n == 2:
		return True
	if not n & 1:
		return False
	for x in range(3, int(n ** 0.5) + 1, 2):
		if n % x == 0:
			return False
	return True

def mhd(a, b, x, y):
	return abs(a - x) + abs(b - y)

def numIN(x=' '):
	return map(int, sys.stdin.readline().strip().split(x))

def charIN(x=' '):
	return sys.stdin.readline().strip().split(x)

def arrIN():
	return list(numIN())

def dis(x, y):
	a = y[0] - x[0]
	b = x[1] - y[1]
	return (a * a + b * b) ** 0.5

def lgcd(a):
	g = a[0]
	for i in range(1, len(a)):
		g = math.gcd(g, a[i])
	return g

def ms(a):
	msf = -MAX
	meh = 0
	st = en = be = 0
	for i in range(len(a)):
		meh += a[i]
		if msf < meh:
			msf = meh
			st = be
			en = i
		if meh < 0:
			meh = 0
			be = i + 1
	return (msf, st, en)

def res(ans, t):
	print('Case #{}: {} {}'.format(t, ans[0], ans[1]))

def divi(n):
	l = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l
n = int(input())
a = arrIN()
b = arrIN()
c = [0] * n
b.sort()
for i in range(n):
	a[i] *= (i + 1) * (n - i)
l = [[i, a[i]] for i in range(n)]
l.sort(key=lambda x: x[1], reverse=True)
j = 0
for i in l:
	c[i[0]] = b[j]
	j += 1
ans = 0
for i in range(n):
	ans += a[i] * c[i] % 998244353
print(ans % 998244353)


from functools import reduce
base = 998244353
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = list(map(lambda x: x[1] * (x[0] + 1) * (n - x[0]), enumerate(a)))
a.sort()
b.sort()
b.reverse()
res = map(lambda x: x[0] * x[1] % base, zip(a, b))
print(reduce(lambda acc, x: (acc + x) % base, res))


import sys
import math
import collections
from pprint import pprint as pp
mod = 998244353
MAX = 10 ** 10

def vector(size, val=0):
	vec = [val for i in range(size)]
	return vec

def matrix(rowNum, colNum, val=0):
	mat = []
	for i in range(rowNum):
		collumn = [val for j in range(colNum)]
		mat.append(collumn)
	return mat
n = int(input())
(a, b) = (list(map(int, input().split())), list(map(int, input().split())))
for i in range(n):
	a[i] *= (i + 1) * (n - i)
a.sort()
b.sort()
ans = 0
for i in range(n):
	ans = (ans + a[i] * b[n - i - 1]) % mod
print(ans)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
mod = 998244353
L = [0] * n
for i in range(n):
	j = n - i
	L[i] = (n - i) * (i + 1)
C = [0] * n
for i in range(n):
	C[i] = L[i] * A[i]
C.sort()
B.sort(reverse=True)
ans = 0
for i in range(n):
	c = C[i] % mod
	b = B[i] % mod
	ans += c * b % mod
print(ans % mod)


M = 998244353

def min_sum(a, b, n):
	a = [-ai * (i + 1) * (n - i) for (i, ai) in enumerate(a)]
	a = sorted(a)
	b = sorted(b)
	s = sum((-ai * bi % M for (ai, bi) in zip(a, b)))
	return s % M
n = int(input())
a = map(int, input().split())
b = map(int, input().split())
s = min_sum(a, b, n)
print(s)


import collections
import time
import os
import sys
import bisect
import heapq
from typing import List

def solve(N, A, B):
	MOD = 998244353
	C = [A[i - 1] * i * (N - i + 1) for i in range(1, N + 1)]
	C.sort()
	B.sort(reverse=True)
	ans = 0
	for (b, c) in zip(B, C):
		ans += b * c
		ans %= MOD
	return ans
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
print(solve(N, A, B))


import sys

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def __next__(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = self.next_line()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_line(self):
		return sys.stdin.readline().split()

	def next_ints(self):
		return [int(x) for x in sys.stdin.readline().split()]

	def next_int(self):
		return int(next(self))

	def solve(self):
		n = self.next_int()
		a = self.next_ints()
		b = sorted(self.next_ints())[::-1]
		for i in range(0, n):
			mm = (i + 1) * (n - i)
			a[i] *= mm
		a = sorted(a)
		mod = 998244353
		c = [a[i] * b[i] % mod for i in range(0, n)]
		print(sum(c) % mod)

def __starting_point():
	Main().solve()
__starting_point()


n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
mult = []
mod = 998244353
for i in range(n):
	mult.append(a[i] * (i + 1) * (n - i))
mult = sorted(mult)
b = sorted(b, reverse=True)
res = 0
for i in range(n):
	res = res + mult[i] % mod * b[i] % mod % mod
print(res % mod)


import sys
import collections
import math
import heapq
from operator import itemgetter

def getint():
	return int(input())

def getints():
	return [int(x) for x in input().split(' ')]
n = getint()
a = getints()
b = getints()
modVal = 998244353
a = [a[i] * (n - i) * (i + 1) for i in range(n)]
a.sort()
b.sort(reverse=True)
res = 0
for i in range(n):
	res = (res + a[i] % modVal * (b[i] % modVal)) % modVal
print(str(res))


M = 998244353
n = int(input())
ch = [0 for i in range(n)]
for i in range(n // 2 + n % 2):
	ch[i] = (i + 1) * (n - i)
i = n - 1
while i >= 0:
	ch[i] = ch[n - i - 1]
	i -= 1
ar = list(map(int, input().split()))
br = list(map(int, input().split()))
for i in range(n):
	ar[i] = ar[i] * ch[i]
ar.sort()
br.sort()
br.reverse()
ans = 0
for i in range(n):
	ans += ar[i] * br[i]
print(ans % M)


import math
from collections import defaultdict
import sys

def main():
	MOD = 998244353
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	pos = {}
	for (i, val) in enumerate(a):
		pos[val] = i
	b.sort(reverse=True)
	a_sorted = sorted([(a[i] * (i + 1) * (n - i), i) for i in range(n)])
	res = 0
	for i in range(n):
		x = a_sorted[i][0] % MOD
		x = x * b[i] % MOD
		res = (res + x) % MOD
	print(res)
main()


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
MOD = 998244353
for i in range(n):
	a[i] *= (i + 1) * (n - i)
b = list(map(int, input().split()))
a.sort()
b.sort(reverse=True)
ans = 0
for (ai, bi) in zip(a, b):
	ans += ai * bi
	ans %= MOD
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
for i in range(n):
	a[i] *= (i + 1) * (n - i)
b = [int(i) for i in input().split()]
a.sort()
b.sort(reverse=1)
ans = 0
for (g, h) in zip(a, b):
	ans += g * h % 998244353
	ans %= 998244353
print(ans)


CONST = 998244353
n = int(input())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
for i in range(n):
	arr1[i] = arr1[i] * (i + 1) * (n - i)
print(sum([x * y % CONST for (x, y) in zip(sorted(arr1), sorted(arr2, reverse=True))]) % CONST)


R = lambda : map(int, input().split())
n = int(input())
a = list(R())
b = list(R())
N = 998244353
val = []
for j in range(n):
	i = j + 1
	val.append(a[j] * i * (n - i + 1))
val.sort()
b.sort(reverse=True)
s = 0
for i in range(n):
	m = val[i] % N * b[i] % N % N
	s = (s + m) % N
print(s)


import math
import bisect
import heapq
from collections import defaultdict

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, x, y) = egcd(b % a, a)
		return (g, y - b // a * x, x)

def mulinv(b, n):
	(g, x, _) = egcd(b, n)
	if g == 1:
		return x % n

def isprime(n):
	for d in range(2, int(math.sqrt(n)) + 1):
		if n % d == 0:
			return False
	return True

def argsort(ls):
	return sorted(range(len(ls)), key=ls.__getitem__)

def f(p=0):
	if p == 1:
		return map(int, input().split())
	elif p == 2:
		return list(map(int, input().split()))
	elif p == 3:
		return list(input())
	else:
		return int(input())
n = f()
a = f(2)
b = sorted(f(2))[::-1]
cl = []
for i in range(n):
	cl.append(a[i] * (i + 1) * (n - i))
ind = argsort(cl)
dl = [0] * n
for i in range(n):
	dl[ind[i]] = i
const = 998244353
res = 0
for i in range(n):
	res += b[dl[i]] * cl[i]
	if res > const:
		res = res % const
print(res)


from math import *
from collections import *
import sys
sys.setrecursionlimit(10 ** 9)
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b.sort(reverse=True)
ans = 0
mod = 998244353
for i in range(n):
	a[i] = (n - i) * (i + 1) * a[i]
a.sort()
for i in range(n):
	ct = b[i] % mod * (a[i] % mod) % mod
	ans = (ans + ct) % mod
print(ans)


from sys import stdin
input = stdin.readline

def gen(n):
	a = []
	k = n - 2
	a = [n]
	while k > 0:
		a.append(a[-1] + k)
		k -= 2
	a = a + a[:n // 2][::-1]
	return a

def f(a, b):
	t = gen(len(a))
	for i in range(len(a)):
		a[i] = a[i] * t[i]
	a = sorted(a, reverse=True)
	b = sorted(b)
	ans = 0
	for i in range(len(a)):
		ans = (ans + a[i] * b[i]) % 998244353
	return ans
n = input()
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
print(f(a, b))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
MOD = 998244353
val = []
for i in range(n):
	val.append(a[i] * (i + 1) * (n - i))
val.sort()
b.sort(reverse=True)
sum = 0
for i in range(n):
	sum = (sum + val[i] * b[i]) % MOD
print(sum % MOD)


N = int(input())
A = input()
A = [int(num) for num in A.split(' ')]
B = input()
B = [int(num) for num in B.split(' ')]
B.sort()
m = [0] * N
for i in range(N):
	m[i] = (i + 1) * (N - i) * A[i]
m.sort(reverse=True)
res = 0
for i in range(N):
	res += m[i] * B[i]
print(res % 998244353)


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def computeLPSArray(pat, M, lps):
	len = 0
	lps[0]
	i = 1
	while i < M:
		if pat[i] == pat[len]:
			len += 1
			lps[i] = len
			i += 1
		elif len != 0:
			len = lps[len - 1]
		else:
			lps[i] = 0
			i += 1
mod = 998244353
t = 1
while t:
	t -= 1
	n = int(fastinput())
	j = 0
	a = []
	for i in fastinput().split():
		a.append(int(i) * (j + 1) * (n - j))
		j += 1
	a.sort()
	b = list(map(int, fastinput().split()))
	b.sort(reverse=True)
	ans = 0
	for i in range(n):
		ans = (ans + a[i] * b[i]) % mod
	print(ans % mod)


N = int(input())
arr = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
arr2.sort()
for i in range(N):
	arr[i] *= i + 1
	arr[i] *= N - i
arr.sort()
arr = arr[::-1]
res = 0
for i in range(N):
	res += arr[i] * arr2[i]
	res %= 998244353
print(res)


mod = 998244353

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = [(i + 1) * (n - i) * a[i] for i in range(n)]
	c.sort(reverse=True)
	b.sort()
	ans = sum((b[i] * c[i] for i in range(n))) % mod
	print(ans)
	return
main()


def getN():
	return int(input())

def getList():
	return list(map(int, input().split()))
MOD = 998244353
n = getN()
anums = getList()
bnums = getList()
anums = [a * (i + 1) * (n - i) for (i, a) in enumerate(anums)]
anums.sort()
bnums.sort(reverse=True)
ans = 0
for (a, b) in zip(anums, bnums):
	ans += a * b
	ans = ans % MOD
print(ans)


n = int(input())
a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
big_num = 998244353
pair = []
for i in range(n):
	a1[i] *= (n - i) * (i + 1)
for i in range(n):
	pair.append((a1[i], i))
pair.sort()
lol = []
for i in range(n):
	lol.append(0)
for i in range(n):
	lol[pair[i][1]] = i
a2.sort(reverse=True)
sum = 0
sum_list = []
for i in range(n):
	prod = a1[i] * a2[lol[i]]
	sum_list.append(prod % big_num)
for i in range(n):
	sum = sum + sum_list[i]
	sum = sum % big_num
print(sum)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mn = [a[i] * (i + 1) * (n - i) for i in range(n)]
mn.sort()
b.sort()
b.reverse()
wyn = 0
for i in range(n):
	wyn = (wyn + b[i] * mn[i]) % 998244353
print(wyn)


def main():
	R = lambda : list(map(int, input().split()))
	n = int(input())
	a = sorted([num * (idx + 1) * (n - idx) for (idx, num) in enumerate(R())])
	b = sorted(R(), reverse=True)
	ans = 0
	for (idx, num) in enumerate(a):
		ans = (ans + num * b[idx]) % 998244353
	print(ans)
main()


n = int(input())
a = list(map(int, input().split()))
b = sorted(map(int, input().split()), reverse=True)
c = sorted((v * (i + 1) * (n - i) for (i, v) in enumerate(a)))
r = 0
for (u, v) in zip(b, c):
	r += u * v
	r %= 998244353
print(r)


from collections import defaultdict, Counter
from functools import lru_cache
from heapq import heappush, heappop
from math import gcd, floor, ceil
from sys import stdin, stdout

def ilist():
	return [int(x) for x in stdin.readline().strip().split(' ')]

def iint():
	return int(stdin.readline().strip())

def istr():
	return stdin.readline().strip()

def solve(a, b):
	p = 998244353
	n = len(a)
	a = sorted([(a[i] * (n - i) * (i + 1), i) for i in range(n)])
	b.sort(reverse=True)
	ans = 0
	for i in range(n):
		ans += a[i][0] * b[i]
		ans %= p
	return ans
n = iint()
a = ilist()
b = ilist()
print(solve(a, b))


R = lambda : map(int, input().split())
(m, n, a, b) = (998244353, int(input()), list(R()), list(R()))
for i in range(n):
	a[i] *= (i + 1) * (n - i)
a.sort()
b.sort(reverse=True)
print(sum([x * y % m for (x, y) in zip(a, b)]) % m)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
qw = []
for i in range(len(a)):
	qw.append(a[i] * (i + 1) * (len(a) - i))
qw.sort()
b.sort()
b.reverse()
c = 0
for i in range(n):
	c = c + qw[i] * b[i]
print(c % 998244353)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = 1
for i in range(n):
	A[i] = A[i] * k * (n + 1 - k)
	k = k + 1
A.sort()
B.sort(reverse=True)
mod = 998244353
Ans = 0
for i in range(n):
	Ans = Ans + B[i] * A[i]
	Ans = Ans % mod
print(Ans)


import sys
import math
import collections
import heapq
import decimal
input = sys.stdin.readline
n = int(input())
a = [int(i) for i in input().split()]
b = sorted([int(i) for i in input().split()], reverse=True)
mod = 998244353
p = []
for i in range(n):
	a[i] = a[i] * (i + 1) * (n - i)
a.sort()
for i in range(n):
	p.append(a[i] * b[i] % mod)
s = 0
for i in range(n):
	s = (s + p[i]) % mod
print(s)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
	a[i] = a[i] * (n - i) * (i + 1)
a.sort()
b.sort(reverse=True)
p = 0
for i in range(n):
	p += a[i] * b[i]
print(p % 998244353)


n = int(input())
print(sum((a * b for (a, b) in zip(sorted((x * i * (n - i + 1) for (i, x) in enumerate(map(int, input().split()), 1))), sorted(map(int, input().split()), reverse=True)))) % 998244353)


from math import gcd, sqrt

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def ii():
	return int(input())
mod = 998244353

def add(a, b):
	s = (a % mod + b % mod) % mod
	return s

def mul(a, b):
	s = a % mod * b % mod % mod
	return s
n = ii()
a = li()
b = li()
b.sort(reverse=True)
for i in range(n):
	a[i] = a[i] * (i + 1) * (n - i)
a.sort()
s = 0
for i in range(n):
	s = add(s, mul(a[i], b[i]))
print(s)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mod = 998244353
for i in range(n):
	a[i] = a[i] * (i + 1) * (n - i)
a.sort()
b.sort(key=lambda P: -1 * P)
Min = 0
for i in range(n):
	Min += a[i] * b[i]
	Min %= mod
print(Min)


MOD = 998244353
n = int(input())
a = [int(i) for i in input().split()]
b = [int(j) for j in input().split()]
for i in range(n):
	a[i] = a[i] * (i + 1) * (n - i)
a.sort()
b.sort()
ans = 0
for i in range(n):
	ans = ans + a[i] * b[n - i - 1]
	ans = ans % MOD
print(ans)


n = int(input())
a = [[q * (n - q1) * (q1 + 1), q1] for (q1, q) in enumerate(list(map(int, input().split())))]
s = sorted(list(map(int, input().split())), reverse=True)
d = sorted(a)
ans = [0] * len(s)
for q in range(n):
	ans[d[q][1]] = s[q]
print(sum((a[q][0] * ans[q] for q in range(n))) % 998244353)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(n):
	A[i] *= (i + 1) * (n - i)
A.sort()
B.sort()
B.reverse()
S = sum((A[i] * B[i] for i in range(n)))
print(S % 998244353)


n = int(input())
a = list(map(int, input().split()))
b = sorted(list(map(int, input().split())), reverse=True)
curr = n
x = n
for i in range(n // 2):
	a[i] *= curr
	a[n - i - 1] *= curr
	x -= 2
	curr += x
if n % 2 == 1:
	a[n // 2] *= curr
a.sort()
mod = 998244353
ans = 0
for i in range(n):
	ans += a[i] * b[i]
	ans %= mod
print(ans)


n = int(input())
x = 998244353
(a, b) = (list(map(int, input().split())), list(map(int, input().split())))
a1 = a[:]
for i in range(n):
	a1[i] = a[i] * ((n - i) * (i + 1))
b1 = sorted(b, reverse=True)
a1 = sorted(a1)
d = {}
for i in range(n):
	d[a1[i]] = b1[i]
c = []
for i in range(n):
	c.append(d[a[i] * (n - i) * (i + 1)])
m = []
for i in range(n):
	m.append(b1[i] * a1[i])
ans = 0
for i in range(n):
	ans = (ans + m[i]) % x
print(ans % x)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	n = int(inp())
	a = lis()
	b = lis()
	ans = []
	for i in range(0, n):
		ans.append(a[i] * (n - i) * (i + 1))
	ans = sorted(ans)
	b = sorted(b)[::-1]
	cnt = 0
	for i in range(n):
		cnt += ans[i] * b[i]
		cnt %= 998244353
	print(cnt)
testcase(1)


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10 ** 5)
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def main():
	t = 1
	for _ in range(t):
		n = int(input())
		a = inar()
		b = inar()
		take = []
		for i in range(n):
			take.append(a[i] * (i + 1) * (n - i))
		b.sort()
		take.sort(reverse=True)
		mod = 998244353
		result = 0
		for i in range(n):
			result = (result + take[i] % mod * (b[i] % mod) % mod) % mod
		print(result)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


MOD = 998244353
n = int(input())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
p = [(i + 1) * (n - i) * a[i] for i in range(n)]
p.sort(reverse=True)
m = sorted(b)
ans1 = 0
ans2 = 0
for i in range(n):
	ans1 += p[i] * m[i]
print(ans1 % MOD)


Mod = 998244353
su = 0
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = sorted([a[i - 1] * i * (n + 1 - i) for i in range(1, n + 1)])
b = sorted(b, reverse=True)
for i in range(n):
	c[i] = c[i] % Mod
	b[i] = b[i] % Mod
	su += b[i] * c[i] % Mod
	su = su % Mod
print(su)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
m = 998244353
for i in range(n):
	a[i] *= (i + 1) * (n - i)
a.sort()
sum = 0
b.sort()
for i in range(n):
	sum += a[i] * b[n - 1 - i] % m
print(sum % m)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
x = 0
mod = 998244353
for i in range(n):
	c.append(a[i] * (i + 1) * (n - i))
c = sorted(c)
b = sorted(b, reverse=True)
for i in range(n):
	x = x + c[i] * b[i]
	x = x % mod
print(x)


from sys import stdin, stdout
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from math import ceil, gcd
from collections import defaultdict as dd
from collections import Counter as C
n = it()
a = mp()
b = sorted(mp(), reverse=True)
mod = 998244353
ans = 0
v = [0] * n
for i in range(n):
	v[i] = a[i] * (i + 1) * (n - i)
v.sort()
for (i, j) in zip(b, v):
	ans += i * j
print(ans % mod)


import sys
from collections import defaultdict
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
b = list(map(int, sys.stdin.readline().split()))
mod = 998244353
c = []
for i in range(n):
	(left, right) = (i + 1, n - i)
	c.append([a[i] * left * right, i])
c.sort()
b.sort()
b.reverse()
newb = [0 for _ in range(n)]
for i in range(n):
	newb[c[i][1]] = b[i]
ans = 0
for i in range(n):
	(left, right) = (i + 1, n - i)
	add = left * right * a[i] * newb[i] % mod
	ans += add
	ans = ans % mod
print(ans % mod)


from math import *
from itertools import *
mod = 998244353
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
b = sorted(b)
b = b[::-1]
l = [0] * n
for i in range(len(a)):
	nice = i * (i + 1) // 2
	nice += (n - i - 1) * (n - i) // 2
	nice = n * (n + 1) // 2 - nice
	l[i] = nice * a[i]
l = sorted(l)
tot = 0
for i in range(n):
	tot += l[i] * b[i]
	tot %= mod
print(tot)


def solve(n, a, b):
	b.sort()
	val = []
	for i in range(n):
		val.append((i + 1) * (n - i) * a[i])
	val.sort()
	b.sort(reverse=True)
	ans = 0
	mod = 998244353
	for i in range(n):
		ans += val[i] % mod * b[i] % mod % mod
	print(ans % mod)
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
solve(n, a, b)


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = 0
	sb = sorted(b, reverse=True)
	val = []
	for i in range(n):
		val.append((i + 1) * (n - i) * a[i])
	sv = sorted(val)
	for i in range(n):
		ans += sv[i] * sb[i]
	print(ans % 998244353)
main()


from operator import itemgetter
import functools
n = int(input())
a = [int(i) for i in input().split(' ')]
b = [int(i) for i in input().split(' ')]
b.sort()
id = [a[i] * (i + 1) * (n - i) for i in range(n)]
id = sorted(id)
ans = 0
for i in range(n):
	ans += id[i] * b[n - i - 1]
	ans %= 998244353
print(ans)


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict, OrderedDict
import math

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
MOD = 998244353
n = intIn()
a = listIn()
b = listIn()
b.sort(reverse=True)
repeat = [0] * n
for i in range(n):
	repeat[i] = (i + 1) * (n - i)
c = [0] * n
di = defaultdict()
for i in range(n):
	c[i] = a[i] * repeat[i]
	di[i] = c[i]
di = OrderedDict(sorted(di.items(), key=lambda x: x[1]))
d = list(di.values())
total = 0
for i in range(n):
	total += b[i] * d[i]
print(total % MOD)


import sys
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 998244353
N = INT()
A = LIST()
B = LIST()
A = [a * (i + 1) * (N - i) for (i, a) in enumerate(A)]
A.sort()
B.sort(reverse=1)
ans = 0
for i in range(N):
	ans += A[i] * B[i]
	ans %= MOD
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sum = 0
b.sort()
l = []
for i in range(n):
	a[i] = a[i] * (i + 1) * (n - i)
a.sort()
b = b[::-1]
for i in range(n):
	sum = (sum + a[i] * b[i] % 998244353) % 998244353
print(sum)


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(N):
	A[i] = A[i] * (i + 1) * (N - i)
A.sort()
B.sort()
ans = 0
for i in range(N):
	ans = ans + A[i] * B[N - i - 1] % 998244353
print(ans % 998244353)


n = int(input())
a = list(map(int, input().split()))
b = sorted(list(map(int, input().split())), reverse=True)
c = sorted([a[i - 1] * i * (n + 1 - i) for i in range(1, n + 1)])
print(sum([b[i] % 998244353 * (c[i] % 998244353) % 998244353 for i in range(n)]) % 998244353)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	res = 0
	ans = [0 for _ in range(n)]
	idx = [0 for _ in range(n)]
	for i in range(n):
		ans[i] = a[i] * (i + 1) * (n - i)
		idx[i] = b[i]
	ans.sort()
	idx.sort()
	idx = idx[::-1]
	for i in range(n):
		res = (res + ans[i] * idx[i]) % 998244353
	print(res)
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = 998244353
k = [a[i] * (n - i) * (i + 1) for i in range(n)]
k.sort(reverse=True)
b.sort()
print(sum((k[i] * b[i] % m for i in range(n))) % m)


n = int(input())
l1 = list(map(int, input().strip().split()))
l2 = list(map(int, input().strip().split()))
for i in range(n):
	l1[i] *= (i + 1) * (n - i)
l1.sort()
l2.sort()
l = []
sm = 0
for i in range(n):
	sm += l1[i] * l2[n - 1 - i] % 998244353
print(sm % 998244353)


I = lambda : map(int, input().split())
(n,) = I()
a = [*I()]
(b, a) = (sorted(I(), reverse=1), sorted((a[i] * (n - i) * (i + 1) for i in range(n))))
print(sum((a[i] * b[i] for i in range(n))) % 998244353)


import sys
input = sys.stdin.buffer.readline

def solution():
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	for i in range(n):
		A[i] *= (i + 1) * (n - i)
	A.sort()
	B.sort()
	B.reverse()
	S = sum((A[i] * B[i] for i in range(n)))
	print(S % 998244353)
solution()


import math
ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
si = lambda : input()
n = ii()
(a, b) = (li(), li())
val = []
for i in range(n):
	val.append((i + 1) * (n - i) * a[i])
val.sort()
b.sort()
sum = 0
for i in range(n):
	sum += val[i] * b[n - i - 1]
print(sum % 998244353)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a2 = [a[i - 1] * i * (n - i + 1) for i in range(1, n + 1)]
a2 = sorted(a2)
b = sorted(b)
c = 0
for i in range(len(a2)):
	c = (c + a2[i] * b[n - i - 1]) % 998244353
print(c % 998244353)


from sys import stdin, stdout
int_in = lambda : int(stdin.readline())
arr_in = lambda : [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for y in range(rows)]
str_in = lambda : stdin.readline().strip()
out = lambda o: stdout.write('{}\n'.format(o))
arr_out = lambda o: out(' '.join(map(str, o)))
bool_out = lambda o: out('YES' if o else 'NO')
tests = lambda : range(1, int_in() + 1)
case_out = lambda i, o: out('Case #{}: {}'.format(i, o))

def solve(n, a, b):
	mod = 998244353
	parts = [a[i] * (n - i + (n - i) * i) for i in range(n)]
	parts.sort()
	b.sort(reverse=True)
	result = 0
	for i in range(n):
		result += parts[i] * b[i]
		result %= mod
	return result % mod
n = int_in()
a = arr_in()
b = arr_in()
out(solve(n, a, b))


from sys import stdin

def main():
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = list(map(int, stdin.readline().split()))
	b.sort(reverse=True)
	ans = 0
	mod = 998244353
	h = []
	for i in range(1, n + 1):
		h.append(i * (n - i + 1))
	for i in range(n):
		h[i] = h[i] * a[i]
	h.sort()
	for i in range(n):
		ans += b[i] * h[i] % mod
	print(ans % mod)
main()


N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(N):
	a[i] = a[i] * (N - i) * (i + 1)
a1 = [(a[i], i) for i in range(N)]
a1.sort(key=lambda x: x[0])
a1.reverse()
a1 = [i[1] for i in a1]
b.sort()
res = [0] * N
for i in range(N):
	res[a1[i]] = b[i]
b = res
Sum = 0
for i in range(N):
	Sum += a[i] * b[i]
print(Sum % 998244353)


import sys, os, io
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from sys import stdin
import math

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def si():
	return input()

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
mod = 998244353
t = 1
for _ in range(t):
	n = ii()
	a = li()
	b = li()
	for i in range(n):
		a[i] *= (i + 1) * (n - i)
	a.sort()
	b.sort(reverse=True)
	res = 0
	for i in range(n):
		res = (res + a[i] * b[i]) % mod
	print(res)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [0] + a
b = [0] + b
for i in range(1, n + 1):
	a[i] *= i
	a[i] *= n + 1 - i
a.sort()
b.sort()
ans = 0
mod = 998244353
for i in range(1, 1 + n):
	ans += a[i] * b[n + 1 - i] % mod
	if ans >= mod:
		ans -= mod
print(ans)


n = int(input())
a = list(map(int, input().split(' ')))
at = []
for i in range(len(a)):
	at.append(a[i] * (i + 1) * (n - i))
at.sort(reverse=True)
b = list(map(int, input().split(' ')))
b.sort()
print(sum([a * b for (a, b) in zip(at, b)]) % 998244353)


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = []
for i in range(n):
	c.append((i + 1) * (n - i) * a[i])
c = sorted(c)
b = sorted(b)
answer = 0
for i in range(n):
	answer = (answer + c[i] * b[n - i - 1]) % 998244353
print(answer)


n = int(input())
a = [int(s) for s in input().split()]
b = [int(s) for s in input().split()]
ans = 0
b.sort()
for i in range(n):
	k = (i + 1) * (n - i)
	a[i] *= k
a.sort()
for i in range(n):
	ans += b[n - i - 1] * a[i]
	ans %= 998244353
print(ans)


n = int(input())
a = sorted((int(x) * (n - i) * (i + 1) for (i, x) in enumerate(input().split())))
b = reversed(sorted((int(x) for x in input().split())))
print(sum((ai * bi % 998244353 for (ai, bi) in zip(a, b))) % 998244353)


def main():
	n = int(input())
	mod = 998244353
	a = list(map(int, input().split()))
	b = sorted(map(int, input().split()))
	for i in range(n):
		a[i] *= (i + 1) * (n - i)
	a.sort(reverse=1)
	ans = 0
	for i in range(n):
		ans = (ans + a[i] * b[i]) % mod
	print(ans)
	return 0
main()