<p>You are given two <strong>0-indexed</strong> arrays <code>nums1</code> and <code>nums2</code> of length <code>n</code>, both of which are <strong>permutations</strong> of <code>[0, 1, ..., n - 1]</code>.</p>

<p>A <strong>good triplet</strong> is a set of <code>3</code> <strong>distinct</strong> values which are present in <strong>increasing order</strong> by position both in <code>nums1</code> and <code>nums2</code>. In other words, if we consider <code>pos1<sub>v</sub></code> as the index of the value <code>v</code> in <code>nums1</code> and <code>pos2<sub>v</sub></code> as the index of the value <code>v</code> in <code>nums2</code>, then a good triplet will be a set <code>(x, y, z)</code> where <code>0 &lt;= x, y, z &lt;= n - 1</code>, such that <code>pos1<sub>x</sub> &lt; pos1<sub>y</sub> &lt; pos1<sub>z</sub></code> and <code>pos2<sub>x</sub> &lt; pos2<sub>y</sub> &lt; pos2<sub>z</sub></code>.</p>

<p>Return <em>the <strong>total number</strong> of good triplets</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [2,0,1,3], nums2 = [0,1,2,3]
<strong>Output:</strong> 1
<strong>Explanation:</strong> 
There are 4 triplets (x,y,z) such that pos1<sub>x</sub> &lt; pos1<sub>y</sub> &lt; pos1<sub>z</sub>. They are (2,0,1), (2,0,3), (2,1,3), and (0,1,3). 
Out of those triplets, only the triplet (0,1,3) satisfies pos2<sub>x</sub> &lt; pos2<sub>y</sub> &lt; pos2<sub>z</sub>. Hence, there is only 1 good triplet.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 4 good triplets are (4,0,3), (4,0,2), (4,1,3), and (4,1,2).
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums1.length == nums2.length</code></li>
	<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums1[i], nums2[i] &lt;= n - 1</code></li>
	<li><code>nums1</code> and <code>nums2</code> are permutations of <code>[0, 1, ..., n - 1]</code>.</li>
</ul>

class segtree:
    def __init__(self, arr = None, f = lambda a, b : a + b):
        if type(arr) == int: 
            self.n = arr + 1
            arr = [0] * self.n
        self.n = len(arr)
        self.tree = [0] * 2 * self.n
        self.f = f
        self._build(arr)

    def _build(self, arr):
        self.tree[self.n : 2*self.n] = arr
        for i in range(self.n - 1, 0, -1):
            self.tree[i] = self.f(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, index: int, val: int) -> None:
        index += self.n
        self.tree[index] = val
        while index > 1:
            self.tree[index // 2] = self.f(self.tree[index], self.tree[index ^ 1])
            index >>= 1
            
    def __setitem__(self, index: int, val: int) -> None:
        self.update(index, val)

    def query(self, left: int, right: int) -> int:
        left, right = left + self.n, right + self.n
        res = 0

        while left <= right:
            if left & 1:
                res = self.f(res, self.tree[left])
                left += 1
            if not right & 1:
                res = self.f(res, self.tree[right])
                right -= 1
            left, right = left >> 1, right >> 1
        return res
    
    def __getitem__(self, key: int | slice) -> int:
        if isinstance(key, slice):
            return self.query(0 if key.start is None else key.start, 
                self.n - 1 if key.stop is None else key.stop)
        return self.tree[key + self.n]
        
    def __str__(self):
        return f'Arr: {self.tree[self.n:]}' \
            # + f'\nNodes: {self.tree[:self.n]}'

class Solution:
    def goodTriplets(self, nums1: List[int], nums2: List[int]) -> int:
        n = len(nums1)
        store = {idx : i for i, idx in enumerate(nums1)}
        nums1 = [store[nums2[i]] for i in range(n)]

        print(nums1)

        st = segtree(n)
        smaller = []

        for i in nums1:
            smaller.append(st[:i - 1])
            st[i] += 1

        res = 0
        st = segtree(n)
        for i, small in zip(nums1[::-1], smaller[::-1]):
            print(i, small, st[i+1:])
            res += st[i + 1:] * small
            st[i] += 1
        
        return res
45200