<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:
        MOD = 10**9 + 7
        n = len(nums)
        nums.sort()

        dp = [(nums[0] ** 3) % MOD] * n

        def get_prefix_sum(arr):
            prefix_sums = [0] * len(arr)
            suma = 0
            for i, el in enumerate(arr):
                prefix_sums[i] = (suma + el) % MOD
                suma += el % MOD
            return prefix_sums


        prefix_sums = get_prefix_sum(nums)
        sum_prefix_sums = get_prefix_sum(prefix_sums)
        """
        [a,b,c,d] a ^ 3 
        b^ 3 + b^2*a
        [c]. [c,b], [c,a] , [c,a,b]  c^ 3 + c^2(b + a + a) b + 2a
        [d] [d, a] , [d,b] , [d,c] [d,a,b] [d,a,c] [d,b,c] [d,a,b,c] = d^ 3 + (a + b + c + a + a + b + a) * d ^ 2 = (4a + 2b + c)
        """
        prev_add = 0
        prev_num = nums[0]
        for i in range(1, n):
            curr_num = nums[i] % MOD
            curr_add = (prev_add * 2 + prev_num) % MOD
            # add prev result
            dp[i] = dp[i - 1] % MOD
            dp[i] %= MOD
            # add
            dp[i] += (curr_add * (curr_num % MOD) * (curr_num % MOD)) % MOD
            dp[i] %= MOD
            # ADD 3rd power
            dp[i] += ((curr_num % MOD) * (curr_num % MOD) * (curr_num % MOD)) % MOD
            dp[i] %= MOD

            prev_num = nums[i]
            prev_add = curr_add % MOD

        return dp[n-1] % MOD
526