<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> representing the strength of some heroes. The<b> power</b> of a group of heroes is defined as follows:</p>

<ul>
	<li>Let <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, ... ,<code>i<sub>k</sub></code> be the indices of the heroes in a group. Then, the power of this group is <code>max(nums[i<sub>0</sub>], nums[i<sub>1</sub>], ... ,nums[i<sub>k</sub>])<sup>2</sup> * min(nums[i<sub>0</sub>], nums[i<sub>1</sub>], ... ,nums[i<sub>k</sub>])</code>.</li>
</ul>

<p>Return <em>the sum of the <strong>power</strong> of all <strong>non-empty</strong> groups of heroes possible.</em> Since the sum could be very large, return it <strong>modulo</strong> <code>10<sup>9 </sup>+ 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,1,4]
<strong>Output:</strong> 141
<strong>Explanation:</strong> 
1<sup>st</sup>&nbsp;group: [2] has power = 2<sup>2</sup>&nbsp;* 2 = 8.
2<sup>nd</sup>&nbsp;group: [1] has power = 1<sup>2</sup> * 1 = 1. 
3<sup>rd</sup>&nbsp;group: [4] has power = 4<sup>2</sup> * 4 = 64. 
4<sup>th</sup>&nbsp;group: [2,1] has power = 2<sup>2</sup> * 1 = 4. 
5<sup>th</sup>&nbsp;group: [2,4] has power = 4<sup>2</sup> * 2 = 32. 
6<sup>th</sup>&nbsp;group: [1,4] has power = 4<sup>2</sup> * 1 = 16. 
​​​​​​​7<sup>th</sup>&nbsp;group: [2,1,4] has power = 4<sup>2</sup>​​​​​​​ * 1 = 16. 
The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141.

</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> A total of 7 groups are possible, and the power of each group will be 1. Therefore, the sum of the powers of all groups is 7.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def sumOfPower(self, nums: List[int]) -> int:
        # power = max^2 * min
        # have to sum that for all subsets (2**n)
            # subset is only diff if it has diff min/max
        # 2 heaps: calc all the subsets that contain min and max?
        # if it was just min, would be relatively easy
            # but have to cross product with all possible maxes
            # heap[0] is the min for:
                #{heap0} + all subsets of heap[1:] -> 2**(len(heap)-1)
            # take some min, max -> there are 2**(n-2) subsets that contain that pair
                # but there are n^2 min/max pairs...
                # every max^2*min is unique?
                    # except if max < min, then 
            # can go through each min, calc min*subsets
                # then distribute max^2 over those?
                    # can calc the % of mins that max would apply to?
                        # min + min + min + min ...
                            # given max applies to say 2 of them,
                            # can be represented as max^2*2/n
                # e.g. for the smallest min, it's the min of every subset that includes it
                    # for the biggest max, it's in 2**(n-2) of those
                    # for the 2nd biggest max, it's in 2**(n-3) of those
                    # so need to track:
                        # min -> its total sum and its n
                            # but still can't apply a max to an array
                                # so need more like max^2 * total_num_mins_it_applies_to / 2**n
                # x+x+x+x+x+ y+y+y+y -> 5x + 4y
                    # want to get (mx+mx+mx+mx+x + my+my+my+y) = 4mx + x + 3my + y
                    # diff is: (m - 1)*(4x + 3y) -> 
                    # 2nd round is mx
                # x^2y*2^(n-2) + x^2(y+1)*2^(n-3) + ... (x-1)^2*y*2^(n-3) + (x-1)^2*(y+1)*2^(n-4)
                # x^2*(some_val) + (x-1)^2*(other_val) + ...
                    # to multiply some_val + other_val + ... by x^2
                        # (target + rest) -> x^2*target + rest
                        # (xy+z)/(y+z)
                        # xy+z - (y+z) -> (x-1)y
                        # can we track y=target? maybe, there's only 1 for each max
                        # target is sum(yi*2^(n-2-i)) for max
                            # sum(yi*2^(n-2-i-1)) for 2max, ... -> just ymax/2
                                # 10 is min -> only applies when 10 is also max
                                # so for 9=max, should omit the min=10 case
                                    # ie y9 = (y10 - 10*2^0) / 2
                                    # y8 -> (y9 - 9*2^1) / 2
                                        # ie, remove the case of 9 as min
                                        # and, divide by 2 to omit all the cases that included 9
                                #[1,2,3,4]
                                # 1*2**2 + 2*2 + 3 + 4
                                    # for max of 3 -> 1*2 + 2 + 3
                                        # so actually, subtract 4, divide by 2 EXCEPT ourselves
                                            # can handle that by adding ourselves back in once lol before the divide
                                    # max of 2 -> 1 + 2
                                    # when we collide with ourselves, n only goes down by 1
        nums.sort()
        y = 0
        n = len(nums)
        mod = 10**9+7
        for i, cmin in enumerate(nums[:-1]):
            y = (y + cmin*pow(2, (n-i-2), mod)) % mod
        y += nums[-1]
        ret = 0
        inv2 = pow(2, -1, mod)
        for i in range(n-1, -1, -1):
            maxi = n-i-1
            v = nums[i]
            if i < n-1:
                y = ((y-nums[i+1] + nums[i]) * inv2) % mod
            ret = (ret + v**2 * y) % mod
        return ret

526