Problem:
 Score : 500 points    Problem Statement For two sequences S and T of length N consisting of 0 and 1 , let us define f(S, T) as follows:    Consider repeating the following operation on S so that S will be equal to T . f(S, T) is the minimum possible total cost of those operations.   Change S_i (from 0 to 1 or vice versa). The cost of this operation is D \times C_i , where D is the number of integers j such that S_j \neq T_j (1 \leq j \leq N) just before this change.     There are 2^N \times (2^N - 1) pairs (S, T) of different sequences of length N consisting of 0 and 1 . Compute the sum of f(S, T) over all of those pairs, modulo (10^9+7) .      Constraints  1 \leq N \leq 2 \times 10^5  1 \leq C_i \leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  C_1  C_2  \cdots  C_N       Output Print the sum of f(S, T) , modulo (10^9+7) .        Sample Input 1 1
1000000000      Sample Output 1 999999993  There are two pairs (S, T) of different sequences of length 2 consisting of 0 and 1 , as follows:   S = (0), T = (1): by changing S_1 to 1 , we can have S = T at the cost of 1000000000 , so f(S, T) = 1000000000 .  S = (1), T = (0): by changing S_1 to 0 , we can have S = T at the cost of 1000000000 , so f(S, T) = 1000000000 .   The sum of these is 2000000000 , and we should print it modulo (10^9+7) , that is, 999999993 .       Sample Input 2 2
5 8      Sample Output 2 124  There are 12 pairs (S, T) of different sequences of length 3 consisting of 0 and 1 , which include:   S = (0, 1), T = (1, 0)   In this case, if we first change S_1 to 1 then change S_2 to 0 , the total cost is 5 \times 2 + 8 = 18 . We cannot have S = T at a smaller cost, so f(S, T) = 18 .       Sample Input 3 5
52 67 72 25 79      Sample Output 3 269312

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *C = map(int, read().split())

    C.sort()

    ans = 0
    for i in range(N):
        ans = (ans + C[i] * (N - i + 1)) % MOD

    ans = ans * pow(2, 2 * N - 2, MOD) % MOD

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input()); MOD = pow(10,9)+7
C= list(map(int,input().split()))
C.sort()


L = [1]
for i in range(1,N):
  temp = (i+2)*pow(2,i-1,MOD)
  L.append(temp)
  
L.reverse()
ans = 0
for i in range(N):
  ans = (ans + L[i]*pow(2,i,MOD)*C[i])%MOD

print(ans*pow(2,N,MOD)%MOD)

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

"""
Sは全部0と仮定して良い，最後に2^N倍
Cはソートしておく，後ろから使う．

iは0indexとする
各C[i]ごとに見るか，そうしたら，それが何番目かによって寄与が決まる
i桁目なので左にi桁あるけど，これは通り数にしか関与しない
右側にN-i-1(=aとする)桁分ある．a桁のうちj桁1があるとして，寄与がj*C[i]

通り数：
右：a個からj個えらぶ=aCj
左：0でも1でも置ける=2^i

変更時のD：j+1

i,jの2つのΣで考えると，j側は結局Σ((j+1) * aCj)
j=0~aでΣ(j+1 * aCj) = (a+2) * pow(2,a-1) = (N-i+2) * pow(2,a-1)

"""

def main():
    mod=10**9+7
    N=I()
    C=LI()
    C.sort()
    ans=0
    
    def calc(ii):
        a = N-ii-1
        if a==0:
            temp_j=1
        else:
            temp_j = (a+2) * pow(2,a-1,mod)
        temp = C[ii]*pow(2,ii,mod)*temp_j
        return temp%mod
    
    for i in range(N):
        ans=(ans+calc(i))%mod
        
    ans=(ans*pow(2,N,mod))%mod
        
    print(ans)

  

main()

Solution (Python):
N=int(input());c=sorted(map(int,input().split()));m=10**9+7;print(sum([c[i]*(N-i+1)*pow(4,N-1,m) for i in range(N)])%m)

Solution (Python):
N=int(input());c=sorted(list(map(int,input().split())));m=10**9+7;print(sum([c[i]*(N-i+1)*pow(4,N-1,m) for i in range(N)])%m)

Solution (Python):
N=int(input());c=sorted(list(map(int,input().split())));m=10**9+7;print(sum([c[i]*(N-i+1)*pow(2,2*N-2,m) for i in range(N)])%m)

Solution (Python):
N=int(input());c=sorted(list(map(int,input().split())));m=10**9+7;ans=0
for i in range(N):ans+=c[i]*(N-i+1)*pow(2,2*N-2,m)
print(ans%m)

Solution (Python):
N=int(input())
c=list(map(int,input().split()))
c.sort()
m=10**9+7
ans=0
for i in range(N):
    ans+=(c[i]*(N-i+1)*pow(2,(N-2)%(m-1),m))%m
print((ans*pow(2,N,m))%m)

Solution (Python):
n=int(input())
*c,=map(int,input().split())
c=sorted(c)[::-1]
mod=10**9+7
ans=0
for i,x in enumerate(c,1):
  d=i*pow(2,i-1,mod)
  if i>1:d-=(i-1)*pow(2,i-2,mod)
  d*=pow(2,n-i,mod)*x%mod
  ans+=d%mod
  ans%=mod
ans*=pow(2,n,mod)
print(ans%mod)

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq
nmax = 2*10**5+1
mod = 10**9+7
fac = [1]*nmax
finv = [1]*nmax
inv = [1]*nmax
def ncr_pre():
    for i in range(2,nmax):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod%i] * (mod//i) %mod
        finv[i] = finv[i-1] * inv[i] %mod

def ncr(n,r):
    if n<r:
        return 0
    if n<0 or r<0:
        return 0
    return fac[n]* (finv[r] * finv[n-r] %mod) %mod
n = ni()
C = lma();C.sort()
ans = 0
for i in range(1,n+1):
    c = C[i-1]
    ans += c*pow(4,i-1,mod)*pow(2,n-i,mod)*pow(2,n-i,mod)*(n-i+2)
    ans%=mod
print(ans)

Solution (Python):

n = int(input())
cl = list(map(int, input().split()))
cl.sort(reverse=True)
MOD = 10**9+7

ans = 0
num = pow(2,n-1,MOD)
inc = pow(2,n-2,MOD)
for i in range(n):
    ans += num*cl[i]
    ans%=MOD
    num += inc
    num%=MOD

ans *= pow(2,n,MOD)
ans%=MOD
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    c = list(map(int, input().split()))
    mod = pow(10, 9) + 7
    
    c.sort(reverse=True)
    
    ans = 0
    
    for i in range(n):
        ans += (i+2) * c[i]
        ans %= mod
    
    ans *= pow(2, n*2-2, mod)
    ans %= mod
    
    print(ans)
    
    
if __name__ == "__main__":
    main()

Solution (Python):
M=10**9+7
N=int(input())
print(pow(4,N-1,M)*sum([C*(N-i+1)%M for i,C in enumerate(sorted(map(int,input().split())))])%M)

Solution (Python):
MOD=10**9+7
N=int(input())
C=sorted(map(int,input().split()))
p=pow(4,N-1,MOD)
ans=0

for i in range(N):
    res=p*C[i]*(N-i+1)
    ans=(ans+res)%MOD
print(ans)

Solution (Python):
N = int(input())
C = sorted(list(map(int, input().split())))
P = int(1e9+7)

ans=0
inv2 = pow(2, P-2, P)
powa = inv2
powb = pow(2, N-1, P)
for i, c in enumerate(C):
    powa = (2*powa) % P
    powb = (inv2*powb) % P
    if i == N - 1:
        ans = (ans + powa * c)%P
    else:
        ans = (ans + powa * powb * (N - i + 1) * c) % P
print((ans*pow(2, N))%P)

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque, defaultdict


from collections import Counter

from itertools import accumulate,combinations,permutations,product

from heapq import heapify,heappop,heappush

from functools import reduce,lru_cache

from decimal import Decimal
from argparse import ArgumentParser

args=ArgumentParser()
args.add_argument('-loc', '--local', type=bool,default=False)
LOCAL=args.parse_args().local

def input(): 
    x=sys.stdin.readline(); 
    if x[-1]=="\n": 
        return x.rstrip() 
    return x
    
def printe(*x):_=print("
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def binr(x): return bin(x)[2:]
def bitcount(x): 
    x= x - ((x >> 1) & 0x5555555555555555)
    x= (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x= (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f 
    x+= (x >> 8); x+= (x >> 16); x+= (x >> 32) 
    return x & 0x7f

def main():
    mod = 1000000007
    

    N = int(input())
    
    cs = list(map(int, input().split())) 
    
    

    cs.sort()
    p2=[pow(2,i,mod) for i in range(N+1)]
    ans=0

    x=2*p2[N-1]**2%mod
    y=4*p2[N-2]**2%mod

    for i,c in enumerate(cs):
        if i<(N-1):
            ans+=y*c%mod*(N-1-i)
            ans%=mod
        ans+=x*c
        ans%=mod
    print(ans)
        


if __name__ == "__main__":
    main()

Solution (Python):
MOD = 10 ** 9 + 7
N = int(input())
C = list(sorted(map(int, input().split()), reverse=True))

ans = 0
pow_list = [1] * (N + 1)
for i in range(1, N + 1):
    pow_list[i] = (2 * pow_list[i - 1]) % MOD
base = pow_list[N]


for i, c in enumerate(C):
    now = pow_list[N - i - 1]
    tot = 0
    
    
    if i >= 1:
        tot = pow_list[i] + i * pow_list[i - 1]
    else:
        tot = pow_list[i]
    ans += (tot * now * c) % MOD
    ans %= MOD
print(ans * base % MOD)

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
from collections import Counter
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



import sys
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


N = int(input())
C = list(sorted(map(int, input().split()), reverse=True))

MAX = 2 * 10 ** 5 + 5
MOD = 10 ** 9 + 7

fac = [0 for i in range(MAX)]
finv = [0 for i in range(MAX)]
inv = [0 for i in range(MAX)]

def comInit(mod):
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod


def com(n, r, mod):
    if n < r:
        return 0
    if n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod

def p(n, r, mod):
    return fac[n] * finv[n - r] % mod
    
comInit(MOD)

ans = 0
pow_list = [1] * (N + 1)
for i in range(1, N + 1):
    pow_list[i] = (2 * pow_list[i - 1]) % MOD
base = pow_list[N]
for i, c in enumerate(C):
    now = pow_list[N - i - 1]
    tot = 0
    if i >= 1:
        tot = pow_list[i] + i * pow_list[i - 1]
    else:
        tot = pow_list[i]
    ans += (tot * now * c) % MOD
    ans %= MOD
print(ans * base % MOD)

Solution (Python):
mod=10**9+7
def A001792(n):
  if n>1:
    return (n+1)*pow(2,n-2,mod)
  else:
    return 1

nn=int(input())
C=list(map(int, input().split()))
C.sort(reverse=True)
mod=10**9+7
ans=0
for i in range(nn):
  ans+=pow(2,nn-i-1,mod)*A001792(i+1)*C[i]
  ans %=mod
ans = ans*pow(2,nn,mod)
ans %=mod
print(ans)

Solution (Python):
mod = 10**9+7
n = int(input())











C = list(map(int,input().split()))
C.sort(reverse = True)
ans = 0

for i,c in enumerate(C):
    ans += c*(i+2)
ans *= pow(4,n-1,mod)
print(ans%mod)

Solution (Python):
N = int(input())
C = sorted(map(int, input().split()))
mod = 10**9+7

ans = 0
for i, c in enumerate(C[::-1], 2):
    ans += i * c
ans %= mod
ans = ans * pow(2, 2*N-2, mod) % mod
print(ans)

Solution (Python):
n = int(input())
c = sorted(map(int, input().split()))

mod = 1000000007

ret = 0
for i in range(n):
    ret = (ret + (i + 2) * c[n - i - 1]) % mod

for i in range(n - 1):
    ret = (ret * 4) % mod

print(ret)

Solution (Python):
MOD = 10**9+7

def solve(C):
    N = len(C)
    C.sort()

    dp1 = 0
    dp2 = 0
    for k,c in enumerate(C):
        ndp2 = 2*pow(4,k,MOD)*c + 4*dp2
        dp1 = 4*dp1 + ndp2 - 2*dp2
        dp2 = ndp2
        dp1 %= MOD
        dp2 %= MOD

    return dp1

def naive(C):
    from itertools import product
    N = len(C)
    C.sort()
    s = 0
    for a in product(range(2),repeat=N):
        for b in product(range(2),repeat=N):
            pop = 1
            for x,y,c in zip(a,b,reversed(C)):
                if x != y:
                    s += c*pop
                    pop += 1
    return s

if __name__ == '__main__':
    N = int(input())
    C = list(map(int,input().split()))
    print(solve(C))

Solution (Python):
MOD = 10**9+7
N = int(input())
C = list(map(int, input().split()))
C.sort(reverse=True)
print((pow(4,N-1,MOD)*sum((i+2)*C[i] for i in range(N)))%MOD)

Solution (Python):
def solve():
  ans = 0
  N = int(input())
  C = list(map(int, input().split()))
  C.sort()
  mod = 10**9+7
  for i in range(N):
    ans += C[i]*(N+1-i)
  ans = ans*pow(2,2*N-2)%mod
  return ans
print(solve())

Solution (Python):
def solve():
  ans = 0
  N = int(input())
  C = list(map(int, input().split()))
  C.sort()
  mod = 10**9+7
  for i in range(N):
    ans += C[i]*(N+1-i)
  ans = ans*pow(2,2*N-2)%mod
  return ans
print(solve())

Solution (Python):
def solve():
  ans = 0
  N = int(input())
  C = list(map(int, input().split()))
  C.sort()
  mod = 10**9+7
  for i in range(N):
    ans += C[i]*(N+1-i)
    ans %= mod
  ans = ans*pow(2,2*N-2,mod)%mod
  return ans
print(solve())

Solution (Python):
def solve():
  ans = 0
  N = int(input())
  C = list(map(int, input().split()))
  C.sort()
  mod = 10**9+7
  for i in range(N):
    ans += C[i]*(N+1-i)
  ans = ans*pow(2,2*N-2,mod)%mod
  return ans
print(solve())

Solution (Python):

import sys

input = sys.stdin.buffer.readline

n = int(input())
C = list(map(int, input().split()))
C.sort(reverse=True)
mod = 10 ** 9 + 7
N = n + 5
bikkuri = [0] * N
bikkuri[0] = 1
gyaku = [0] * N
for i in range(1, N):
    bikkuri[i] = (i * bikkuri[i - 1]) % mod
gyaku[N - 1] = pow(bikkuri[N - 1], mod - 2, mod)
for i in range(N - 1, -1, -1):
    gyaku[i - 1] = (gyaku[i] * i) % mod


def comb(n, r):
    if r < 0 or n - r < 0:
        return 0
    return (bikkuri[n] * gyaku[r] * gyaku[n - r]) % mod


ans = 0
for i in range(n):
    ans += (i + 2) * C[i] * pow(2, 2 * n - 2, mod)
    ans %= mod
print(ans)

Solution (Python):
from bisect import bisect_left
n=int(input())
c=sorted(list(map(int,input().split())))
mod=10**9+7

p=[1]*(n+1)
for i in range(n):
    p[i+1]=p[i]*2
    p[i+1]%=mod
ans=0

for i in range(n):
    b=bisect_left(c,c[i])
    x=n-i-1
    ans+=(p[x]+x*p[x-1])*c[i]*p[i]
    ans%=mod
print(ans*p[n]%mod)

Solution (Python):
N = int(input())
C = list(map(int, input().split()))
MOD = 1_000_000_007

B = [0 for _ in range(N+1)]
B[0] = 1
for i in range(1,N+1):
  B[i] = B[i-1] * 2
  B[i] %= MOD  

C.sort()

ans = 0
for n in range(1,N+1):
  ans += C[n-1] * ((N-n) * B[N-n-1] + B[N-n]) * B[n-1]
  ans %=  MOD
  
ans *= B[N]
ans %= MOD

print(ans)

"""
D = [0 for _ in range(N)]
D[0] = C[0]
for i in range(1,N):
  D[i] = D[i-1] + C[i]
print(C)

  
ans = 0
for j in range(1,N+1):
  ans += D[j-1] * B[j-1]  
  print(D[j-1] * B[j-1]  
  
"""

Solution (Python):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
c=list(map(int,input().split()))


def cmb(n, r, mod=10**9+7):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

N = 2*10**5+10
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

ans=0
c.sort(reverse=True)

for i in range(n):
    ans+=c[i]*(i+2)
    ans%=mod
ans*=pow(4,n-1,mod)
print(ans%mod)

Solution (Python):
import sys
input = sys.stdin.buffer.readline

MOD = 10 ** 9 + 7

N = int(input())
Cs = sorted(map(int, input().split()), reverse = True)

magic = [pow(2, N-1, MOD)]
for _ in range(N-1):
    magic.append( (magic[-1] + pow(2, N-2, MOD)) % MOD )

ans = 0
for C, m in zip(Cs, magic):
    ans += C * m
    ans %= MOD

ans *= pow(2, N, MOD)
ans %= MOD

print(ans)

Solution (Python):

def resolve():
    MOD = 10 ** 9 + 7
    N = int(input())
    C = sorted(map(int, input().split()))
    P = [1] * (N + 1)
    for i in range(N):
        P[i + 1] *= P[i] * 2
        P[i + 1] %= MOD
    ans = 0
    for i in range(N):
        
        l, r = i, N - i - 1
        now = P[r]
        if r != 0:
            now += P[r - 1] * r
        now *= P[l]
        now *= C[i]
        ans += now
        ans %= MOD
    ans *= P[N]
    print(ans % MOD)


if __name__ == "__main__":
    resolve()

Solution (Python):
MOD = 10**9 + 7

def main():
    n = int(input())
    c = list(map(int, input().split()))
    c.sort()
    ans = 0
    for i in range(n):
        tmp = pow(2, i, MOD)
        if i < n-1:
            tmp *= pow(2, n-i-1, MOD) + pow(2, n-i-2, MOD) * (n-i-1)
        else:
            tmp *= pow(2, n-i-1, MOD)
        tmp %= MOD
        ans += c[i] * tmp
    print(ans * pow(2, n, MOD) % MOD)

if __name__ == "__main__":
    main()

Solution (Python):
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  

N = I()
C = LI()
mod = 10**9+7

C = [0] + sorted(C,reverse=True)

ans = 0
for i in range(1,N+1):
    ans += C[i]*(i+1)
    ans %= mod

ans *= pow(4,N-1,mod)
ans %= mod

print(ans)

Solution (Python):
def main():
    MOD = 10 ** 9 + 7
    N = int(input())
    C = list(map(int, input().split(' ')))
    C.sort()
    pow_2 = 1  
    for _ in range(2 * N - 2):
        pow_2 *= 2
        pow_2 %= MOD
    ans = 0
    for k, c in enumerate(C):
        ans += c * ((N - k + 1) * pow_2)
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

stdin = sys.stdin


def ni(): return int(ns())


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


def naa(N): return [na() for _ in range(N)]


def ns(): return stdin.readline().rstrip()  


N = ni()
C_array = na()
C_array.sort()
ans = 0
mod = 10 ** 9 + 7
dp = [C_array[0], 0]
arr2 = [0, 1]
j = 1
for i in range(N):
    j = j * 2 % mod
    arr2.append(j)


cold = C_array[0]
cusum = 0
for i, c in enumerate(C_array[1:]):
    new00 = (dp[0] + cold * arr2[i + 1] + cusum *
             arr2[i] + c * arr2[i + 1]) % mod
    new01 = (dp[1] + cusum * arr2[i] + c * arr2[i + 1]) % mod
    
    new0 = (new00 + new01) % mod
    new1 = (dp[0] + dp[1]) % mod
    dp = [new0, new1]
    
    cusum = (cold + cusum) % mod
    cold = c
ans = (dp[0] + dp[1]) * arr2[N + 1] % mod
print(ans)

Solution (Python):
MOD = 10**9 + 7

N = int(input())
Cs = list(map(int, input().split()))

Cs.sort(reverse=True)

pow2 = pow(2, 2*N-2, MOD)

ans = 0
for i, C in enumerate(Cs, start=1):
    ans += C*(i+1)*pow2
    ans %= MOD

print(ans)

Solution (Python):
N = int(input())
C = sorted(list(map(int,input().split())),reverse=True)
L = 10**9 + 7


if N > 1:
    b = 2**(N-2) % (L)
    a = [C[i] * b * (2+i) % L for i in range(N)]
    print(sum(a)*b*2*2%L)
else:
    print(C[0]*2%L)

Solution (Python):

import sys
INF = float("inf")

MOD = 1000000007  


class Combination(object):

    def __init__(self, N, mod=MOD):
        fac, finv, inv = [0]*(N+1), [0]*(N+1), [0]*(N+1)
        fac[:2] = 1, 1
        finv[:2] = 1, 1
        inv[1] = 1
        for i in range(2, N+1):
            fac[i] = fac[i-1]*i % mod
            inv[i] = -inv[mod % i]*(mod//i) % mod
            finv[i] = finv[i-1]*inv[i] % mod
        self.N = N
        self.MOD = mod
        self.fac = fac
        self.finv = finv
        self.inv = inv

    def __call__(self, n, k):
        if n < k:
            return 0
        if n < 0 or k < 0:
            return 0
        fac = self.fac
        finv = self.finv
        mod = self.MOD
        return fac[n] * (finv[k]*finv[n-k] % mod) % mod


def solve(N: int, C: "List[int]"):

    ans = 0
    C.sort(reverse=True)
    for i, c in enumerate(C, start=1):
        b = c*(pow(2, 2*N-2, MOD)*(i+1)) % MOD
        ans += b
        ans %= MOD
    print(ans % MOD)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    C = [int(next(tokens)) for _ in range(N)]  
    solve(N, C)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
M = 10**9+7

pp = [None]*n
v = 1
for i in range(n):
    pp[i] = v
    v *= 2
    v %= M

a.sort()
a = a[::-1]
ans = 0
ans += a[0]*(pow(2, 0, M))*pow(2, n-1,M)
for i in range(1,n):
    num = a[i]
    ans += num*(pp[i] + i*pp[i-1])*pp[n-i-1]
    ans %= M
ans *= pow(2,n,M)
ans %= M
print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")

n = int(input())
a = list(map(int, input().split()))
a.sort()
a = a[::-1]
M = 10**9+7
ans = 0
ans += a[0]*(pow(2, 0, M))*pow(2, n-1,M)
for i in range(1,n):
    num = a[i]
    ans += num*(pow(2, i, M) + i*pow(2,i-1,M))*pow(2,n-i-1,M)
    ans %= M
ans *= pow(2,n,M)
ans %= M
print(ans)

Solution (Python):
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math

MOD = 10 ** 9 + 7




class Combination:
    def __init__(self, mod=10**9+7, n=202020):
        self.f = [0] * n
        self.rf = [0] * n
        self.inv = [0] * n
        self.f[0], self.f[1] = 1, 1
        self.rf[0], self.rf[1] = 1, 1
        self.inv[1] = 1
        self.MOD = mod
        for i in range(2, n):
            self.f[i] = (self.f[i - 1] * i) % self.MOD
            self.inv[i] = (-self.inv[self.MOD % i] * (self.MOD // i)) % self.MOD
            self.rf[i] = (self.rf[i - 1] * self.inv[i]) % self.MOD

    def __call__(self, n, r):
        return (self.f[n] * self.rf[r] * self.rf[n - r]) % self.MOD


C = Combination()

n = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = 0
for i in range(n):
    ans = (ans + A[i]*(i+2)) % MOD

ans = (ans * pow(4, n-1, MOD)) % MOD
print(ans)

Solution (Python):
n = int(input())
mod = 10 ** 9 + 7
c = list(map(int, input().split()))
c.sort()

ans = 0
for i in range(n):
    if i < n - 1:
        ans += c[i] * (n - i + 1) * pow(2, n - i - 2, mod) * pow(2, i, mod)
        ans %= mod

    else:
        ans += c[i] * pow(2, n - i - 1, mod) * pow(2, i, mod)
        ans %= mod


ans *= pow(2, n, mod)
ans %= mod

print(ans)

Solution (Python):

import queue
import sys


buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, C):
    mod = 10**9 + 7
    if N == 1:
        return (C[0] * 2) % mod

    C.sort()
    ans = 0
    a = pow(2, N, mod)
    b = pow(2, N-1, mod)
    c = pow(2, N-2, mod)
    for i in range(N):
        d = (N-i-1) * c
        d %= mod
        n = a * (b + d)
        n %= mod
        ans += n * C[i]
        ans %= mod

    return ans % mod




def main():
    N = read_int()
    C = read_int_n()
    print(slv(N, C))


if __name__ == '__main__':
    main()

Solution (Python):


LI = lambda: list(map(int, input().split()))


N = int(input())
C = LI()

MOD = 10 ** 9 + 7


def main():
    ans = 0
    C.sort()
    for i in range(N):
        x = C[i] * (N - i + 1) % MOD
        ans = (ans + x) % MOD
    ans = ans * pow(4, N - 1, MOD) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N=int(input())
C=list(map(int,input().split()))
mod=10**9+7
C.sort()
C.reverse()
ans=0
for i in range(N):
  ans+=C[i]*(i+2)
  ans%=mod

p=pow(4,N-1,mod)
ans=ans*p%mod
print(ans)

Solution (Python):
mod = int(1e9+7)
n = int(input())
arr = list(map(int, input().split()))
arr.sort()

s = 0
for e in arr:
    s += e
    s %= mod

total = s*pow(2, n-1, mod) % mod

def fn(x):
    if x == 0:
        return 0
    return (x*pow(2, x-1, mod)) % mod

for i in range(n):
    total += fn(n-i-1) * pow(2, i, mod) % mod * arr[i] % mod
    total %= mod
    

print(total*pow(2, n, mod)%mod)

Solution (Python):
import sys
from collections import defaultdict
from queue import deque
readline = sys.stdin.buffer.readline



def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    N = gete(int)
    c = list(geta(int))
    c.sort(reverse=True)
    mod = 10**9 + 7
    dp = [0] * (N + 1)

    base = 1
    for n in range(1, N + 1):
        dp[n] = (4 * dp[n - 1] + (n + 1) * base * c[n - 1]) % mod
        base = (4 * base) % mod
    print(dp[N])


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import defaultdict
from queue import deque
readline = sys.stdin.buffer.readline



def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    N = gete(int)
    c = list(geta(int))
    c.sort(key=lambda x: -x)
    mod = 10**9 + 7
    dp = [0] * (N + 1)

    base = 1
    for n in range(1, N + 1):
        dp[n] = 4 * dp[n - 1] + (n + 1) * base * c[n - 1]
        base *= 4

        dp[n] %= mod
        base %= mod
    print(dp[N])


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
ans = 0
mod = 10**9+7
for i,ai in enumerate(a):
  chk = pow(2,n-1-i,mod)+(n-1-i)*pow(2,max(0,n-2-i),mod)
  chk %= mod
  ans += chk*pow(2,i,mod)*ai
  ans %= mod
print(ans*pow(2,n,mod)%mod)

Solution (Python):
n=int(input())
c=sorted(list(map(int,input().split())))


mod=pow(10,9)+7



ans=0
for i,ci in enumerate(c):
  l=i
  r=n-i
  b=pow(2,r-1,mod)+(r-1)*pow(2,r-2,mod) if r>1 else 1
  b*=pow(2,l,mod)
  ans+=ci*b
  ans%=mod
ans*=pow(2,n,mod)
print(ans%mod)

Solution (Python):
import sys
import itertools
import fractions
stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
C=nl()
mod=10**9+7
n=0
C.sort()
two=[0]*(N+1)
two[0]=1
for i in range(N):
    two[i+1]=(two[i]*2)%mod
ans=0
for i in range(N):
    l=i
    r=N-1-i
    now=two[r]
    if(r!=0):
        now+=two[r-1]*r
    now*=two[i]
    now*=C[i]
    ans+=now
    ans%=mod

ans*=two[N]
print(ans%mod)

Solution (Python):
import numpy as np
MOD = 10**9 + 7

def plu(a,b):
    return (a + b)%MOD

plus = np.frompyfunc(plu,2,1)

n = int(input())
c = np.sort(np.array(input().split(),dtype = np.int64))
p = pow(2,2*n-2,MOD)
print(plus.reduce(np.mod(np.mod(p*c,MOD)*(n-np.arange(n)+1),MOD)))

Solution (Python):
import sys
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 15
from heapq import  heapify,heappop,heappush


def main():
    N = int(input())
    C = list(map(int,input().split()))
    C.sort()
    
    power = [1]
    for i in range(N):
        power.append(power[-1]*2%MOD)

    ans = 0
    for i,c in enumerate(C):
        ans += c*(power[N - 2]*power[N]*(N - i - 1))%MOD
        ans += c*(power[N - 1]*power[N])%MOD
        ans %= MOD
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys; input = sys.stdin.buffer.readline
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")

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


def main():
	N = int(input())
	C = list(reversed(sorted(getlist())))
	ans = 0
	for i in range(N):
		ans += pow(2, 2 * N - 2, con) * (i + 2) * C[i]
		ans %= con

	print(ans)

if __name__ == '__main__':
	main()

Solution (Python):

number=10**9+7

n=int(input())
c=list(map(int, input().split()))

c.sort(reverse=True)
sum=0
for i in range(1, n+1):
    sum=(sum+(pow(4, (n-1), number))*(i+1)*c[i-1])%number
print(sum)

Solution (Python):
n=int(input())
c=list(map(int,input().split()))
c.sort(reverse=True)
mod=10**9+7
ans=0
a=pow(4,n-1,mod)
for i,cost in enumerate(c):
  ans+=cost*(i+2)*a
  ans%=mod
print(ans)

Solution (Python):
N=int(input())
C=list(map(int,input().split()))
mod=10**9+7
C.sort(reverse=True)

ans=pow(2,N-1,mod)*C[0]
ans%=mod
for i in range(1,N):
    ans+=(pow(2,N-1,mod)+i*pow(2,N-2,mod))*C[i]
    ans%=mod

ans*=pow(2,N,mod)
ans%=mod
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF = 1 << 60
MOD = 10**9 + 7 
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n = int(input())
    C = list(map(int, input().split()))
    C.append(INF) 
    C.sort(reverse = 1)

    res = 0
    for i in range(1, n + 1):
        res += (i + 1) * C[i]
        res %= MOD

    res *= pow(4, n - 1, MOD)
    res %= MOD
    print(res)
resolve()

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N=int(input())
    C=list(map(int,input().split()))
    
    def COMinit(n,MOD):
        fac,finv,inv=[0]*max(2,n+1),[0]*max(2,n+1),[0]*max(2,n+1)
        fac[0]=fac[1]=1
        finv[0]=finv[1]=1
        inv[1]=1
        for i in range(2,(n+1)):
            fac[i]=fac[i-1]*i%MOD
            inv[i]=MOD-inv[MOD%i]*(MOD//i)%MOD
            finv[i]=finv[i-1]*inv[i]%MOD
        return fac,finv,inv
    
    fac,finv,inv=COMinit(N,MOD)
    
    def COM(n, k, MOD):
        if n<k or n<0 or k<0:
            return 0
        return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD
    
    def modinv(a, mod):
        b,u,v = mod,1,0
        while b:
            t=a//b
            a-=t*b
            a,b=b,a
            u-=t*v
            u,v=v,u
        u%=mod
        if u < 0:
            u+=mod
        return u

    def modpow(n,k,MOD):
        if k<0:
            return pow(modinv(n,MOD),-k,MOD)
        else:
            return pow(n,k,MOD)
    
    C.sort(reverse=1)
    ans=0
    for i in range(N):
        ans+=(modpow(2,i,MOD)+i*modpow(2,i-1,MOD))*2*modpow(2,N-(i+1),MOD)*C[i]
        ans%=MOD
    ans*=pow(2,N-1,MOD)
    ans%=MOD
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
cost = 0
for i in range(1, n+1):
  cost = (cost + a[i-1]*(i+1)) % 1000000007
for i in range(1, n):
  cost = cost * 4 % 1000000007
print(cost)