<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 BIT:
    # ranged query and ranged update 
    # see https://cp-algorithms.com/data_structures/fenwick.html
    def __init__(self, size):
        self.n = size
        self.B1 = [0] * (self.n+1)
        self.B2 = [0] * (self.n+1)

    def incr(self, s, e):
        # [s, e]
        self.rangeUpdate(s, e, 1)

    def rangeUpdate(self, s, e, d):
        s += 1
        e += 1
        self._add(self.B1, s, d)
        self._add(self.B1, e+1, -d)
        self._add(self.B2, s, d*(s-1))
        self._add(self.B2, e+1, -d*e)

    def _add(self, B, k, d):
        while k <= self.n:
            B[k] += d
            k += k & -k

    def rangeQuery(self, s, e):
        # [s, e]
        s, e = s+1, e+1
        return self._sum(e) - self._sum(s-1)

    def _sum(self, k):
        return self._sumOf(self.B1, k) * k - self._sumOf(self.B2, k)

    def _sumOf(self, B, k):
        ans = 0
        while k > 0:
            ans += B[k]
            k -= k & -k
        return ans


class Solution:
    def sumCounts(self, nums: List[int]) -> int:
        MOD = 10 ** 9 + 7
        N = len(nums)
        B = BIT(N + 5)
        lookup = defaultdict(lambda: -1)
        res = 0
        dp = 0

        for r in range(N):
            l = lookup[nums[r]]
            dp = (dp + 2 * B.rangeQuery(l + 1, r) + (r - l)) % MOD
            res += dp
            res %= MOD
            B.incr(l + 1, r)
            lookup[nums[r]] = r


        return res
3456