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 in C# (Mono 4.6.2.0) (judge verdict: Accepted)

// ReSharper disable ArrangeTypeMemberModifiers
// ReSharper disable ConvertIfStatementToSwitchStatement
// ReSharper disable FunctionRecursiveOnAllPaths
// ReSharper disable InconsistentNaming
// ReSharper disable InlineOutVariableDeclaration
// ReSharper disable InvertIf
// ReSharper disable JoinDeclarationAndInitializer
// ReSharper disable MemberCanBeMadeStatic.Global
// ReSharper disable MemberCanBeMadeStatic.Local
// ReSharper disable NonReadonlyMemberInGetHashCode
// ReSharper disable PossibleNullReferenceException
// ReSharper disable RedundantUsingDirective
// ReSharper disable SuggestVarOrType_BuiltInTypes
// ReSharper disable SuggestVarOrType_Elsewhere
// ReSharper disable TailRecursiveCall
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedMember.Local
// ReSharper disable UseObjectOrCollectionInitializert

#if true && UTAKA_LOCAL

//#if false
#define UTAKA_DEBUG
#endif

//#define TOPCODER

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using static UtakaApp.Util;

namespace UtakaApp
{
    public partial class Program
    {
        /*
        https://atcoder.jp/contests/abc150/tasks/abc150_e
        */

        public const string SiteName    = "AtCoder";
        public const string ContestName = "abc150";
        public const string ProblemName = "E";

        public void Solve()
        {
            int    N = cin.ReadInt;
            long[] C = cin.ReadLongArray(N);

            InitMpows(2, N);

            Array.Sort(C);

            Mint ans = 0;

            for (int i = 0; i < N; i++)
            {
                int l = i;
                int r = N - 1 - i;

                Mint now        = mpows[r];
                if (r != 0) now += mpows[r - 1] * r;
                now *= mpows[l];
                now *= C[i];

                ans += now;
            }

            ans *= mpows[N];

            cou.WriteLine(ans);
        }

        public Mint[] mpows;

        /// <summary>
        /// baseValue ^ exponent までの配列を用意する
        /// </summary>
        public void InitMpows(int baseValue, int exponent)
        {
            mpows    = new Mint[exponent + 1];
            mpows[0] = 1;
            for (int i = 1; i <= exponent; i++)
            {
                mpows[i] = mpows[i - 1] * baseValue;
            }
        }
    }

    public struct Mint
    {
        public const int  Mod              = 1000000007;
        public const long PositiveAddition = ((long)Mod) << 31;

        public long Value;

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public Mint(long value)
        {
            if ((Value = value % Mod) < 0)
            {
                Value += Mod;
            }
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static implicit operator long(Mint mint) => mint.Value;

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static implicit operator Mint(long val) => new Mint(val);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Mint operator +(Mint a, int b) => new Mint() {Value = (a.Value + b + PositiveAddition) % Mod};

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Mint operator +(Mint a, long b) => new Mint(a.Value + b);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Mint operator +(Mint a, Mint b)
        {
            long res = a.Value + b.Value;
            return new Mint() {Value = res >= Mod ? res - Mod : res};
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Mint operator -(Mint a, int b) => new Mint() {Value = (a.Value - b + PositiveAddition) % Mod};

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Mint operator -(Mint a, long b) => new Mint(a.Value - b);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Mint operator -(Mint a, Mint b)
        {
            long res = a.Value - b.Value;
            return new Mint() {Value = res < 0 ? res + Mod : res};
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Mint operator *(Mint a, int b) => new Mint(a.Value * b);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Mint operator *(Mint a, long b) => a * new Mint(b);

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Mint operator *(Mint a, Mint b) => new Mint() {Value = a.Value * b.Value % Mod};

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Mint operator /(Mint a, Mint b) => new Mint() {Value = a.Value * GetInverse(b) % Mod};

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public override string ToString() => Value.ToString();

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        static long GetInverse(long a)
        {
            long div;

            long p  = Mod;
            long x1 = 1;
            long y1 = 0;
            long x2 = 0;
            long y2 = 1;

            while (true)
            {
                if (p == 1) return x2 + Mod;
                div =  a / p;
                x1  -= x2 * div;
                y1  -= y2 * div;
                a   %= p;
                if (a == 1) return x1 + Mod;
                div =  p / a;
                x2  -= x1 * div;
                y2  -= y1 * div;
                p   %= a;
            }
        }
    }


//=========================================================================================
// Debug IO
//=========================================================================================

    public partial class Program
    {
        //=========================================================================================
        // Main
        //=========================================================================================

        private readonly ConsoleInput cin;
        private readonly IConsole     cou;

        public Program(ConsoleInput cin, IConsole cou)
        {
            this.cin = cin;
            this.cou = cou;
        }

#if !TOPCODER
        public static void Main(string[] args)
        {
#if UTAKA_LOCAL
//#if false
            new TestCaseCheckerForAtCoder().TestProblems();
#else
            var cin = new ConsoleInput(System.Console.In);
            var console = new MyConsole();
            new Program(cin, console).Solve();
            console.Flush();
#endif
        }
#endif

        public void WriteEnumerable<T>(IEnumerable<T> A, string delimiter = " ")
        {
            var sb = new StringBuilder();
            foreach (T v in A)
            {
                sb.Append(v);
                sb.Append(delimiter);
            }

            if (sb.Length > 0)
            {
                sb.Length--;
            }

            cou.WriteLine(sb.ToString());
        }
    }

    public static class Util
    {
        //=========================================================================================
        // Library
        //=========================================================================================

        public static T[] Array1D<T>(int a, T defaultValue)
            where T : struct
        {
            var ret = new T[a];
            for (int i = 0; i < a; i++)
            {
                ret[i] = defaultValue;
            }

            return ret;
        }

        public static T[] Array1D<T>(int a)
            where T : struct
        {
            var ret = new T[a];
            return ret;
        }

        /// <summary>
        /// 要素数 (a, b) の、defaultValue で満たされた配列を作成します。
        /// </summary>
        /// <typeparam name="T">配列の型</typeparam>
        /// <param name="a">1次元の要素数</param>
        /// <param name="b">2次元の要素数</param>
        /// <param name="defaultValue">デフォルト値</param>
        /// <returns>指定した条件で初期化された配列</returns>
        public static T[,] Array2D<T>(int a, int b, T defaultValue)
            where T : struct
        {
            var ret = new T[a, b];
            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < b; j++)
                {
                    ret[i, j] = defaultValue;
                }
            }

            return ret;
        }

        public static T[,,] Array3D<T>(int a, int b, int c, T defaultValue)
            where T : struct
        {
            T[,,] ret = new T[a, b, c];

            for (int i = 0; i < a; i++)
            {
                for (int j = 0; j < b; j++)
                {
                    for (int k = 0; k < c; k++)
                    {
                        ret[i, j, k] = defaultValue;
                    }
                }
            }

            return ret;
        }

        /// <summary>
        /// 要素数 (a, b) の、defaultValue で満たされたJag配列を作成します。
        /// </summary>
        /// <typeparam name="T">配列の型</typeparam>
        /// <param name="a">1次元の要素数</param>
        /// <param name="b">2次元の要素数</param>
        /// <param name="defaultValue">デフォルト値</param>
        /// <returns>指定した条件で初期化された配列</returns>
        public static T[][] JagArray2D<T>(int a, int b, T defaultValue)
            where T : struct
        {
            var ret = new T[a][];
            for (int i = 0; i < a; i++)
            {
                var row = new T[b];
                ret[i] = row;

                for (int j = 0; j < b; j++)
                {
                    row[j] = defaultValue;
                }
            }

            return ret;
        }

        /// <summary>
        /// 要素数 (a, b) のdefault値で満たされたJag配列を作成します。
        /// </summary>
        /// <typeparam name="T">配列の型</typeparam>
        /// <param name="a">1次元の要素数</param>
        /// <param name="b">2次元の要素数</param>
        /// <returns>初期化された配列</returns>
        public static T[][] JagArray2D<T>(int a, int b)
            where T : struct
        {
            var ret = new T[a][];
            for (int i = 0; i < a; i++)
            {
                ret[i] = new T[b];
            }

            return ret;
        }

        public static T[][][] JagArray3D<T>(int a, int b, int c, T defaultValue)
            where T : struct
        {
            var ret = new T[a][][];
            for (int i = 0; i < a; i++)
            {
                T[][] aRow = new T[b][];
                ret[i] = aRow;

                for (int j = 0; j < b; j++)
                {
                    T[] bRow = new T[c];
                    aRow[j] = bRow;

                    for (int k = 0; k < c; k++)
                    {
                        bRow[k] = defaultValue;
                    }
                }
            }

            return ret;
        }

        public static T[][][] JagArray3D<T>(int a, int b, int c)
            where T : struct
        {
            var ret = new T[a][][];
            for (int i = 0; i < a; i++)
            {
                T[][] aRow = new T[b][];
                ret[i] = aRow;

                for (int j = 0; j < b; j++)
                {
                    T[] bRow = new T[c];
                    aRow[j] = bRow;
                }
            }

            return ret;
        }

        public static T[][][][] JagArray4D<T>(int a, int b, int c, int d, T defaultValue)
            where T : struct
        {
            var ret = new T[a][][][];
            for (int i = 0; i < a; i++)
            {
                T[][][] aRow = new T[b][][];
                ret[i] = aRow;

                for (int j = 0; j < b; j++)
                {
                    T[][] bRow = new T[c][];
                    aRow[j] = bRow;

                    for (int k = 0; k < c; k++)
                    {
                        T[] cRow = new T[d];
                        bRow[k] = cRow;

                        for (int l = 0; l < d; l++)
                        {
                            cRow[l] = defaultValue;
                        }
                    }
                }
            }

            return ret;
        }

        public static T[][][][] JagArray4D<T>(int a, int b, int c, int d)
            where T : struct
        {
            var ret = new T[a][][][];
            for (int i = 0; i < a; i++)
            {
                T[][][] aRow = new T[b][][];
                ret[i] = aRow;

                for (int j = 0; j < b; j++)
                {
                    T[][] bRow = new T[c][];
                    aRow[j] = bRow;

                    for (int k = 0; k < c; k++)
                    {
                        T[] cRow = new T[d];
                        bRow[k] = cRow;
                    }
                }
            }

            return ret;
        }

        public static T[][][][][] JagArray5D<T>(int a, int b, int c, int d, int e, T defaultValue)
            where T : struct
        {
            var ret = new T[a][][][][];
            for (int i = 0; i < a; i++)
            {
                T[][][][] aRow = new T[b][][][];
                ret[i] = aRow;

                for (int j = 0; j < b; j++)
                {
                    T[][][] bRow = new T[c][][];
                    aRow[j] = bRow;

                    for (int k = 0; k < c; k++)
                    {
                        T[][] cRow = new T[d][];
                        bRow[k] = cRow;

                        for (int l = 0; l < d; l++)
                        {
                            T[] dRow = new T[e];
                            cRow[l] = dRow;

                            for (int m = 0; m < e; m++)
                            {
                                dRow[m] = defaultValue;
                            }
                        }
                    }
                }
            }

            return ret;
        }

        public static T[][][][][] JagArray5D<T>(int a, int b, int c, int d, int e)
            where T : struct
        {
            var ret = new T[a][][][][];
            for (int i = 0; i < a; i++)
            {
                T[][][][] aRow = new T[b][][][];
                ret[i] = aRow;

                for (int j = 0; j < b; j++)
                {
                    T[][][] bRow = new T[c][][];
                    aRow[j] = bRow;

                    for (int k = 0; k < c; k++)
                    {
                        T[][] cRow = new T[d][];
                        bRow[k] = cRow;

                        for (int l = 0; l < d; l++)
                        {
                            T[] dRow = new T[e];
                            cRow[l] = dRow;
                        }
                    }
                }
            }

            return ret;
        }

        /// <summary>
        /// ジャグ配列をコピーして返す。
        /// </summary>
        public static T[][] CopyJagArray2D<T>(T[][] source)
            where T : struct
        {
            int   len  = source.Length;
            T[][] dest = new T[len][];

            for (int i = 0; i < len; i++)
            {
                T[] inner       = source[i];
                int innerLength = inner.Length;
                T[] newer       = new T[innerLength];
                Array.Copy(inner, newer, innerLength);
                dest[i] = newer;
            }

            return dest;
        }
    }

    public class IntLargeComparer : IComparer<int>
    {
        public static IntLargeComparer Instance = new IntLargeComparer();

        public int Compare(int x, int y)
        {
            return y - x;
        }
    }

    public class LongLargeComparer : IComparer<long>
    {
        public static LongLargeComparer Instance = new LongLargeComparer();

        public int Compare(long x, long y)
        {
            if (x > y) return -1;
            if (x < y) return 1;
            return 0;
        }
    }

    public static class UtakaExtentions
    {
        /// <summary>
        /// 配列をコピーします
        /// </summary>
        public static T[] Copy<T>(this IList<T> a)
        {
            T[] ret = new T[a.Count];

            for (int i = 0; i < a.Count; i++) ret[i] = a[i];

            return ret;
        }
    }

    public class ConsoleInput
    {
        private readonly char          _separator = ' ';
        private readonly TextReader    _stream;
        private readonly Queue<string> inputStream;

        public ConsoleInput(TextReader stream, char separator = ' ')
        {
            _separator  = separator;
            _stream     = stream;
            inputStream = new Queue<string>();
        }

        public string Read
        {
            get
            {
                if (inputStream.Count != 0)
                {
                    return inputStream.Dequeue();
                }

                var tmp = _stream.ReadLine().Split(_separator);
                for (var i = 0; i < tmp.Length; ++i)
                {
                    inputStream.Enqueue(tmp[i]);
                }

                return inputStream.Dequeue();
            }
        }

        public string ReadLine   => _stream.ReadLine();
        public int    ReadInt    => int.Parse(Read);
        public long   ReadLong   => long.Parse(Read);
        public double ReadDouble => double.Parse(Read);

        public string[] ReadStrArray(long N)
        {
            var ret = new string[N];
            for (long i = 0; i < N; ++i)
            {
                ret[i] = Read;
            }

            return ret;
        }

        public int[] ReadIntArray(long N)
        {
            var ret = new int[N];
            for (long i = 0; i < N; ++i)
            {
                ret[i] = ReadInt;
            }

            return ret;
        }

        public int[] ReadIntArrayMinus1(long N)
        {
            var ret = new int[N];
            for (long i = 0; i < N; ++i)
            {
                ret[i] = ReadInt - 1;
            }

            return ret;
        }

        public long[] ReadLongArray(long N)
        {
            var ret = new long[N];
            for (long i = 0; i < N; ++i)
            {
                ret[i] = ReadLong;
            }

            return ret;
        }
    }

    public interface IConsole
    {
        void Flush();
        void Write(object obj);
        void Write(string str);
        void WriteLine(object obj);
        void WriteLine(string str);
        void WriteLine();
    }

    public class MyConsole : IConsole
    {
        public MyConsole()
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
            Console.SetOut(sw);
        }

        public void Flush()
        {
            Console.Out.Flush();
        }

        public void Write(object obj)
        {
            Write(obj.ToString());
        }

        public void Write(string str)
        {
            Console.Write(str);
        }

        public void WriteLine(object obj)
        {
            WriteLine(obj.ToString());
        }

        public void WriteLine(string str)
        {
            Console.WriteLine(str);
        }

        public void WriteLine()
        {
            Console.WriteLine();
        }
    }

#if UTAKA_LOCAL

    public class DebugConsole : IConsole
    {
        private readonly StringBuilder mSb;

        public DebugConsole()
        {
            mSb = new StringBuilder();
        }

        public void Flush()
        {
            // 何もしない
        }

        public void Write(object obj)
        {
            Write(obj.ToString());
        }

        public void Write(string str)
        {
            mSb.Append(str);
        }

        public void WriteLine(object obj)
        {
            WriteLine(obj.ToString());
        }

        public void WriteLine(string str)
        {
            mSb.AppendLine(str);
        }

        public void WriteLine()
        {
            mSb.AppendLine();
        }

        public string GetAllOutput()
        {
            return mSb.ToString();
        }
    }

#endif

#if UTAKA_LOCAL

    public class TestCaseCheckerForAtCoder
    {
        private string GetDirectoryPath()
        {
            var problemPart = string.IsNullOrEmpty(Program.ProblemName)
                ? ""
                : $"/{Program.ProblemName}";

            return
                $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/AlgorithmPrac/{Program.SiteName}/{Program.ContestName}{problemPart}";
        }

        private string GetInputFilePath(int testCaseNumber)
        {
            return $"{GetDirectoryPath()}/sample-{testCaseNumber}.in";
        }

        private string GetOutputFilePath(int testCaseNumber)
        {
            return $"{GetDirectoryPath()}/sample-{testCaseNumber}.out";
        }

        public bool TestProblem(int testCaseNumber)
        {
            var inputFilePath  = GetInputFilePath(testCaseNumber);
            var outputFilePath = GetOutputFilePath(testCaseNumber);

            TextReader inputStream = new StreamReader(inputFilePath);
            var        cin         = new ConsoleInput(inputStream);

            var debugConsoleWriter = new DebugConsole();

            new Program(cin, debugConsoleWriter).Solve();

            var output = debugConsoleWriter.GetAllOutput();

            TextReader outputStream = new StreamReader(outputFilePath);
            var        outputAnswer = outputStream.ReadToEnd();

            Dbg.WriteLine(output);
            Dbg.WriteLine(outputAnswer);

            var isCorrect = output == outputAnswer;

            return isCorrect;
        }

        public void TestProblems(int targetTestCaseNumber = -1)
        {
            if (targetTestCaseNumber >= 0)
            {
                Console.WriteLine($"!!!!!!!!!!!! Check TestCase {targetTestCaseNumber} !!!!!!!!!!");
            }

            bool isSuccessAll = true;

            int testCaseNumber = 0;
            while (true)
            {
                testCaseNumber++;

                bool foundTargetTest = targetTestCaseNumber == testCaseNumber;

                if (targetTestCaseNumber >= 0 && !foundTargetTest)
                {
                    continue;
                }

                var inputFileName = GetInputFilePath(testCaseNumber);
                if (!File.Exists(inputFileName))
                {
                    break;
                }

                Console.WriteLine($"TestCase {testCaseNumber} =====================================================");

                var result = TestProblem(testCaseNumber);

                if (result)
                {
                    Console.WriteLine("Success");
                }
                else
                {
                    isSuccessAll = false;
                    Console.WriteLine("Failure *****");
                }

                if (foundTargetTest)
                {
                    break;
                }
            }

            if (isSuccessAll)
            {
                Console.WriteLine("!!!!!!!!! All Success !!!!!!!!!");
            }
        }
    }

#endif

    public static class Dbg
    {
        [Conditional("UTAKA_DEBUG")]
        public static void WriteLine(string str)
        {
            Console.WriteLine(str);
        }

        [Conditional("UTAKA_DEBUG")]
        public static void WriteLine()
        {
            Console.WriteLine();
        }

        [Conditional("UTAKA_DEBUG")]
        public static void Write(string str)
        {
            Console.Write(str);
        }
    }
}