<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>

class Solution:
    def sumCounts(self, nums: List[int]) -> int:
        
        lazy = [0] * (len(nums) * 4)
        ttls = [0] * (len(nums) * 4)
        ttls2 = [0] * (len(nums) * 4)

        mod = 10 ** 9 + 7

        def add(idx, l, r, K):
            lenth = r - l + 1
            
            ttls2[idx] = (ttls2[idx] + 2 * K * ttls[idx] + K * K * lenth) % mod
            ttls[idx] = (ttls[idx] + K * lenth) % mod

        def pushdown(idx, l, r):
            if l == r:
                pass

            else:
                lazy[2 * idx + 1] += lazy[idx]
                lazy[2 * idx + 2] += lazy[idx]

                mid = (l + r) // 2
                add(2 * idx + 1, l, mid, lazy[idx])
                add(2 * idx + 2, mid + 1, r, lazy[idx])

            lazy[idx] = 0

        def update(idx, lb, rb, ql, qr):
            if ql <= lb and rb <= qr:
                add(idx, lb, rb, 1)
                lazy[idx] += 1

            elif ql > rb or lb > qr:
                pass

            else:
                if lazy[idx]:
                    pushdown(idx, lb, rb)

                mid = (lb + rb) // 2
                update(idx * 2 + 1, lb, mid, ql, qr)
                update(idx * 2 + 2, mid + 1, rb, ql, qr)

                ttls[idx] = (ttls[2 * idx + 1] + ttls[2 * idx + 2]) % mod
                ttls2[idx] = (ttls2[2 * idx + 1] + ttls2[2 * idx + 2]) % mod

        res = 0
        last = defaultdict(lambda : -1)
        for idx, num in enumerate(nums):

            update(0, 0, len(nums) - 1, last[num] + 1, idx)
            last[num] = idx

            res = (res + ttls2[0]) % mod

        return res

6144