<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 SegmentTree:
    def __init__(self, n):
        self.lzy = [0] * (4 * n)
        self.sum = [0] * (4 * n)
        self.sqr = [0] * (4 * n)
        self.n = n

    def update_lzy(self, l, r, i):
        if l != r:
            self.lzy[2 * i + 1] += self.lzy[i]
            self.lzy[2 * i + 2] += self.lzy[i]
        gap = r - l + 1
        new_sum = (self.sum[i] + self.lzy[i] * gap) % (10**9 + 7)
        new_sqr = (self.sqr[i] + self.lzy[i] * self.sum[i] * 2 + self.lzy[i] * self.lzy[i] * gap) % (10**9 + 7)
        self.sum[i] = new_sum
        self.sqr[i] = new_sqr
        self.lzy[i] = 0

    def add_one(self, x, y, l=0, r=None, i=0):
        if r is None:
            r = self.n - 1
        self.update_lzy(l, r, i)
        if r < x or l > y:
            return
        if l >= x and r <= y:
            self.lzy[i] = 1
            self.update_lzy(l, r, i)
            return
        m = (l + r) // 2
        self.add_one(x, y, l, m, 2 * i + 1)
        self.add_one(x, y, m + 1, r, 2 * i + 2)
        self.sum[i] = (self.sum[2 * i + 1] + self.sum[2 * i + 2]) % (10**9 + 7)
        self.sqr[i] = (self.sqr[2 * i + 1] + self.sqr[2 * i + 2]) % (10**9 + 7)


class Solution:
    def sumCounts(self, nums):
        n = len(nums)
        last_pos = [-1] * 100001
        tree = SegmentTree(n)
        ans = 0
        for j in range(n):
            st = last_pos[nums[j]] + 1
            ed = j
            tree.add_one(st, ed)
            ans = (ans + tree.sqr[0]) % (10**9 + 7)
            last_pos[nums[j]] = j
        return ans
44700