<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 Solution:
    def goodTriplets(self, nums1: List[int], nums2: List[int]) -> int:
        n = len(nums1)
        
        # Step 1: Map the indices of nums1 elements in nums2
        index_map = {val: i for i, val in enumerate(nums2)}
        
        # Step 2: Convert nums1 to the corresponding indices in nums2
        nums1 = [index_map[val] for val in nums1]
        
        # Step 3: Use a Fenwick Tree (Binary Indexed Tree) to count smaller and larger elements
        def count_smaller_larger(nums):
            tree = FenwickTree(n)
            smaller = [0] * n
            larger = [0] * n
            for i in range(n):
                tree.update(nums[i], 1)
                smaller[i] = tree.query(nums[i] - 1)
            
            tree = FenwickTree(n)
            for i in reversed(range(n)):
                tree.update(nums[i], 1)
                larger[i] = tree.query(n - 1) - tree.query(nums[i])
            
            return smaller, larger
        
        # Step 4: Count the good triplets
        smaller, larger = count_smaller_larger(nums1)
        total_good_triplets = 0
        for i in range(1, n - 1):
            total_good_triplets += smaller[i] * larger[i]
        
        return total_good_triplets

class FenwickTree:
    def __init__(self, n):
        self.sums = [0] * (n + 1)
    
    def update(self, index, delta):
        index += 1
        while index < len(self.sums):
            self.sums[index] += delta
            index += index & -index
    
    def query(self, index):
        index += 1
        result = 0
        while index > 0:
            result += self.sums[index]
            index -= index & -index
        return result

# Example usage:
solution = Solution()
nums1 = [2, 0, 1, 3]
nums2 = [0, 1, 2, 3]
print(solution.goodTriplets(nums1, nums2))  # Output: 1

nums1 = [4, 0, 1, 3, 2]
nums2 = [4, 1, 0, 2, 3]
print(solution.goodTriplets(nums1, nums2))  # Output: 4
1707