We need to compute sum over all non-empty subsets of the set of n points of the maximum distance between any two points in the subset. The points are on a line, coordinates distinct. The distance is absolute difference: for subset S, F(S) = max_{i,j in S} |x_i - x_j| = max_{i in S} x_i - min_{i in S} x_i (since sorted). So it's the range.
We need sum_{S ⊆ [n], S ≠ ∅} (max(S) - min(S)), where max and min refer to coordinates after sorting. Since coordinates are distinct, ordering is unique.
We need mod 1e9+7.
n up to 3e5, coordinates up to 1e9. Direct enumeration is impossible.
We need to compute sum of ranges over all subsets. Since the range is the difference between max and min, we can try to compute sum of max over subsets, and sum of min over subsets, and subtract.
Define sum_max = ∑{S≠∅} max(S). sum_min = ∑{S≠∅} min(S). Then answer = sum_max - sum_min.
Compute sum_max: For each point i, consider how many subsets have it as the maximum. For a point with coordinate x_i, it is the maximum if all other elements in the subset are from the set of points with coordinate less than x_i. But we need strict max, and coordinates are distinct, so we consider points with smaller coordinates. Let L_i = number of points with coordinate < x_i (i.e., to the left if sorted ascending). Then for any subset where i is the maximum, we can choose any subset of the L_i smaller points (including empty) and include i. So number of subsets where i is max is 2^{L_i}. So sum_max = ∑_{i=1}^{n} x_i * 2^{L_i}.
Similarly, sum_min: For each point i, number of subsets where i is the minimum is 2^{R_i}, where R_i = number of points with coordinate > x_i (points to the right). Because subset must include i and any subset of larger points. So sum_min = ∑_{i=1}^{n} x_i * 2^{R_i}.
Then answer = ∑ x_i * (2^{L_i} - 2^{R_i}). But careful: This counts also subsets of size 1? For size-1 subset, max = min = that element. So in sum_max, for a singleton {i}, it is counted as max: indeed, if subset has only i, then i is both max and min. In sum_max we count it with factor 2^{L_i} includes empty subset of smaller points -> that gives {i} indeed. Similarly sum_min includes it with factor 2^{R_i}. So the difference will cancel the contribution of singletons (since 2^{L_i} - 2^{R_i} maybe not zero? Wait for singleton the contribution to answer is 0 because max-min=0. So in our answer formula using sum_max - sum_min, we subtract them, so we need to ensure that for singleton subsets, x_i appears in both sums and cancels. That's fine if we compute that way. However, is the counting for max and min correct? For a given subset, there is exactly one max and one min, so each subset contributes its max exactly once to sum_max and its min exactly once to sum_min, so the subtraction gives the range. So the formula is correct.
But wait: For max: Subset must contain i and no element with coordinate > x_i. But if there are elements with coordinate equal? Coordinates distinct, so no equality. So exactly i is max iff all other elements have coordinates < x_i. So count = 2^{L_i}. Similarly for min: count = 2^{R_i}. So sum_max = ∑ x_i * 2^{L_i}. sum_min = ∑ x_i * 2^{R_i}.
Thus answer = ∑_{i=1}^{n} x_i * (2^{L_i} - 2^{R_i}).
Now we need to compute L_i and R_i. Sort points by coordinate. Let the sorted order be x_{(1)} < x_{(2)} < ... < x_{(n)}. In sorted order, for position i (1-indexed), L_i = i-1, R_i = n - i. So answer = ∑{i=1}^{n} x{(i)} * (2^{i-1} - 2^{n-i}) mod MOD.
But careful: Are we counting each subset exactly once? For subset size 1, i is both max and min, but our formula subtracts: x_i * (2^{L_i} - 2^{R_i}) For i at sorted position, that term is x_i * (2^{i-1} - 2^{n-i}). For i=1 (smallest), 2^{0} - 2^{n-1} = 1 - 2^{n-1} negative, so contributes negative; similarly for largest, i=n, 2^{n-1} - 2^{0} = 2^{n-1} - 1 positive. The sum over all i yields the sum of ranges. For n=2: sorted x1 < x2: sum = x1*(1 - 2^{1}) + x2*(2^{1} - 1) = x1*(1-2) + x2*(2-1)= -x1 + x2 = x2 - x1 = 3. Good.
For n=3: sorted say x1=1, x2=3, x3=4? Actually example: Input 4 3 1, sorted: 1,3,4. Compute: i=1: 2^{0}=1, 2^{2}=4 => 1-4=-3 => -31 = -3; i=2: 2^{1}=2, 2^{1}=2 => 0 => 03=0; i=3: 2^{2}=4,2^{0}=1 => 3 => 3*4=12; sum = -3+0+12=9. That matches example. So formula is correct.
Therefore we can compute by sorting, precomputing powers of 2 mod MOD up to n, then sum.
Complexities O(n log n) for sorting. n up to 3e5, fine.
But wait: There is a nuance: In original problem, the subset is non-empty and we take max distance between any pair, which is max - min. That's correct.
Thus answer = sum_{i=1}^{n} x_i * (2^{i-1} - 2^{n-i}) mod MOD. But careful: The input coordinates are not necessarily sorted, and we need to use sorted order. So sort first.
But is it that simple? The problem seems classic. However, let's double-check: For any subset, the range is max - min. Sum over all subsets can be computed by pairing contributions of each pair? Alternatively, we can also compute sum of (x_j - x_i) * 2^{j-i-1} for each ordered pair (i,j) with i<j? Because a subset's range is contributed by the farthest points. For each pair (i,j) (sorted), the number of subsets where these two are the min and max? That is: any subset that includes both i and j, and no element outside [i,j]? Actually, if i is min and j is max, then all other elements must be between i and j (i.e., indices in (i,j)). But there may also be subsets where i is min and j is max but also includes some points outside? No, if i is min, then all elements must have coordinates >= x_i, but i is the smallest, so no element smaller; if j is max, then all elements must have coordinates <= x_j, so no element larger. Therefore the only allowed elements are those with indices between i and j inclusive. But if we require i to be min and j to be max, we must ensure that no element smaller than i (none), and no element larger than j (none) are in the subset. But we must also ensure that i is the minimum and j is the maximum. For that, we need to include i and j, and any subset of the interior points (indices i+1,...,j-1). So number of subsets where i is min and j is max is 2^{(j-i-1)}. So the contribution of that pair to the total sum of ranges is (x_j - x_i) * 2^{j-i-1}. Then sum over all pairs i<j gives the total sum? Let's test for n=3 sorted 1,3,4: pairs: (1,2): (3-1)*2^{0}=2; (1,3): (4-1)2^{1}=32=6; (2,3): (4-3)*2^{0}=1. Sum = 2+6+1=9. Yes matches. So alternative method also works.
Our derived formula sum_{i} x_i (2^{i-1} - 2^{n-i}) is equivalent to sum_{i<j} (x_j - x_i) 2^{j-i-1} as can be shown algebraically.
But we need mod 1e9+7. So we can implement with sorting, compute powers of two up to n (since exponents up to n-1), then compute sum.
One caution: Since 2^{n-i} may be large exponent, but we mod. So we need powers mod.
But is there any catch? Check for n=1: Only one computer. Subset: only {1}. Range = 0. So answer should be 0. Our formula: sorted: only i=1, 2^{0} - 2^{0} = 1-1 = 0, so 0. Good.
Now n up to 3e5, sorting O(n log n) fine. Powers precompute: pow2[0] = 1; for i from 1 to n: pow2[i] = 2*pow2[i-1] % MOD.
Then iterate sorted array, compute contribution: (pow2[i] - pow2[n-1-i])? Actually careful: For i (0-indexed after sorting), L_i = i, R_i = n-1-i. So contribution = x[i] * (pow2[i] - pow2[n-1-i]) mod MOD. Summing mod.
But careful: pow2[i] is 2^i. pow2[n-1-i] is 2^{n-1-i}. So contribution = x_i * (2^{i} - 2^{n-1-i}). But earlier we had i 1-indexed: 2^{i-1} - 2^{n-i}. So using 0-index i (starting from 0), it's 2^{i} - 2^{n-1-i}. Let's check with n=2: sorted [x0,x1] (i=0,1). Contribution: i=0: x0*(2^0 - 2^{1}) = x0*(1-2) = -x0; i=1: x1*(2^1 - 2^0) = x1*(2-1) = x1; sum = x1-x0. Good.
For n=3: sorted [1,3,4] i=0: 2^0 - 2^{2}=1-4=-3 => -31=-3; i=1: 2^1-2^{1}=2-2=0 => 0; i=2: 2^2-2^0=4-1=3 => 34=12 => sum=9. Good.
So that works.
Now need to consider mod negative: (pow2[i] - pow2[n-1-i]) may be negative; we take modulo properly: ( (pow2[i] - pow2[n-1-i]) % MOD + MOD) % MOD, or compute as (pow2[i] + MOD - pow2[n-1-i]) % MOD.
So overall answer = ∑ x_i * diff_i mod MOD.
Complexities: O(n log n). Memory: O(n) for arrays and pow2.
Edge Cases: Large n, but powers of 2 mod 1e9+7 we can compute up to n. n=3e5, that's fine.
Now, check if there is any twist? Problem statement says: "Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers." They denote A = {1,...,n}. And F(a) = max_{i,j in a} |x_i - x_j|. Since points on line, it's max - min. That's fine.
But could there be an alternative interpretation that the distance is Euclidean? But they say "straight street", so absolute difference.
Thus solution is straightforward.
Now we need to output single integer.
We'll implement in C++ or Python. Since I'm ChatGPT, I should provide solution explanation. But we can generate final answer.
However, let me double-check with large n, perhaps mod is needed for multiplication: x_i up to 1e9, diff_i up to MOD, product up to ~1e18, but in 64-bit it's okay for Python, but C++ we need long long and mod multiplication: ( (long long) x_i % MOD) * diff_i % MOD (but x_i may be up to 1e9, less than MOD? MOD=1e9+7, which is 1000000007, so x_i ≤ 1e9, less than MOD, so no need to mod x_i before multiplication; product ≤ 1e9 * (MOD-1) ≈ 1e18, fits in 64-bit signed (9e17), but careful: 1e9 * (1e9+7) ≈ 1e18+..., still within 9.22e18. So long long is safe. But we can also mod for safety.
Implementation steps:
- Read n.
- Read list of n integers.
- Sort list ascending.
- Precompute pow2 array size n (since exponent up to n-1, but we need up to n-1? Actually we need pow2[k] for k = 0..n-1 inclusive. So size n. But to compute 2^{n-1-i} we need up to n-1, which is max n-1. So we can compute pow2[0]=1; for i=1..n-1: pow2[i]=2*pow2[i-1]%MOD. Alternatively compute up to n.
- Initialize ans=0.
- For i from 0 to n-1:
diff = pow2[i] - pow2[n-1-i];
if diff < 0: diff += MOD;
ans = (ans + x[i] * diff) % MOD;
- Print ans.
But careful: pow2[n-1-i] must be defined. Since pow2 array length at least n (index up to n-1). So we compute pow2[0..n-1] inclusive.
Time: O(n log n) due to sort.
Check with sample.
Now consider n=3e5, sorting fine.
Edge case: n=1, pow2[0] =1, n-1-i: for i=0, n-1-i =0, diff=0, ans=0.
Now done.
But wait: Another nuance: The expression sum_{S} max - min is indeed sum_max - sum_min, but we must ensure that each subset is counted exactly once. Yes.
Thus answer is straightforward.
But problem seems to be from Codeforces? Possibly titled "Leha and function"? Actually it's "Leha and another game about arrays"? Not sure. But anyway.
Now I'll produce final answer.
But let's see if any alternative approach required due to large n? Could compute using prefix sums? But not needed.
Potential overflow: In C++, we can do:
const int MOD = 1e9+7;
vector x(n);
for i.. read;
sort;
vector pow2(n);
pow2[0]=1;
for i=1;i<n;i++) pow2[i]=(pow2[i-1]*2)%MOD;
long long ans=0;
for i=0;i<n;i++){
int diff = pow2[i] - pow2[n-1-i];
if(diff<0) diff+=MOD;
ans = (ans + (long long)x[i]*diff)%MOD;
}
cout << ans;
Works.
But there is nuance: pow2[i] is defined for i up to n-1. For n-1-i, when i=0 we need pow2[n-1], which is computed. For i=n-1, need pow2[0]. So fine.
Now what about the alternative expression: sum_{i} x_i (2^{i} - 2^{n-1-i})? That matches.
But I recall there might be a trick: The answer can also be expressed as ∑_{i=1}^{n} x_i * (2^{i-1} - 2^{n-i}). So it's essentially the same.
Now, final answer: implement in C++ or Python. Since the problem expects answer modulo 1e9+7.
Now, I'll produce solution.
But wait: Let's double-check with another random small n to be sure.
n=2: [a,b] with a<b.
Sorted: a,b.
pow2[0]=1, pow2[1]=2.
i=0: diff=1-2 = -1 => +MOD = MOD-1? Actually we add MOD to get positive diff. Then contribution = a * (MOD-1) % MOD = MOD - a. i=1: diff=2-1=1 => b*1 = b. Sum = (b + MOD - a) % MOD = (b - a) % MOD. Since b-a positive less than MOD, answer is b-a. Good.
n=3: [1,100,101] sorted: 1,100,101. Compute pow2: [1,2,4]. i=0: diff=1-4=-3 mod = MOD-3; contribution = 1*(MOD-3) = MOD-3; i=1: diff=2-2=0; i=2: diff=4-1=3 => contribution=101*3=303. Sum = (303 + MOD -3) % MOD = 300. Let's compute manually: Subsets: single: 0 each; pairs: {1,100}:99; {1,101}:100; {100,101}:1; triple: {1,100,101}:100; sum = 99+100+1+100=300. Yes.
Now correctness established.
Now potential pitfalls: The sum could be negative before mod, but we mod to positive.
Now complexity: O(n log n) fine.
Now we should produce final solution.
But the problem says "modulo 10^9 + 7", and n up to 3e5, coordinates up to 1e9.
Now, there is also a possibility that we might get TLE if we use Python with large list? But Python can handle 3e5 sort easily. However, computing pow2 with loop O(n) is fine. But careful: In Python, pow2[i] can be computed as pow(2, i, MOD) but doing in loop may be slower? Actually pow(2,i,MOD) each time would be O(log i) but we can precompute in O(n) using multiplication.
But we can also compute answer directly without precomputing all pow2 by using a cumulative approach? Not needed.
Now ensure we mod correctly.
Alternatively, we can compute using formula sum_{i} x_i * (pow(2, i, MOD) - pow(2, n-1-i, MOD)). But precomputing is efficient.
Thus final solution is straightforward.
But wait: Could there be any overflow if we use C++ int for diff? diff computed as int diff = pow2[i] - pow2[n-1-i]; pow2 are int modded, so they are in [0, MOD-1]. diff may be negative but we adjust.
Now, we also need to consider the case when n is large, the pow2 values computed by multiplication may overflow int before mod? We do (pow2[i-1]2) % MOD so no overflow if using long long intermediate: but 2pow2[i-1] is at most 2*(MOD-1) which fits in int? MOD is 1e9+7, so 2*(MOD-1) ~ 2e9, which is within 32-bit signed? 2e9 is close to 2^31-1 ~ 2.147e9, so it's borderline. Actually 2*(1e9+6) = 2e9+12, which is 2000000012 > 2147483647? Let's compute: 1,000,000,007 - 1 = 1,000,000,006. Times 2 = 2,000,000,012. That is less than 2,147,483,647? 2,147,483,647 is about 2.147e9. 2,000,000,012 is less than that? Actually 2,147,483,647 is 2.147e9, so 2,000,000,012 < 2,147,483,647, so it fits within signed 32-bit int. But if we use unsigned int, it's fine. But to be safe, use long long for multiplication or cast to long long before mod. In C++, we can do (1LL * pow2[i-1] * 2) % MOD. Since pow2[i-1] is int, multiply by 2 may still fit, but better to use 1LL to avoid any risk. Or we can use pow2[i] = (pow2[i-1] * 2) % MOD; most compilers treat int multiplication with mod int; but product could be up to 2e9 which is fine for int but if product exceeds 2^31-1, it would overflow to negative and then mod would be wrong. But 2*(MOD-1) = 2,000,000,012 which is less than 2,147,483,647? Actually 2,147,483,647 is maximum signed 32-bit. 2,000,000,012 is less than 2,147,483,647. So it's safe. However, if MOD were larger, it wouldn't be. So it's okay.
But we can also precompute using left shift: pow2[i] = (pow2[i-1] << 1) % MOD. That may overflow if shift yields >32-bit but left shift on int? Actually (pow2[i-1] << 1) is equivalent to multiply by 2, still same range. So safe.
But we can just use long long for clarity.
Now produce final answer.
But wait: There's also possibility to compute without sorting? Possibly using order statistics but we need order anyway. Sorting is fine.
Now finalize.
Now I'll produce solution in C++ and maybe Python.
But the user expects final answer in code? The prompt says: "Print a single integer — the required sum modulo 10^9 + 7." So they want output.
But as ChatGPT, I need to provide final answer (the output) for given input? Actually the problem statement is a typical Codeforces problem, and the user is providing examples with expected outputs. The user likely wants the solution, but the prompt "Leha decided to move..." is the problem description, and they ask to "Print a single integer". But that is part of the problem. The user is not providing a specific test case; they are asking to solve the problem. Usually in such interactions, they want the algorithm explained and maybe final code. Since it's a typical competitive programming problem, I should give a solution explanation and then code.
Given the nature, I'll produce a solution: sort, precompute powers, sum contributions.
But there is also an alternative combinatorial approach: sum over pairs (i,j) distance * 2^{j-i-1}. That might be computed in O(n) after sorting, but it's essentially same.
Now we need to output code. Probably they want code in C++.
I'll output a C++ solution with necessary includes and main.
Now, verify again: In formula, for sorted ascending, contribution x[i]*(2^i - 2^{n-1-i}) . But careful: i from 0 to n-1. So if we precompute pow2 as array of size n, then pow2[k] = 2^k mod MOD. Then contribution = x[i] * (pow2[i] - pow2[n-1-i]) % MOD.
But we must ensure we handle modulo for negative.
Now test on sample:
Input:
2
4 7
Sorted: [4,7]
pow2[0]=1, pow2[1]=2
i=0: diff=1-2 = -1 => +MOD -> MOD-1, contribution = 4*(MOD-1) % MOD = MOD-4? Actually 4*(MOD-1) mod MOD = -4 mod MOD = MOD-4. i=1: diff=2-1=1, contribution=7. Sum = (MOD-4+7) % MOD = (MOD+3) % MOD = 3. Good.
Second sample: Input 4 3 1 sorted [1,3,4]; pow2[0]=1, pow2[1]=2, pow2[2]=4; i=0: diff=1-4=-3 => MOD-3; 1*(MOD-3)=MOD-3; i=1: diff=2-2=0; i=2: diff=4-1=3; 4*3=12; sum = MOD-3+12 = MOD+9 => mod =>9. Good.
Now, if n is large, sum may be large but mod handles.
Now we are done.
But wait, is there any need to consider modulo with subtraction? We need to mod the final answer.
Now, possible further optimization: we can compute ans as sum_{i} x_i * pow2[i] - sum_{i} x_i * pow2[n-1-i] . So we can compute prefix sums? Not needed; O(n) is fine.
Now code:
#include
#include
#include
using namespace std;
const int MOD = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector x(n);
for (int i = 0; i < n; ++i) cin >> x[i];
sort(x.begin(), x.end());
vector pow2(n);
pow2[0] = 1;
for (int i = 1; i < n; ++i) pow2[i] = (pow2[i-1] * 2) % MOD;
long long ans = 0;
for (int i = 0; i < n; ++i) {
int diff = pow2[i] - pow2[n-1-i];
if (diff < 0) diff += MOD;
ans = (ans + (long long) x[i] * diff) % MOD;
}
cout << ans << endl;
return 0;
}
That's fine.
But there is subtle: For n=1, pow2 array size =1, pow2[0]=1, loop i=0: n-1-i = 0, diff=0, ans remains 0.
Now, check memory: vector of ints, size n up to 3e5, fine.
Now, would we get time limit issues? Sorting O(n log n) fine.
Now, alternative: could compute using 2^n? Not needed.
Now, answer accepted.
Now final answer: produce code.
But is there any reason we must consider using long long for diff? diff is int mod MOD, but we multiply with x[i] which is up to 1e9, product may exceed 2^63? Let's check: product up to 1e9 * (MOD-1) ≈ 1e18, less than 2^63 ≈ 9.22e18, so safe in 64-bit signed long long. We'll use long long for ans and intermediate multiplication.
Now, I'll present final solution.
But the user might also want a Python solution? Usually they accept either, but I'll provide C++ as it's typical.
Now, to be thorough, maybe we should mention that we can also solve using the pair approach: sort, then for each i, we can consider contribution of x_i as positive when it's max and negative when it's min. That's the same.
Now final answer.
Now proceed to produce final message with code and explanation? The user asked: "Help the hacker to attend a date." So they want a solution. Usually they want code. So I'll provide solution in C++.
But the prompt says: "Print a single integer — the required sum modulo 109 + 7." So output should be integer.
But I'll provide full solution.
Now also check alternative: Could there be any overflow in pow2 calculation using int if we compute pow2[i] = (pow2[i-1] * 2) % MOD; as said, safe.
Now, final answer.
However, one nuance: Since we compute pow2 up to n-1, and for i from 0 to n-1, we use pow2[n-1-i] which for i=0 uses pow2[n-1] and that's defined. So pow2 array length must be at least n. Actually if we compute pow2[0] to pow2[n-1], length n. So okay.
But what about n=0? n>=1 per constraints.
Now done.
Now I'll produce final answer with code. ```cpp
#include
#include
#include
using namespace std;
const int MOD = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> x(n);
for (int i = 0; i < n; ++i) {
cin >> x[i];
}
sort(x.begin(), x.end());
// precompute powers of two modulo MOD up to n-1
vector<int> pow2(n);
pow2[0] = 1;
for (int i = 1; i < n; ++i) {
pow2[i] = (pow2[i-1] * 2) % MOD;
}
long long ans = 0;
for (int i = 0; i < n; ++i) {
int diff = pow2[i] - pow2[n-1-i];
if (diff < 0) diff += MOD;
ans = (ans + (long long)x[i] * diff) % MOD;
}
cout << ans << '\n';
return 0;
}