<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 Node:
    def __init__(self):
        self.add = 0
        self.value = 0

class Segtree:
    def __init__(self):
        self.arr = []

    def init(self, n):
        self.arr = [Node() for _ in range(4 * n)]

    def add(self, a, b):
        mod = int(1e9 + 7)
        return (a + b) % mod

    def update(self, a, b, l, r, i, d1, d2):
        if l > r:
            return 0, 0
        cur = self.arr[i]
        if l > b or r < a:
            cur.value = self.add((r - l + 1) * d2, cur.value)
            cur.add += d2
            return 0, cur.value
        if l >= a and r <= b:
            cur.value += (r - l + 1) * (d1 + d2)
            cur.add += d1 + d2
            return cur.value, cur.value
        m = (r + l) // 2
        vl = self.update(a, b, l, m, 2 * i + 1, d1, cur.add + d2)
        vr = self.update(a, b, m + 1, r, 2 * i + 2, d1, cur.add + d2)
        cur.add = 0
        cur.value = self.add(vl[1], vr[1])
        return vl[0] + vr[0], vl[1] + vr[1]

class Solution:
    def sumCounts(self, nums):
        n = len(nums)
        mp = {}
        ans = 0
        cur = 0
        t = Segtree()
        t.init(n)
        for i, v in enumerate(nums):
            prev = mp.get(v, -1)
            a, b = t.update(prev + 1, i, 0, n - 1, 0, 1, 0)
            cur = cur + a * 2 - (i - prev)
            ans = t.add(cur, ans)
            mp[v] = i
        return ans
7281