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

from sortedcontainers import SortedList
MOD = 10**9+7
class ST:
    def __init__(self,n,init=0):
        self.trees = [init]*(4*n)
        self.tags = [0]*(4*n)
    def pushup(self,o):
        self.trees[o] = self.trees[2*o]+self.trees[2*o+1]
    def pushdown(self,o,l,r):
        try:
            m = (l+r)//2
            if self.tags[o]:
                self.tags[2*o]+=self.tags[o]
                self.tags[2*o+1]+=self.tags[o]
                self.trees[2*o]+=(m-l+1)*self.tags[o]
                self.trees[2*o+1]+=(r-m)*self.tags[o]
                self.tags[o]=0
        except IndexError as e:
            print(f"IndexError: out of range at o={o}, l={l}, r={r}")
            raise e  # Re-raise the error after printing the information

    def add(self,o,l,r,L,R,val):
        if L<=l<=r<=R:
            self.trees[o]+=val*(r-l+1)
            self.tags[o]+=val
            return
        self.pushdown(o,l,r)
        m = (l+r)//2
        if R<=m: 
            self.add(2*o,l,m,L,R,val)
        elif L>=m+1:
            self.add(2*o+1,m+1,r,L,R,val)
        else:
            self.add(2*o,l,m,L,R,val)
            self.add(2*o+1,m+1,r,L,R,val)
        self.pushup(o)

    def query(self,o,l,r,L,R):
        if L<=l<=r<=R:
            return self.trees[o]
        self.pushdown(o,l,r)
        m = (l+r)//2
        if R<=m: return self.query(2*o,l,m,L,R)
        elif L>=m+1: return self.query(2*o+1,m+1,r,L,R)
        else: return self.query(2*o,l,m,L,R)+self.query(2*o+1,m+1,r,L,R)
class Solution:
    def sumCounts(self, nums: List[int]) -> int:
        sl = SortedList()
        n = len(nums)
        left = [-1]*n
        for i,num in enumerate(nums):
            idx = sl.bisect_left((num,-i))
            if idx<len(sl) and sl[idx][0]==num:
                left[i]=-sl[idx][1]
            sl.add((num,-i))

        f = ST(n)
        ans = 0
        cur = 0
        for i in range(n):
            u,v = left[i]+2,i+1
            cur+=v-u+1+2*f.query(1,1,n,u,v)
            ans+=cur
            ans%=MOD
            f.add(1,1,n,left[i]+2,i+1,1)


        return ans
        
8457