<p>Given an integer array&nbsp;<code>nums</code>, return <em>the number of longest increasing subsequences.</em></p>

<p><strong>Notice</strong> that the sequence has to be <strong>strictly</strong> increasing.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,5,4,7]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7].
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,2,2,2,2]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2000</code></li>
	<li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
	<li>The answer is guaranteed to fit inside a 32-bit integer.</li>
</ul>

class Solution:
    def findNumberOfLIS(self, nums: List[int]) -> int:
        # store the number of various lengths of longest increasing subsequence
        def dfs(index):
            if index in cache: return cache[index]
            max_val = 1
            max_val_count = 1
            for next_index in range(index+1, len(nums)):
                if nums[next_index] > nums[index]:
                    next_val, next_count = dfs(next_index)
                    new_val = 1 + next_val
                    if new_val > max_val: 
                        max_val = new_val
                        max_val_count = next_count
                    elif new_val == max_val:
                        max_val_count += next_count

            cache[index] = (max_val, max_val_count)
            return cache[index]
        
        cache = {}
        cache[len(nums)-1] = (1, 1)

        for i in range(len(nums)):
            dfs(i)
    
        max_val, max_count = 0, 0
        for val, count in cache.values():
            if val > max_val:
                max_val = val
                max_count = count
            elif val == max_val:
                max_count += count
        return max_count
        
17700