#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];
    }
    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


#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));
        ans %= mod;
    }
    print((ans + mod) % mod);
}
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];
        a[i] %= mod;
    }
    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) % mod);
}
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)) % 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 + mod) % mod);
}
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;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=5e5+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)b[i]=read();
	sort(a+1,a+1+n);
	sort(b+1,b+1+n,cmp);
	int ans=0,now=n;
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)*i;
	sort(c+1,c+1+n,cmp);
	for(int i=1;i<=n;i++){
		//cout<<a[i]<<b[i]<<c[i]<<endl;
		ans=(ans+a[i]*b[i]*c[i])%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=1e6+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
inline bool cmp1(int x,int y){return x<y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read()%mod;
	for(int i=1;i<=n;i++)b[i]=read()%mod;
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)%mod*i%mod*a[i]%mod;
	sort(c+1,c+1+n,cmp1);
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=(ans+b[i]%mod*c[i]%mod)%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=5e5+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)b[i]=read();
	sort(b+1,b+1+n,cmp);
	int ans=0,now=n;
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)*i*a[i];
	sort(c+1,c+1+n);

	for(int i=1;i<=n;i++){
		ans=(ans+b[i]*c[i])%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=1e6+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)b[i]=read();
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)*i%mod*a[i]%mod;
	sort(c+1,c+1+n);
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=(ans+b[i]*c[i]%mod)%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e19
#define inf 0x7fffffff
const int N=5e5+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)b[i]=read();
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)*i%mod*a[i]%mod;
	sort(c+1,c+1+n);
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=(ans+b[i]*c[i]%mod)%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e19
#define inf 0x7fffffff
const int N=1e6+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)b[i]=read();
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)*i%mod*a[i]%mod;
	sort(c+1,c+1+n);
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=(ans+b[i]*c[i]%mod)%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=5e5+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)b[i]=read();
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)*i%mod*a[i]%mod;
	sort(c+1,c+1+n);
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=(ans+b[i]*c[i]%mod)%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=5e5+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)b[i]=read();
	sort(b+1,b+1+n,cmp);
	int ans=0,now=n;
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)*i*a[i]%mod;
	sort(c+1,c+1+n);

	for(int i=1;i<=n;i++){
		ans=(ans+b[i]*c[i])%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=1e6+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
inline bool cmp1(int x,int y){return x<y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read()%mod;
	for(int i=1;i<=n;i++)b[i]=read()%mod;
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)*i*a[i];
	sort(c+1,c+1+n,cmp1);
	ll ans=0;
	for(int i=1;i<=n;i++){
		c[i]%=mod;
		ans=(ans+b[i]*c[i])%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=1e6+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read()%mod;
	for(int i=1;i<=n;i++)b[i]=read()%mod;
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)%mod*i%mod*a[i]%mod;
	sort(c+1,c+1+n);
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=(ans+b[i]%mod*c[i]%mod)%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=1e6+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)b[i]=read()%mod;
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)*i%mod*a[i]%mod;
	sort(c+1,c+1+n);
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=(ans+b[i]*c[i]%mod)%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=1e6+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read()%mod;
	for(int i=1;i<=n;i++)b[i]=read()%mod;
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)%mod*i%mod*a[i]%mod;
	sort(c+1,c+1+n);
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=(ans+b[i]%mod*c[i]%mod)%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
#define INF 1e19
#define inf 0x7fffffff
#define int ll
const int N=1e6+50;
const int mod=998244353;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}

int n,a[N],b[N],c[N];
inline bool cmp(int x,int y){return x>y;}
inline bool cmp(int x,int y){return x<y;}
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read()%mod;
	for(int i=1;i<=n;i++)b[i]=read()%mod;
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++)
		c[i]=(n-i+1)%mod*i%mod*a[i]%mod;
	sort(c+1,c+1+n,cmp1);
	int ans=0;
	for(int i=1;i<=n;i++){
		ans=(ans+b[i]%mod*c[i]%mod)%mod;
	}
	cout<<ans%mod;
}


#include<bits/stdc++.h>
#define ipa pair<lld,lld>
#define lld long long int
#define x first
#define y second
#define ld long double
#define pb push_back
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PI (atan(1)*4)
#define mp make_pair
#define mlog 21
#define mod 998244353
using namespace std;
const int maxn=1e6+2;
main()
{
	fastread;
	lld i,j,n,fans=0;
	cin>>n;
	lld a[n],b[n];
	pair<lld,lld> pa[n+1];
	pair<lld,lld> ans[n+1];
	for(i=0;i<n;++i)
	{
		cin>>a[i];
		pa[i].x=a[i];
		pa[i].y=i;
	}
	for(i=0;i<n;++i)
		cin>>b[i];
	sort(b,b+n);
	sort(pa,pa+n);
	for(i=0;i<n;++i)
	{
		ans[pa[i].y].x=pa[i].x;
		ans[pa[i].y].y=b[n-1-i];
	}
	for(i=0;i<n;++i)
	{
		lld temp1=((i+1)%mod*(n-i)%mod)%mod;
		lld temp2=(ans[i].x%mod * ans[i].y%mod)%mod;
		lld temp3=(temp1%mod * temp2%mod)%mod;
		//cout<<ans[i].x<<" "<<ans[i].y<<"\n";
		fans=(fans%mod+temp3%mod)%mod;
	}
	cout<<fans;
}


#include<bits/stdc++.h>
#define ipa pair<lld,lld>
#define lld long long int
#define x first
#define y second
#define ld long double
#define pb push_back
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PI (atan(1)*4)
#define mp make_pair
#define mlog 21
#define mod 998244353
using namespace std;
const int maxn=1e6+2;
main()
{
	fastread;
	lld i,j,n,fans=0;
	cin>>n;
	lld a[n],b[n],ans=0;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
	}
	for(i=0;i<n;++i)
		cin>>b[i];

	sort(b,b+n);
	for(i=0;i<n;++i)
	{
		a[i]=a[i]*(i+1)*(n-i);
	}
	sort(a,a+n);
	for(i=0;i<n;++i)
	{
		a[i]=a[i]%mod;
		lld temp=a[i]*b[n-1-i];
		ans=(ans+temp)%mod;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
#define ipa pair<lld,lld>
#define lld long long int
#define x first
#define y second
#define ld long double
#define pb push_back
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PI (atan(1)*4)
#define mp make_pair
#define mlog 21
#define mod 998244353
using namespace std;
const int maxn=1e6+2;
main()
{
	fastread;
	lld i,j,n,fans=0;
	cin>>n;
	lld a[n],b[n];
	pair<lld,lld> pa[n+1];
	pair<lld,lld> ans[n+1];
	for(i=0;i<n;++i)
	{
		cin>>a[i];
		pa[i].x=a[i];
		pa[i].y=i;
	}
	for(i=0;i<n;++i)
		cin>>b[i];
	sort(b,b+n);
	sort(pa,pa+n);
	for(i=0;i<n;++i)
	{
		ans[pa[i].y].x=pa[i].x;
		ans[pa[i].y].y=b[n-1-i];
	}
	for(i=0;i<n;++i)
	{
		lld temp3=(i+1)*(n-i)*ans[i].x*ans[i].y;
		fans=(fans+temp3)%mod;
	}
	cout<<fans;
}


#include<bits/stdc++.h>
#define ipa pair<lld,lld>
#define lld long long int
#define x first
#define y second
#define ld long double
#define pb push_back
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PI (atan(1)*4)
#define mp make_pair
#define mlog 21
#define mod 998244353
using namespace std;
const int maxn=1e6+2;
main()
{
	fastread;
	lld i,j,n,fans=0;
	cin>>n;
	lld a[n],b[n],ans=0;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
	}
	for(i=0;i<n;++i)
		cin>>b[i];

	sort(b,b+n);
	for(i=0;i<n;++i)
	{
		a[i]=a[i]*(i+1)*(n-i);
		a[i]=a[i]%mod;
	}
	sort(a,a+n);
	for(i=0;i<n;++i)
	{
		lld temp=a[i]*b[n-1-i];
		ans=(ans+temp)%mod;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
#define ipa pair<lld,lld>
#define lld long long int
#define x first
#define y second
#define ld long double
#define pb push_back
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PI (atan(1)*4)
#define mp make_pair
#define mlog 21
#define mod 998244353
using namespace std;
const int maxn=1e6+2;
main()
{
	fastread;
	lld i,j,n,fans=0;
	cin>>n;
	lld a[n],b[n],ans=0;
	for(i=0;i<n;++i)
	{
		cin>>a[i];
		a[i]=a[i]*(i+1)*(n-i);
		a[i]=a[i]%mod;
	}
	for(i=0;i<n;++i)
		cin>>b[i];

	sort(b,b+n);
	sort(a,a+n);
	for(i=0;i<n;++i)
	{
		lld temp=a[i]*b[n-1-i];
		ans=(ans+temp)%mod;
	}
	cout<<ans;
}


#include<bits/stdc++.h>
#define ipa pair<lld,lld>
#define lld long long int
#define x first
#define y second
#define ld long double
#define pb push_back
#define fastread ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PI (atan(1)*4)
#define mp make_pair
#define mlog 21
#define mod 998244353
using namespace std;
const int maxn=1e6+2;
main()
{
	fastread;
	lld i,j,n,fans=0;
	cin>>n;
	lld a[n],b[n];
	pair<lld,lld> pa[n+1];
	pair<lld,lld> ans[n+1];
	for(i=0;i<n;++i)
	{
		cin>>a[i];
		pa[i].x=a[i];
		pa[i].y=i;
	}
	for(i=0;i<n;++i)
		cin>>b[i];
	sort(b,b+n);
	sort(pa,pa+n);
	for(i=0;i<n;++i)
	{
		ans[pa[i].y].x=pa[i].x;
		ans[pa[i].y].y=b[n-1-i];
	}
	for(i=0;i<n;++i)
	{
		lld temp1=(ans[i].x*ans[i].y)%mod;
		lld temp2=((i+1)*(n-i))%mod;
		lld temp3=(temp1*temp2)%mod;
		fans=(fans+temp3)%mod;
	}
	cout<<fans;
}


    #include<bits/stdc++.h>
    #define ipa pair<lld,lld>
    #define lld long long int
    #define x first
    #define y second
    #define ld long double
    #define pb push_back
    #define fastread ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
    #define PI (atan(1)*4)
    #define mp make_pair
    #define mlog 21
    #define mod 998244353
    using namespace std;
    const int maxn=1e6+2;
    main()
    {
    	fastread;
    	lld i,j,n,fans=0;
    	cin>>n;
    	lld a[n],b[n];
    	pair<lld,lld> pa[n+1];
    	pair<lld,lld> ans[n+1];
    	for(i=0;i<n;++i)
    	{
    		cin>>a[i];
    		pa[i].x=a[i]*(i+1)*(n-i);
    		pa[i].y=i;
    	}
    	for(i=0;i<n;++i)
    		cin>>b[i];
    	sort(b,b+n);
    	sort(pa,pa+n);
    	for(i=0;i<n;++i)
    	{
    		ans[pa[i].y].x=pa[i].x;
    		ans[pa[i].y].y=b[n-1-i];
    	}
    	for(i=0;i<n;++i)
    	{
    		lld temp2=(ans[i].x%mod * ans[i].y%mod)%mod;
    		fans=(fans%mod+temp2%mod)%mod;
    	}
    	cout<<fans;
    }


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
#define MOD 998244353
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

int main() {
	optimizar_io
	int n; cin >> n;
	vector<lli> a(n), b(n);
	for(lli i = 0; i < n; i++) {
		cin >> a[i];
		lli times = (i + 1ll) * (n - i);
		a[i] = (a[i] * times) % MOD;
	}
	fore(i, 0, n) cin >> b[i];

	sort(all(a)); sort(all(b));

	lli ans = 0;
	for(int i = 0, j = n-1; i < n; i++, j--) {
		lli mul = (a[i] * b[j]) % MOD;
		ans = (ans + mul) % MOD;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
#define MOD 998244353ll
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

int main() {
	optimizar_io
	int n; cin >> n;
	vector<lli> a(n), b(n);
	for(lli i = 0; i < n; i++) {
		cin >> a[i];
		lli times = (i + 1ll) * (n - i);
		a[i] = (a[i] * times) % MOD;
	}
	fore(i, 0, n) cin >> b[i];

	sort(all(a)); sort(all(b));

	lli ans = 0;
	for(int i = 0, j = n-1; i < n; i++, j--) {
		lli mul = (a[i] * b[j]) % MOD;
		ans = (ans + mul) % MOD;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
#define MOD 998244353
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

int main() {
	optimizar_io
	int n; cin >> n;
	vector<lli> b(n);
	vector<pair<lli, int>> a(n, pair<lli, int>());
	fore(i, 0, n) {
		cin >> a[i].fi;
		a[i].se = i;
	}
	fore(i, 0, n) cin >> b[i];

	sort(all(a)); sort(all(b));

	lli ans = 0;
	for(int i = 0, j = n-1; i < n; i++, j--) {
		lli mul = a[i].fi * b[j];
		int times = (a[i].se + 1) * (n - a[i].se);
		mul %= MOD;
		lli aux = mul * times;
		aux %= MOD;
		ans = (ans + aux) % MOD;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
#define MOD 998244353
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

int main() {
	optimizar_io
	int n; cin >> n;
	vector<lli> a(n), b(n);
	fore(i, 0, n) {
		cin >> a[i];
		int times = (i + 1) * (n - i);
		a[i] = (a[i] * times) % MOD;
	}
	fore(i, 0, n) cin >> b[i];

	sort(all(a)); sort(all(b));

	lli ans = 0;
	for(int i = 0, j = n-1; i < n; i++, j--) {
		lli mul = (a[i] * b[j]) % MOD;
		ans = (ans + mul) % MOD;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
#define MOD 998244353ll
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

int main() {
	optimizar_io
	int n; cin >> n;
	vector<lli> a(n), b(n);
	fore(i, 0, n) cin >> a[i];
	fore(i, 0, n) cin >> b[i];
	fore(i, 0, n) {
		lli times = (i + 1ll) * (n - i) * 1ll;
		a[i] = (a[i] * times);
	}
	sort(all(a)); sort(all(b));
	reverse(all(b));

	lli ans = 0;
	for(int i = 0; i < n; i++) {
		a[i] %= MOD;
		b[i] %= MOD;
		lli mul = (a[i] * b[i]);
		mul %= MOD;
		ans = (ans + mul);
		ans %= MOD;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
#define MOD 998244353ll
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

int main() {
	optimizar_io
	int n; cin >> n;
	vector<lli> a(n), b(n);
	fore(i, 0, n) cin >> a[i];
	fore(i, 0, n) cin >> b[i];
	fore(i, 0, n) {
		lli times = (i + 1ll) * (n - i) * 1ll;
		a[i] = (a[i] * times);
		a[i] %= MOD;
	}
	sort(all(a)); sort(all(b));
	reverse(all(b));

	lli ans = 0;
	for(int i = 0; i < n; i++) {
		a[i] %= MOD;
		b[i] %= MOD;
		lli mul = (a[i] * b[i]);
		mul %= MOD;
		ans = (ans + mul);
		ans %= MOD;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

lli MOD = 998244353ll;
int main() {
	optimizar_io
	int n; cin >> n;
	vector<lli> a(n), b(n);
	fore(i, 0, n) cin >> a[i];
	fore(i, 0, n) cin >> b[i];
	fore(i, 0, n) {
		lli times = (i + 1ll) * (n - i) * 1ll;
		a[i] = (a[i] * times);
		a[i] %= MOD;
	}
	sort(all(a)); sort(all(b));
	reverse(all(b));

	lli ans = 0;
	for(int i = 0; i < n; i++) {
		lli mul = (a[i] * b[i]) % MOD;
		ans = (ans + mul) % MOD;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

lli MOD = 998244353ll;
int main() {
	optimizar_io
	int n; cin >> n;
	vector<lli> a(n), b(n);
	for(lli i = 0; i < n; i++) {
		cin >> a[i];
		lli times = (i + 1ll) * (n - i);
		a[i] = (a[i] * times) % MOD;
	}
	fore(i, 0, n) cin >> b[i];

	sort(all(a)); sort(reverse(all(b)));

	lli ans = 0;
	for(int i = 0; i < n; i++) {
		lli mul = (a[i] * b[i]) % MOD;
		ans = (ans + mul) % MOD;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(0); cin.tie(0);
#define fore(i, a, b) for(int i = a; i < b; i++)
#define fori(i, a, b) for(int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define popb pop_back
#define mk make_pair
#define fi first
#define se second
#define endl '\n'
typedef long long int lli;
typedef long double ld;
const double PI = acos(-1.0);
ld eps = 1e-9;

lli MOD = 998244353ll;
int main() {
	optimizar_io
	int n; cin >> n;
	vector<lli> a(n), b(n);
	for(lli i = 0; i < n; i++) {
		cin >> a[i];
		lli times = (i + 1ll) * (n - i);
		a[i] = (a[i] * times) % MOD;
	}
	fore(i, 0, n) cin >> b[i];

	sort(all(a)); sort(all(b));
	reverse(all(b));

	lli ans = 0;
	for(int i = 0; i < n; i++) {
		lli mul = (a[i] * b[i]) % MOD;
		ans = (ans + mul) % MOD;
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN (int)(3e5 + 100)
#define INF 2e5
typedef unsigned long long int ulli;

bool cmp(ulli a, ulli b){return a > b;}

int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ulli n, sum = 0, MOD = 998244353;
    vector<ulli> a(MAXN), b(MAXN);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i], a[i] *= (i + 1) * (n - i), a[i] %= MOD;
    for(int i = 0; i < n; i++) cin >> b[i];
    sort(a.begin(), a.begin() + n); sort(b.begin(), b.begin() + n);
    for(int i = 0; i < n; i++) sum += a[i] * b[n - 1 - i] % MOD;
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN (int)(3e5 + 100)
#define INF 2e5
typedef unsigned long long int ulli;

bool cmp(ulli a, ulli b){return a > b;}

int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ulli n, sum = 0, MOD = 998244353;
    vector<ulli> a(MAXN), b(MAXN);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i], a[i] *= (i + 1) * (n - i);
    for(int i = 0; i < n; i++) cin >> b[i];
    sort(a.begin(), a.begin() + n); sort(b.begin(), b.begin() + n);
    for(int i = 0; i < n; i++) sum += a[i] % MOD * b[n - 1 - i] % MOD;
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN (int)(3e5 + 100)
#define INF 2e5
typedef unsigned long long int ulli;

bool cmp(ulli a, ulli b){return a > b;}

int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ulli n, sum = 0, MOD = 998244353;
    vector<ulli> a(MAXN), b(MAXN);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i], a[i] *= (i + 1) * (n - i);
    for(int i = 0; i < n; i++) cin >> b[i];
    sort(a.begin(), a.begin() + n); sort(b.begin(), b.begin() + n);
    for(int i = 0; i < n; i++) sum += a[i] * b[n - 1 - i] % MOD;
    cout << sum;
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <utility>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
#define MAXN (int)(3e5 + 100)
#define INF 2e5
typedef unsigned long long int ulli;

bool cmp(ulli a, ulli b){return a > b;}

int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    ulli sum = 0, MOD = 998244353;
    multiset< pair<ulli, int> > sa;
    vector<ulli> a(MAXN), b(MAXN), c(MAXN), d(MAXN);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i], sa.insert(make_pair(a[i], i));
    for(int i = 0; i < n; i++) cin >> b[i];
    sort(b.begin(), b.begin() + n, cmp);
    multiset< pair<ulli, int> >::iterator sai = sa.begin();
    for(int i = 0; i < n; i++) sum += (sai->first) * b[i] * (sai->second + 1) * (n - sai->second) % MOD, sai++;
    cout << sum;

    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <utility>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;
#define MAXN (int)(3e5 + 100)
#define INF 2e5
typedef unsigned long long int ulli;

bool cmp(ulli a, ulli b){return a > b;}

int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ulli n, sum = 0, MOD = 998244353;
    multiset< pair<ulli, ulli> > sa;
    vector<ulli> a(MAXN), b(MAXN), c(MAXN), d(MAXN);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i], sa.insert(make_pair(a[i], i));
    for(int i = 0; i < n; i++) cin >> b[i];
    sort(b.begin(), b.begin() + n, cmp);
    multiset< pair<ulli, ulli> >::iterator sai = sa.begin();
    for(int i = 0; i < n; i++) sum += (sai->first) * b[i] * (sai->second + 1) * (n - sai->second) % MOD, sai++;
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define MAXN (int)(3e5 + 100)
#define INF 2e5
typedef unsigned long long int ulli;

bool cmp(ulli a, ulli b){return a > b;}

int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ulli n, sum = 0, MOD = 998244353;
    vector<ulli> a(MAXN), b(MAXN);
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i], a[i] *= (i + 1) * (n - i);
    for(int i = 0; i < n; i++) cin >> b[i];
    sort(a.begin(), a.begin() + n); sort(b.begin(), b.begin() + n);
    for(int i = 0; i < n; i++) sum += a[i]%MOD * b[n - 1 - i]%MOD, sum%=MOD;
    cout << sum;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(double pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    long long n, p, s;
    cin >>n;

    for(int i = 0; i < n; i++){
        cin >> p >> s;
        if(isInteger((double) cbrt(p*s))){
            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(double pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;

    for(int i = 0; i < n; i++){
        long long p,s;
        cin >> p >> s;
        if(isInteger((double) cbrt(p*s))){
            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    long long p,s,r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> p >> s;
        r = ceil(pow(p*s,1/3.));
        if (r*r*r == p*s && p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(float pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;
            long long p,s,r;

    for(int i = 0; i < n; i++){
        cin >> p >> s;
        r = (long long)cbrt(p*s);
        if(isInteger((float) cbrt(p*s)) && p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(float pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;
            long long p,s,r;

    for(int i = 0; i < n; i++){
        cin >> p >> s;
        r = ceil(pow(p*s,1/3.));
        if (r*r*r == p*s && p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(float pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;
            long long p,s,r;

    for(int i = 0; i < n; i++){
        cin >> p >> s;
        r = (long long)cbrt(p*s);
        if(r*r*r == p*s && p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(float pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;

    for(int i = 0; i < n; i++){
        long long p,s;
        cin >> p >> s;
        if(isInteger((float) cbrt(p*s)) && p%(long long)cbrt(p*s) == 0 && s%(long long)cbrt(p*s) == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(float pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;

    for(int i = 0; i < n; i++){
        long long p,s;
        cin >> p >> s;
        long long r = (long long)cbrt(p*s);
        if(isInteger((float) cbrt(p*s)) && p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, p, s, r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> p >> s;
        r = ceil(pow(p*s,1/3.));
        if (r*r*r == p*s && p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(float pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;
            long long p,s,r;

    for(int i = 0; i < n; i++){
        cin >> p >> s;
        r = (long long)cbrt(p*s);
        if(r*r*r != p*s)
            cout << "NO" << endl;
        else if (p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(float pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;

    for(int i = 0; i < n; i++){
        long long p,s;
        cin >> p >> s;
        if(isInteger((float) cbrt(p*s))){
            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,r;
    long long p,s;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> p >> s;
        r = ceil(pow(p*s,1/3.));
        if (r*r*r == p*s && p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    long long p,s,r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> p >> s;
        r = ceil(pow(p*s,1/3.));
        if (r*r*r == p*s && p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, p, s r;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> p >> s;
        r = ceil(pow(p*s,1/3.));
        if (r*r*r == p*s && p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(float pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;

    for(int i = 0; i < n; i++){
        long long p,s,r;
        cin >> p >> s;
        r = (long long)cbrt(p*s);

        if(r*r*r == p*s && p%r == 0 && s%r == 0){

            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool isInteger(long double pkmn){
    if(ceil(pkmn) == floor(pkmn)){
        return true;
    }
    return false;
}
int main()
{
    int n;
    cin >> n;

    for(int i = 0; i < n; i++){
        long long p,s;
        cin >> p >> s;
        if(isInteger((long double) cbrt(p*s))){
            cout << "YES" <<endl;
        }else{
            cout << "NO" << endl;
        }
    }

    return 0;
}


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

int pri[MAXN],len=0;
int vis[MAXN];
void prime(){
  int n=sqrt(1e9+500);
  mem(vis,0);
  lp(i,2,n){
    if (vis[i]==0) {
      pri[len++]=i;//cout<<i<<endl;
      int p=i;
      while(p<=n) {
        vis[p]=1;
        p+=i;///p=ki 注意
      }
    }
  }
}

bool solve(){
  ll a,b;
  cin>>a>>b;
  int n=max(sqrt(a),sqrt(b));
  int cnt=lower_bound(pri,pri+len,n)-pri+1;

  int i=0;
  while(i<cnt){
    if (a%pri[i]==0 || b%pri[i]==0){
      if (a%pri[i]!=0 || b%pri[i]!=0) return 0;

      int ap=0,bp=0;
      while(a%pri[i]==0) {ap++;a/=pri[i];}
      while(b%pri[i]==0) {bp++;b/=pri[i];}
    //  printf("pri= %d ap = %d bp= %d \n",pri[i],ap,bp);

      int q=ap+bp,p=ap-bp;
      if (q%3!=0) return 0;
      double aw=(q+3*p)/6.0,bw=(q-3*p)/6.0;
      if (ap==2*aw+bw && bp==2*bw+aw) continue;
      else return 0;
    }

    i++;
    if (a==1 && b==1) return 1;
  }
}

void print_arr(int a[],int len){
  rep(i,len) cout<<a[i]<<" ";
  cout<<endl;
}

int main(){
  prime();
//  print_arr(pri,len);
  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}
/*
1
1000000000 1000000

*/


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

bool solve(){
  ll a,b;cin>>a>>b;

  ll l=1,r=1e6;  ///r*r*r>=1e18
  ll ans,mid;
  ll w=a*b;
  while(l<=r){
    mid = (l+r)/2;
    ans=mid*mid*mid;
 // printf("l= %lld r= %lld mid=%lld ans=%lld w=%lld\n",l,r,mid,ans,w);
    if (mid*mid*mid == w ) break;
    else {
      if (ans<w) l=mid+1;
      else r=mid-1;
    }
  }

  if (ans==w && a%ans && b%ans){
    return 1;
  }
  else return 0;
}

int main(){
 // ll p=LLONG_MAX;
 // cout<<(int)(log10(p)+1)<<endl;

  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

bool solve(){
  ll a,b;cin>>a>>b;

  ll l=1,r=1e6;  ///r*r*r>=1e18
  ll ans,mid;
  ll w=a*b;
  while(l<=r){
    mid = (l+r)/2;
    ans=mid*mid*mid;
 // printf("l= %lld r= %lld mid=%lld ans=%lld w=%lld\n",l,r,mid,ans,w);
    if (mid*mid*mid == w ) break;
    else {
      if (ans<w) l=mid+1;
      else r=mid-1;
    }
  }

  if (ans==w && a%ans==0 && b%ans==0){///a%ans==0 不要写成 a%ans
    return 1;
  }
  else return 0;
}

int main(){
 // ll p=LLONG_MAX;
 // cout<<(int)(log10(p)+1)<<endl;

  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

int pri[MAXN],len=0;
int vis[MAXN];
void prime(){
  int n=sqrt(1e9+500);
  mem(vis,0);
  lp(i,2,n){
    if (vis[i]==0) {
      pri[len++]=i;//cout<<i<<endl;
      int p=i;
      while(p<=n) {
        vis[p]=1;
        p+=i;///p=ki 注意
      }
    }
  }
}

bool solve(){
  ll a,b;
  cin>>a>>b;
  int n=max(sqrt(a),sqrt(b));
  int cnt=lower_bound(pri,pri+len,n)-pri+1;  ///lowerbound作用是插入的地方，如果在尾部就会越界
  cnt=min(len,cnt);

  int i=0;
  while(i<cnt){
    if (a%pri[i]==0 || b%pri[i]==0){
      if (a%pri[i]!=0 || b%pri[i]!=0) return 0;

      int ap=0,bp=0;
      while(a%pri[i]==0) {ap++;a/=pri[i];}
      while(b%pri[i]==0) {bp++;b/=pri[i];}
    //  printf("pri= %d ap = %d bp= %d \n",pri[i],ap,bp);

      int q=ap+bp,p=ap-bp;
      if (q%3!=0) return 0;
      double aw=(q+3*p)/6.0,bw=(q-3*p)/6.0;
      if (ap==2*aw+bw && bp==2*bw+aw) continue;
      else return 0;
    }

    i++;
    if (a==1 && b==1) return 1;
  }
  return 0;
}

void print_arr(int a[],int len){
  rep(i,len) cout<<a[i]<<" ";
  cout<<endl;
}

int main(){
  prime();
 // print_arr(pri,len);
  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}
/*
1
1000000000 1000000

*/


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

bool solve(){
  ll a,b;
  scanf("%lld%lld",&a,&b);

  ll l=1,r=1e6;  ///r*r*r>=1e18
  ll ans,mid;
  ll w=a*b;
  while(l<=r){
    mid = (l+r)>>1;
    ans = mid*mid*mid;
//  printf("l= %lld r= %lld mid=%lld ans=%lld w=%lld\n",l,r,mid,ans,w);
    if (mid*mid*mid == w ) break;
    else {
      if (ans<w) l=mid+1;
      else r=mid-1;
    }
  }

  if (ans==w && a%mid==0 && b%mid==0){///a%ans==0 不要写成 a%ans
    return 1;
  }
  else return 0;
}

int main(){
 // ll p=LLONG_MAX;
 // cout<<(int)(log10(p)+1)<<endl;

  int n;scanf("%d",&n);
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

int pri[MAXN],len=0;
int vis[MAXN];
void prime(){
  int n=sqrt(1e9+500);
  mem(vis,0);
  lp(i,2,n){
    if (vis[i]==0) {
      pri[len++]=i;//cout<<i<<endl;
      int p=i;
      while(p<=n) {
        vis[p]=1;
        p+=i;///p=ki 注意
      }
    }
  }
}

bool solve(){
  ll a,b;
  cin>>a>>b;
  int n=max(sqrt(a),sqrt(b));
  int cnt=lower_bound(pri,pri+len,n)-pri+1;  ///lowerbound作用是插入的地方，如果在尾部就会越界
  cnt=min(len,cnt);

  int i=0;
  while(i<cnt){
    if (a==1 || b==1) {
      if (a!=1 || b!=1) return 0;
      else return 1;
    }
    if (a%pri[i]==0 || b%pri[i]==0){
      if (a%pri[i]!=0 || b%pri[i]!=0) return 0;

      int ap=0,bp=0;
      while(a%pri[i]==0) {ap++;a/=pri[i];}
      while(b%pri[i]==0) {bp++;b/=pri[i];}
    //  printf("pri= %d ap = %d bp= %d \n",pri[i],ap,bp);

      int q=ap+bp,p=ap-bp;
      if (q%3!=0) return 0;
      double aw=(q+3*p)/6.0,bw=(q-3*p)/6.0;
      if (ap==2*aw+bw && bp==2*bw+aw) continue;
      else return 0;
    }
    i++;
  }
  return 0;
}

void print_arr(int a[],int len){
  rep(i,len) cout<<a[i]<<" ";
  cout<<endl;
}

int main(){
  std::ios::sync_with_stdio(false);
  cin.tie(0);

  prime();
 // print_arr(pri,len);
  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}
/*
1
1000000000 1000000

*/


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

bool solve(){
  ll a,b;cin>>a>>b;

  ll l=1,r=1e6;  ///r*r*r>=1e18
  ll ans,mid;
  ll w=a*b;
  while(l<=r){
    mid = (l+r)/2;
    ans=mid*mid*mid;
//  printf("l= %lld r= %lld mid=%lld ans=%lld w=%lld\n",l,r,mid,ans,w);
    if (mid*mid*mid == w ) break;
    else {
      if (ans<w) l=mid+1;
      else r=mid-1;
    }
  }

  if (ans==w && a%mid==0 && b%mid==0){///a%ans==0 不要写成 a%ans
    return 1;
  }
  else return 0;
}

int main(){
 // ll p=LLONG_MAX;
 // cout<<(int)(log10(p)+1)<<endl;

  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

bool solve(){
  ll a,b;cin>>a>>b;

  ll l=1,r=1e6;  ///r*r*r>=1e18
  ll ans,mid;
  ll w=a*b;
  while(l<=r){
    mid = (l+r)/2;
    ans=mid*mid*mid;
  printf("l= %lld r= %lld mid=%lld ans=%lld w=%lld\n",l,r,mid,ans,w);
    if (mid*mid*mid == w ) break;
    else {
      if (ans<w) l=mid+1;
      else r=mid-1;
    }
  }

  if (ans==w && a%mid==0 && b%mid==0){///a%ans==0 不要写成 a%ans
    return 1;
  }
  else return 0;
}

int main(){
 // ll p=LLONG_MAX;
 // cout<<(int)(log10(p)+1)<<endl;

  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

bool solve(){
  ll a,b;cin>>a>>b;

  ll l=1,r=1e9;
  ll mid;
  ll w=a*b;
  while(l<r){
    mid = (l+r)/2;
    if (mid*mid*mid < w ) l=mid+1;
    else r=mid;
  }
  ll ans=l*l*l;
  if (ans==w && a%ans && b%ans){
    return 1;
  }
  else return 0;
}

int main(){
  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

int pri[MAXN],len=0;
int vis[MAXN];
void prime(){
  int n=sqrt(1e9+500);
  mem(vis,0);
  lp(i,2,n){
    if (vis[i]==0) {
      pri[len++]=i;//cout<<i<<endl;
      int p=i;
      while(p<=n) {
        vis[p]=1;
        p+=i;///p=ki 注意
      }
    }
  }
}

bool solve(){
  ll a,b;
  scanf("%lld%lld",&a,&b);
  if (a==1 && b==1) return 0;
  int n=max(sqrt(a),sqrt(b));
  int cnt=lower_bound(pri,pri+len,n)-pri+1;  ///lowerbound作用是插入的地方，如果在尾部就会越界
  cnt=min(len,cnt);

  int i=0;
  while(i<cnt){
    if (a==1 || b==1) {
      if (a!=1 || b!=1) return 0;
      else return 1;
    }
    if (a%pri[i]==0 || b%pri[i]==0){
      if (a%pri[i]!=0 || b%pri[i]!=0) return 0;

      int ap=0,bp=0;
      while(a%pri[i]==0) {ap++;a/=pri[i];}
      while(b%pri[i]==0) {bp++;b/=pri[i];}
    //  printf("pri= %d ap = %d bp= %d \n",pri[i],ap,bp);

      int q=ap+bp,p=ap-bp;
      if (q%3!=0) return 0;
      double aw=(q+3*p)/6.0,bw=(q-3*p)/6.0;
      if (ap==2*aw+bw && bp==2*bw+aw) continue;
      else return 0;
    }
    i++;
  }
  return 0;
}

void print_arr(int a[],int len){
  rep(i,len) cout<<a[i]<<" ";
  cout<<endl;
}

int main(){
  prime();
 // print_arr(pri,len);
  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}
/*
1
1000000000 1000000

*/


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

int pri[MAXN],len=0;
int vis[MAXN];
void prime(){
  int n=sqrt(1e9+500);
  mem(vis,0);
  lp(i,2,n){
    if (vis[i]==0) {
      pri[len++]=i;//cout<<i<<endl;
      int p=i;
      while(p<=n) {
        vis[p]=1;
        p+=i;///p=ki 注意
      }
    }
  }
}

bool solve(){
  ll a,b;
  scanf("%lld%lld",&a,&b);
  int n=max(sqrt(a),sqrt(b));
  int cnt=lower_bound(pri,pri+len,n)-pri+1;  ///lowerbound作用是插入的地方，如果在尾部就会越界
  cnt=min(len,cnt);

  int i=0;
  while(i<cnt){
    if (a==1 || b==1) {
      if (a!=1 || b!=1) return 0;
      else return 1;
    }
    if (a%pri[i]==0 || b%pri[i]==0){
      if (a%pri[i]!=0 || b%pri[i]!=0) return 0;

      int ap=0,bp=0;
      while(a%pri[i]==0) {ap++;a/=pri[i];}
      while(b%pri[i]==0) {bp++;b/=pri[i];}
    //  printf("pri= %d ap = %d bp= %d \n",pri[i],ap,bp);

      int q=ap+bp,p=ap-bp;
      if (q%3!=0) return 0;
      double aw=(q+3*p)/6.0,bw=(q-3*p)/6.0;
      if (ap==2*aw+bw && bp==2*bw+aw) continue;
      else return 0;
    }
    i++;
  }
  return 0;
}

void print_arr(int a[],int len){
  rep(i,len) cout<<a[i]<<" ";
  cout<<endl;
}

int main(){
  prime();
 // print_arr(pri,len);
  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}
/*
1
1000000000 1000000

*/


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

int pri[MAXN],len=0;
int vis[MAXN];
void prime(){
  int n=sqrt(1e9+500);
  mem(vis,0);
  lp(i,2,n){
    if (vis[i]==0) {
      pri[len++]=i;//cout<<i<<endl;
      int p=i;
      while(p<=n) {
        vis[p]=1;
        p+=i;///p=ki 注意
      }
    }
  }
}

bool solve(){
  ll a,b;
  cin>>a>>b;
  int n=max(sqrt(a),sqrt(b));
  int cnt=lower_bound(pri,pri+len,n)-pri+1;  ///lowerbound作用是插入的地方，如果在尾部就会越界
  cnt=min(len,cnt);

  int i=0;
  while(i<cnt){
    if (a==1 || b==1) {
      if (a!=1 || b!=1) return 0;
      else return 1;
    }
    if (a%pri[i]==0 || b%pri[i]==0){
      if (a%pri[i]!=0 || b%pri[i]!=0) return 0;

      int ap=0,bp=0;
      while(a%pri[i]==0) {ap++;a/=pri[i];}
      while(b%pri[i]==0) {bp++;b/=pri[i];}
    //  printf("pri= %d ap = %d bp= %d \n",pri[i],ap,bp);

      int q=ap+bp,p=ap-bp;
      if (q%3!=0) return 0;
      double aw=(q+3*p)/6.0,bw=(q-3*p)/6.0;
      if (ap==2*aw+bw && bp==2*bw+aw) continue;
      else return 0;
    }
    i++;
  }
  return 0;
}

void print_arr(int a[],int len){
  rep(i,len) cout<<a[i]<<" ";
  cout<<endl;
}

int main(){
  prime();
 // print_arr(pri,len);
  int n;cin>>n;
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}
/*
1
1000000000 1000000

*/


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

#pragma comment (linker,"/STACK:10240000000,10240000000")
#define mem(a,x) memset(a,x,sizeof a)
#define rep(i,n) for (int i=0;i<n;i++)
#define lp(i,l,r) for (int i=l;i<=r;i++)
#define rlp(i,r,l) for (int i=r;i>=l;i--)
#define T_T int _;scanf("%d",&_);for (int test=1;test<=_;test++)

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

const db eps=1e-13;
const int MAXN=1*1e6+500;

const char movech='A';///×Ö·ûÆ«ÒÆÁ¿
const int SIZE=28;///×Ö·û¼¯´óÐ¡

bool solve(){
  ll a,b;
  scanf("%lld%lld",&a,&b);
  //cout<<a*b<<endl;
  ll ans= pow(a*b,1/3.0)+0.5;
  if (ans*ans*ans  == a*b && a%ans==0 && b%ans==0){
    return 1;
  }
  else return 0;
}

int main(){
 // ll p=LLONG_MAX;
 // cout<<(int)(log10(p)+1)<<endl;

 //  cout<<pow(1000000000,1/3.0);

  int n;scanf("%d",&n);
  while(n--){
    if (solve()) printf("YES\n");else printf("NO\n");
  }
}


#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EPS 1e-10
#define set0(x) memset((x),0,sizeof(x))
#define setINF(x) memset((x),63,sizeof(x))
using namespace std;
int n,a,b;
bool isp[31630];
vector<int> prime;
const int rt=31625;
int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        for(int i=2;i<=rt;i++){
                if(isp[i])continue;
                prime.push_back(i);
                if(1ll*i*i>rt)continue;
                for(int j=i*i;j<=rt;j+=i){
                        isp[j]=true;
                }
        }
        cin>>n;
        while(n--){
                cin>>a>>b;
                int ta=a,tb=b;
                bool frog=true;
                for(int i=0;i<prime.size();i++){
                        int md=prime[i],ca=0,cb=0;
                        while(ta%md==0){
                                ta/=md;
                                ca++;
                        }
                        while(tb%md==0){
                                tb/=md;
                                cb++;
                        }
                        while(ca && cb){
                                if(ca>cb){
                                        ca-=2;
                                        cb--;
                                }
                                else{
                                        ca--;
                                        cb-=2;
                                }
                        }
                        if(ca || cb){
                                cout<<"No\n";
                                frog=false;
                                break;
                        }
                }
                if(frog){
                        if(ta==1 && tb==1)cout<<"Yes\n";
                        else cout<<"No\n";
                }
        }
        return 0;
}


#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EPS 1e-10
#define set0(x) memset((x),0,sizeof(x))
#define setINF(x) memset((x),63,sizeof(x))
using namespace std;
int n,a,b;
bool isp[31630];
vector<int> prime;
const int rt=1000;
int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        for(int i=2;i<=rt;i++){
                if(isp[i])continue;
                prime.push_back(i);
                if(1ll*i*i>rt)continue;
                for(int j=i*i;j<=rt;j+=i){
                        isp[j]=true;
                }
        }
        cin>>n;
        while(n--){
                cin>>a>>b;
                int ta=a,tb=b;
                bool frog=true;
                for(int i=0;i<prime.size() && ta>1 && tb>1;i++){
                        int md=prime[i],ca=0,cb=0;
                        while(ta%md==0){
                                ta/=md;
                                ca++;
                        }
                        while(tb%md==0){
                                tb/=md;
                                cb++;
                        }
                        if((ca+cb)%3){
                                frog=false;
                                break;
                        }
                        int c=(ca+cb)/3,d=ca-cb;
                        if((c+d)&1 || c-d<0 || c+d<0){
                                frog=false;
                                break;
                        }
                }
                if(ta>tb)swap(ta,tb);
                if(frog){
                        if(ta==1 && tb==1)cout<<"Yes\n";
                        else if(1ll*ta*ta==tb)cout<<"Yes\n";
                        else cout<<"No\n";
                }
                else cout<<"No\n";
        }
        return 0;
}


#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EPS 1e-10
#define set0(x) memset((x),0,sizeof(x))
#define setINF(x) memset((x),63,sizeof(x))
using namespace std;
int n,a,b;
bool isp[31630];
vector<int> prime;
const int rt=31625;
int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        for(int i=2;i<=rt;i++){
                if(isp[i])continue;
                prime.push_back(i);
                if(1ll*i*i>rt)continue;
                for(int j=i*i;j<=rt;j+=i){
                        isp[j]=true;
                }
        }
        cin>>n;
        while(n--){
                cin>>a>>b;
                int ta=a,tb=b;
                bool frog=true;
                for(int i=0;i<prime.size() && ta>1 && tb>1;i++){
                        int md=prime[i],ca=0,cb=0;
                        while(ta%md==0){
                                ta/=md;
                                ca++;
                        }
                        while(tb%md==0){
                                tb/=md;
                                cb++;
                        }
                        if((ca+cb)%3){
                                frog=false;
                                break;
                        }
                        int c=(ca+cb)/3,d=ca-cb;
                        if((c+d)&1 || c-d<0 || c+d<0){
                                frog=false;
                                break;
                        }
                }
                if(frog){
                        if(ta==1 && tb==1)cout<<"Yes\n";
                        else cout<<"No\n";
                }
                else cout<<"No\n";
        }
        return 0;
}


#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EPS 1e-10
#define set0(x) memset((x),0,sizeof(x))
#define setINF(x) memset((x),63,sizeof(x))
using namespace std;
int n,a,b;
bool isp[31630];
vector<int> prime;
const int rt=31625;
int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        for(int i=2;i<=rt;i++){
                if(isp[i])continue;
                prime.push_back(i);
                if(1ll*i*i>rt)continue;
                for(int j=i*i;j<=rt;j+=i){
                        isp[j]=true;
                }
        }
        cin>>n;
        while(n--){
                cin>>a>>b;
                long long t=1ll*a*b;
                bool frog=true;
                for(int i=0;i<prime.size();i++){
                        int md=prime[i],cnt=0;
                        while(t%md==0){
                                t/=md;
                                cnt++;
                        }
                        if(cnt%3){
                                cout<<"No\n";
                                frog=false;
                                break;
                        }
                }
                if(frog){
                        if(t==1)cout<<"Yes\n";
                        else cout<<"No\n";
                }
        }
        return 0;
}


#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EPS 1e-10
#define set0(x) memset((x),0,sizeof(x))
#define setINF(x) memset((x),63,sizeof(x))
using namespace std;
int n,a,b;
vector<pair<int,int> > fa;
int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>n;
        while(n--){
                cin>>a>>b;
                fa.clear();
                int ta=a,tb=b;
                for(int i=2;i*i<=a;i++){
                        if(ta%i==0){
                                int cnt=0;
                                while(ta%i==0){
                                        ta/=i;
                                        cnt++;
                                }
                                fa.push_back(make_pair(i,cnt));
                        }
                }
                if(ta>1)fa.push_back(make_pair(ta,1));
                bool frog=true;
                for(int i=0;i<fa.size();i++){
                        int u=fa[i].first;
                        int cnt=0;
                        while(tb%u==0){
                                tb/=u;
                                cnt++;
                        }
                        int ca=fa[i].second,cb=cnt;
                        while(ca && cb){
                                if(ca>cb){
                                        ca-=2;
                                        cb--;
                                }
                                else{
                                        ca--;
                                        cb-=2;
                                }
                        }
                        if(ca || cb){
                                cout<<"No\n";
                                frog=false;
                                break;
                        }
                }
                if(frog)cout<<"Yes\n";
        }
        return 0;
}


#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EPS 1e-10
#define set0(x) memset((x),0,sizeof(x))
#define setINF(x) memset((x),63,sizeof(x))
using namespace std;
int n,a,b;
bitset<100000005> isp;
vector<int> prime;
const int rt=31625;
int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        for(int i=2;i<=100000000;i++){
                if(isp[i])continue;
                if(i<rt)prime.push_back(i);
                if(1ll*i*i>100000000)continue;
                for(int j=i*i;j<=100000000;j+=i){
                        isp[j]=true;
                }
        }
        cin>>n;
        while(n--){
                cin>>a>>b;
                int ta=a,tb=b;
                bool frog=true;
                for(int i=0;i<prime.size() && ta>1 && tb>1;i++){
                        if(ta>rt && ta<100000000 && !isp[ta])break;
                        if(tb>rt && tb<100000000 && !isp[tb])break;
                        int md=prime[i],ca=0,cb=0;
                        while(ta%md==0){
                                ta/=md;
                                ca++;
                        }
                        while(tb%md==0){
                                tb/=md;
                                cb++;
                        }
                        if((ca+cb)%3){
                                frog=false;
                                break;
                        }
                        int c=(ca+cb)/3,d=ca-cb;
                        if((c+d)&1 || c-d<0 || c+d<0){
                                frog=false;
                                break;
                        }
                }
                if(frog){
                        if(ta==1 && tb==1)cout<<"Yes\n";
                        else cout<<"No\n";
                }
                else cout<<"No\n";
        }
        return 0;
}