<p>Given an array <code>nums</code> of <strong>distinct</strong> positive integers, return <em>the number of tuples </em><code>(a, b, c, d)</code><em> such that </em><code>a * b = c * d</code><em> where </em><code>a</code><em>, </em><code>b</code><em>, </em><code>c</code><em>, and </em><code>d</code><em> are elements of </em><code>nums</code><em>, and </em><code>a != b != c != d</code><em>.</em></p>

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

<pre>
<strong>Input:</strong> nums = [2,3,4,6]
<strong>Output:</strong> 8
<strong>Explanation:</strong> There are 8 valid tuples:
(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)
(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,4,5,10]
<strong>Output:</strong> 16
<strong>Explanation:</strong> There are 16 valid tuples:
(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)
(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)
(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)
(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li>All elements in <code>nums</code> are <strong>distinct</strong>.</li>
</ul>

class Solution:
    def tupleSameProduct(self, nums: List[int]) -> int:
        nums = list(set(nums))  # Only consider unique ints.
        ints_count = len(nums)
        if ints_count < 4:
            return 0

        # For each unique product, track all tuples achieving it.
        products, current_product = dict(), 0
        start_idx, end_idx = 0, 1
        while True:
            if end_idx >= ints_count:
                if start_idx >= end_idx - 1:
                    break

                start_idx += 1
                end_idx += start_idx + 1 - end_idx
                continue

            current_product += nums[start_idx] * nums[end_idx] - current_product
            if current_product not in products.keys():
                products.update({current_product: []})

            products[current_product].append((nums[start_idx], nums[end_idx]))
            end_idx += 1

        count = 0
        for product in products.values():
            product_len = len(product)
            if product_len == 2:
                count += 8  # For two tuples each having 2 ints, permutation = 2 * 2 * 2 = 8.
                continue

            if product_len > 2:
                # Combination of 2 out of n is n * (n - 1) / 2.
                # Each combination has multiple of 8. So outer multiple is 8 / 2 = 4
                count += (product_len - 1) * product_len * 4

        return count

104200