<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, arr: list[int], l: int, r: int):
        self.lazy = 0
        if l == r:
            self.l = self.r = l
            self.left = self.right = None
            self.sum = arr[l]
            self.squareSum = arr[l] * arr[l]
        else:
            self.l = l
            self.r = r
            mid = (r + l) // 2
            self.left = SegmentTree(arr, l, mid)
            self.right = SegmentTree(arr, mid + 1, r)
            self.sum = self.left.sum + self.right.sum
            self.squareSum = self.left.squareSum + self.right.squareSum
    
    def getSquareSum(self, l: int, r: int):
        if l <= self.l and self.r <= r:
            return self.squareSum
        elif self.l > r or self.r < l:
            return 0
        else:
            return self.left.getSquareSum(l, r) + self.right.getSquareSum(l, r)
    
    def add(self, l: int, r: int, k: int):
        if self.lazy > 0:
            selfK = self.lazy
            self.lazy = 0
            self.add(self.l, self.r, selfK)

        if l <= self.l and self.r <= r:
            self.squareSum += (2 * k * self.sum + k * k * (self.r - self.l + 1))
            self.sum += k * (self.r - self.l + 1)
            
            if self.left != None:
                self.left.lazy += k
            if self.right != None:
                self.right.lazy += k
        elif self.l > r or self.r < l:
            return
        else:
            self.left.add(l, r, k)  
            self.right.add(l, r, k)
            self.sum = self.left.sum + self.right.sum
            self.squareSum = self.left.squareSum + self.right.squareSum

class Solution:
    def sumCounts(self, nums: List[int]) -> int:
        ans = 0
        m = 1000000007
        root = SegmentTree([0] * len(nums), 0, len(nums) - 1)
        lastSeen = {}

        for i in range(len(nums)):
            st = 0
            if nums[i] in lastSeen:
                st = lastSeen[nums[i]] + 1
            root.add(st, i, 1)
            ans = (ans + root.getSquareSum(0, len(nums) - 1)) % m
            lastSeen[nums[i]] = i

        return ans
4406