<p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> containing all numbers from <code>1</code> to <code>n</code>, return <em>the number of increasing quadruplets</em>.</p>

<p>A quadruplet <code>(i, j, k, l)</code> is increasing if:</p>

<ul>
	<li><code>0 &lt;= i &lt; j &lt; k &lt; l &lt; n</code>, and</li>
	<li><code>nums[i] &lt; nums[k] &lt; nums[j] &lt; nums[l]</code>.</li>
</ul>

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

<pre>
<strong>Input:</strong> nums = [1,3,2,4,5]
<strong>Output:</strong> 2
<strong>Explanation:</strong> 
- When i = 0, j = 1, k = 2, and l = 3, nums[i] &lt; nums[k] &lt; nums[j] &lt; nums[l].
- When i = 0, j = 1, k = 2, and l = 4, nums[i] &lt; nums[k] &lt; nums[j] &lt; nums[l]. 
There are no other quadruplets, so we return 2.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There exists only one quadruplet with i = 0, j = 1, k = 2, l = 3, but since nums[j] &lt; nums[k], we return 0.
</pre>

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

<ul>
	<li><code>4 &lt;= nums.length &lt;= 4000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= nums.length</code></li>
	<li>All the integers of <code>nums</code> are <strong>unique</strong>. <code>nums</code> is a permutation.</li>
</ul>

class Solution:
    def countQuadruplets(self, nums: List[int]) -> int:
        n = len(nums)
        lt = [[0] * n for _ in range(n)] # n by m matrix (i , j)
        gt = [[0] * n for _ in range(n)]

        # such that gt[i][j] is the count of all elements nums[j] from (i, j)
        # that are greater than nums[i]
        for i in range(0, n, 1):
            greater = 0
            for j in range(i + 1, n, 1):
                greater += (nums[i] < nums[j])
                gt[i][j] = greater
        # such that lt[j][i] is the count of all elements num[j] from (j, i)
        # that are less than nums[i]
        for i in range(0, n, 1):
            less = 0
            for j in range(i - 1, -1, -1):
                less += (nums[j] < nums[i])
                lt[j][i] = less
        
        res = 0
        for j in range(0, n - 2): # n - 2 b/c need a k and l index (i < j < k < l)
            for k in range(j+1, n -1): # need an l index
                if nums[j] > nums[k]:
                    poss_i = lt[0][k] - lt[j][k]
                    poss_l = gt[j][n-1] - gt[j][k]
                    res += poss_i * poss_l
        return res

646800