<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, l, r):
        self.l = l
        self.r = r

        self.info = None
        self.assign = None
        self.add = 0

        self.left = None
        self.right = None

    @staticmethod
    def build(nums, l=None, r=None):
        if l is None:
            l = 0
        if r is None:
            r = len(nums) - 1

        node = SegmentTree(l, r)
        if l < r:
            m = (l + r) // 2
            node.left = SegmentTree.build(nums, l, m)
            node.right = SegmentTree.build(nums, m + 1, r)
            node.info = node.left.info + node.right.info
        else:
            node.info = nums[l]
        return node

    def clear(self):
        self.assign = None
        self.add = 0

    def update(self):
        if self.assign is not None:
            self.info = self.assign * (self.r - self.l + 1)
            if self.left:
                self.left.add = 0
                self.left.assign = self.assign
            if self.right:
                self.right.add = 0
                self.right.assign = self.assign
            self.assign = None

        if self.add:
            self.info += self.add * (self.r - self.l + 1)
            if self.left:
                self.left.add += self.add
            if self.right:
                self.right.add += self.add
            self.add = 0

    def singleAssign(self, i, val):
        self.update()

        if self.l == self.r == i:
            self.info = val
        else:
            m = (self.l + self.r) // 2
            if i > m:
                self.right.singleAssign(i, val)
            else:
                self.left.singleAssign(i, val)
            self.info = self.left.info + self.right.info

    def rangeAssign(self, l, r, val):
        self.update()

        if self.l == l and self.r == r:
            self.info = val * (r - l + 1)
            if self.left:
                self.left.clear()
                self.left.assign = val
            if self.right:
                self.right.clear()
                self.right.assign = val
        else:
            m = (self.l + self.r) // 2
            if l > m:
                self.left.update()
                self.right.rangeAssign(l, r, val)
            elif r <= m:
                self.left.rangeAssign(l, r, val)
                self.right.update()
            else:
                self.left.rangeAssign(l, m, val)
                self.right.rangeAssign(m + 1, r, val)
            self.info = self.left.info + self.right.info

    def rangeAdd(self, l, r, val):
        self.update()

        if self.l == l and self.r == r:
            self.info += val * (r - l + 1)
            if self.left:
                self.left.update()
                self.left.add += val
            if self.right:
                self.right.update()
                self.right.add += val
        else:
            m = (self.l + self.r) // 2
            if l > m:
                self.left.update()
                self.right.rangeAdd(l, r, val)
            elif r <= m:
                self.left.rangeAdd(l, r, val)
                self.right.update()
            else:
                self.left.rangeAdd(l, m, val)
                self.right.rangeAdd(m + 1, r, val)
            self.info = self.left.info + self.right.info

    def query(self, l, r):
        if l > r:
            return 0
        self.update()

        if self.l == l and self.r == r:
            return self.info
        m = (self.l + self.r) // 2
        if l > m:
            return self.right.query(l, r)
        if r <= m:
            return self.left.query(l, r)
        return self.left.query(l, m) + self.right.query(m + 1, r)

class Solution:
    def sumCounts(self, nums: List[int]) -> int:
        MOD = 10 ** 9 + 7
        n = len(nums)
        loc = {}
        cur = 0

        root = SegmentTree.build([0] * n)
        ans = 0
        prv = 0
        for i in range(n):
            if nums[i] in loc:
                j = loc[nums[i]]
            else:
                j = -1
                cur += 1
            prv = prv + i - j - 1 + 2 * root.query(j + 1, i - 1) + 1
            ans = (ans + prv) % MOD
            loc[nums[i]] = i
            root.rangeAdd(j + 1, i, 1)

        return ans
7287