<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>

import numpy as np

class Solution:
    def countQuadruplets(self, A: List[int]) -> int:
        n = len(A)
        dp = [0]*n  # Number of growing i->k->j ending at j.
        res = 0
        for j, v in enumerate(A):
            cnt = 0   # cnt_j counts A[i] < A[j] seen so far
            for i in range(j):
                if A[i] > v:
                    dp[i] += cnt  # (we found cnt of {i},j,k = (optimised),i,k)
                else:
                    res += dp[i]  # our i,j = desc j,l ; 
                    cnt += 1      # our i,j = desc i,k
        return res
                    

class Solution2:
    def countQuadruplets(self, nums: List[int]) -> int:
        n = len(nums)
        pre  = [[0] * n for _ in range(n)]   #np.zeros((n,n), dtype=int)
        post = [[0] * n for _ in range(n)]   #np.zeros((n,n), dtype=int)
        for k in range(2, n-1):
            cum = int(nums[0] < nums[k])
            for j in range(1, k):
                if nums[j] > nums[k]:
                    pre[k][j] = cum
                cum += int(nums[j] < nums[k])
        for j in range(n-3, 0, -1):
            cum = int(nums[n-1] > nums[j])
            for k in range(n-2, j, -1):
                if nums[j] > nums[k]:
                    post[k][j] = cum
                cum += int(nums[k] > nums[j])
        #print(pre)
        #print(post)
        ret = 0
        for k in range(2, n-1):
            for j in range(n-3, 0, -1):
                ret += pre[k][j] * post[k][j]
        return ret
29100