#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
// using namespace atcoder;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

const ll MOD = 998244353;

int main() {
    ll n; cin >> n;
    ll a[n] = {};
    rep(i,n) cin >> a[i];
    sort(a,a+n);

    ll ans = 0;
    ll now = 0;
    rep(i,n){
        ans += now * a[i] % MOD;
        ans += a[i]*a[i];
        ans %= MOD;
        now = (now*2 + a[i]) % MOD;
    }

    cout << ans << endl;
    return 0;
}



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

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef pair<int,int>   II;
typedef vector< II >      VII;
typedef vector<int>     VI;
typedef vector< VI > 	VVI;
typedef long long int 	LL;
typedef vector<LL> 		VL;
#define si(n) cin>>n
#define dout(n) cout<<n<<"\n"
#define DRT() int t; si(t); while(t--)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//# define M_PI           3.14159265358979323846264338327950288
// DSU

void IO(){
	#ifndef ONLINE_JUDGE
    freopen("../input.txt", "r", stdin);
    freopen("../output.txt", "w", stdout);
	#endif
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	off;
}
LL n,m;
VL arr;
VL brr;
map<int, VI> occ;
LL dp[1005][1005];
LL rec(LL ind, LL bind){
	if(ind == n || bind == m){
		return max(n - ind, m - bind);
	}
	if(dp[ind][bind] != -1)
		return dp[ind][bind];
	LL ans = 0;
	ans = rec(ind+1, bind+1) + (arr[ind] != brr[bind]);
	LL tt = 1 + min(rec(ind, bind+1), rec(ind+1, bind));
	return dp[ind][bind] = min(ans,tt);
}
int main(){
	IO();
	cin>>n>>m;
	arr.resize(n);brr.resize(m);
	rep(i,0,n){
		cin>>arr[i];
	}
	rep(i,0,m){
		cin>>brr[i];
		// occ[brr[i]].PB(i);
	}
	rep(i,0,1005){
		rep(j,0,1005){
			dp[i][j] = -1;
		}
	}
	cout<<rec(0, 0);
	return 0;
}