1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <bits/stdc++.h> #define int long long #define re register using namespace std; const int N = 2e5 + 10,mod = 998244353; int n,ans; int arr[N],pot[N],mul[N],inv[N]; inline int read(){ int r = 0,w = 1; char c = getchar(); while (c < '0' || c > '9'){ if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9'){ r = (r << 3) + (r << 1) + (c ^ 48); c = getchar(); } return r * w; } inline int Add(int a,int b){ return (a + b) % mod; } inline int Sub(int a,int b){ return ((a - b) % mod + mod) % mod; } inline int Mul(int a,int b){ return a * b % mod; } inline void exgcd(int a,int b,int &x,int &y){ if (!b){ x = 1; y = 0; return; } exgcd(b,a % b,y,x); y = y - a / b * x; } inline int get_inv(int a,int p){ int x,y; exgcd(a,p,x,y); return (x % mod + mod) % mod; } inline void init(){ pot[0] = 1; for (re int i = 1;i <= n + 1;i++){ pot[i] = Mul(pot[i - 1],2); mul[i] = Add(mul[i - 1],Mul(arr[i],pot[i])); inv[i] = get_inv(pot[i],mod); } } signed main(){ n = read(); for (re int i = 1;i <= n;i++) arr[i] = read(); sort(arr + 1,arr + n + 1); init(); for (re int i = 1;i <= n;i++){ ans = Add(ans,Mul(Mul(Sub(mul[n],mul[i]),inv[i + 1]),arr[i])); ans = Add(ans,Mul(arr[i],arr[i])); } printf("%lld",ans); return 0; }
|