> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Sum of Subsequence Widths Python Solution

> Tested Python solution for LeetCode 891 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 891, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/sum-of-subseq-widths/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 891   # by problem number
lcpy gen -s sum_of_subseq_widths   # by problem name
```

## Problem

The **width** of a sequence is the difference between the maximum and minimum elements in the sequence.

Given an array of integers `nums`, return *the sum of the* ***widths*** *of all the non-empty* ***subsequences*** *of* `nums`. Since the answer may be very large, return it **modulo** `10^9 + 7`.

A **subsequence** is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, `[3,6,2,7]` is a subsequence of the array `[0,3,1,6,2,2,7]`.

### Examples

```
Input: nums = [2,1,3]
Output: 6
Explanation: The subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3].
The corresponding widths are 0, 0, 0, 1, 1, 2, 2.
The sum of these widths is 6.
```

```
Input: nums = [2]
Output: 0
```

### Constraints

* `1 <= nums.length <= 10^5`
* `1 <= nums[i] <= 10^5`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sum_of_subseq_widths/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sum_of_subseq_widths/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(1)
    def sum_subseq_widths(self, nums: list[int]) -> int:
        mod = 10**9 + 7
        ordered = sorted(nums)
        total = 0
        n = len(ordered)
        pow2 = 1
        for i, value in enumerate(ordered):
            total += value * (pow2 - 1) - value * (pow(2, n - 1 - i, mod) - 1)
            total %= mod
            pow2 = pow2 * 2 % mod
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.