<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>.</p>

<p>The <strong>distinct count</strong> of a subarray of <code>nums</code> is defined as:</p>

<ul>
	<li>Let <code>nums[i..j]</code> be a subarray of <code>nums</code> consisting of all the indices from <code>i</code> to <code>j</code> such that <code>0 &lt;= i &lt;= j &lt; nums.length</code>. Then the number of distinct values in <code>nums[i..j]</code> is called the distinct count of <code>nums[i..j]</code>.</li>
</ul>

<p>Return <em>the sum of the <strong>squares</strong> of <strong>distinct counts</strong> of all subarrays of </em><code>nums</code>.</p>

<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,1]
<strong>Output:</strong> 15
<strong>Explanation:</strong> Six possible subarrays are:
[1]: 1 distinct value
[2]: 1 distinct value
[1]: 1 distinct value
[1,2]: 2 distinct values
[2,1]: 2 distinct values
[1,2,1]: 2 distinct values
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15.
</pre>

<p><strong>Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Three possible subarrays are:
[2]: 1 distinct value
[2]: 1 distinct value
[2,2]: 1 distinct value
The sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3.</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>5</sup></code></li>
</ul>


# @lc code=start
class Solution:

    def sumCounts(self, nums: List[int]) -> int:
        n = len(nums)
        MOD = 10**9 + 7

        # tree 表示求和
        tree = [0 for _ in range(2 << n.bit_length())]
        # 值可能为0时，dirty设置为-1
        dirty = [-1 for _ in range(2 << n.bit_length())]

        def push_down(o, l, r):
            if dirty[o] == -1: return
            mid = (l + r) >> 1
            set_value(o * 2 + 1, l, mid, l, mid, dirty[o])
            set_value(o * 2 + 2, mid + 1, r, mid + 1, r, dirty[o])
            dirty[o] = -1

        def set_value(o, l, r, L, R, val):
            # o: 根节点 l：区间左端点 r：区间右端点 LR：更新下标范围 val：更新值
            mid = (l + r) >> 1
            if l == L and r == R:
                tree[o] += val * (r - l + 1)
                if dirty[o] != -1:
                    dirty[o] += val
                else:
                    dirty[o] = val
                return
            push_down(o, l, r)
            if R <= mid:
                set_value(o * 2 + 1, l, mid, L, R, val)
            elif mid + 1 <= L:
                set_value(o * 2 + 2, mid + 1, r, L, R, val)
            else:
                set_value(o * 2 + 1, l, mid, L, mid, val)
                set_value(o * 2 + 2, mid + 1, r, mid + 1, R, val)
            process(o, l, r)

        def query(o, l, r, L, R):
            if l == L and r == R:
                return tree[o]
            push_down(o, l, r)
            mid = (l + r) >> 1
            if R <= mid:
                return query(o * 2 + 1, l, mid, L, R)
            if mid + 1 <= L:
                return query(o * 2 + 2, mid + 1, r, L, R)
            return query(o * 2 + 1, l, mid, L, mid) + query(o * 2 + 2, mid + 1, r, mid + 1, R)

        def process(o, l, r):
            if l == r: return
            a, b = tree[o * 2 + 1], tree[o * 2 + 2]
            tree[o] = a + b

        ans, pre = 0, 0
        lst = defaultdict(lambda: -1)
        for i, num in enumerate(nums):
            ii = lst[num]
            pre += (2 * query(0, 0, n - 1, ii + 1, i) + i - ii) % MOD
            # print('=', pre, query(0, 0, n - 1, ii + 1, i), i - ii)
            # print('set ', ii + 1, i)
            set_value(0, 0, n - 1, ii + 1, i, 1)
            ans = (ans + pre) % MOD
            lst[num] = i
        return ans

7192