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: introductory

Starter code:

Solutions:
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)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

count = []
for i in range(n):
    count.append((n-i)*(i+1)*a[i])
b.sort()
count.sort(reverse = True)
ans = 0
for i in range(n):

    ans += count[i]*b[i]
    ans %= 998244353
print(ans)


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())
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 = sorted(a)
b = sorted(b)
ans = 0
for i in range(n):
	ans += b[n - i - 1] * a[i] % 998244353
	ans %= 998244353
print(ans)


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()

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)


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)


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())
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)


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 = 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(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 = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [0] + a
b = [0] + b
# print(n, a, b)
for i in range(1, n+1) :
    a[i] *= i
    a[i] *= n+1-i
a.sort()
b.sort()
# print(n, a, b)
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)


# AC
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 = [[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)


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())
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)

n=int(input())
a=list(map(int,input().split()))
b=sorted(list(map(int,input().split())),reverse=True)

ans=0

mmm=998244353

c=sorted([a[i]*(i+1)*(n-i) for i in range(n)])

for i in range(n):
    ans=(ans+c[i]*b[i])%mmm

print(ans)


R = lambda: map(int, input().split())

m = 998244353
n = int(input())
a = list(R())
b = 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())
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)
 


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)

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)


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 = [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)