<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 {
public:

    int dp[2020]; 
    int cnt[2020];
    int n;

    int rec(int i, vector<int>& arr){
        //pruning

        //basecase

        //cache check
        if(dp[i] != -1) return dp[i];

        //compute
        int ans = 1;
        int c = 1;
        for(int j = 0; j<i; j++){
            if(arr[i] > arr[j]){
                int temp = 1 + rec(j,arr);
                if(temp > ans){
                    ans = temp;
                    c = cnt[j];
                }
                else if(temp == ans) c = c + cnt[j];
            }
        }

        //save and return
        cnt[i] = c;
        return dp[i] = ans;
    }

    int findNumberOfLIS(vector<int>& arr) {
        n = arr.size();
        memset(dp,-1,sizeof(dp));
        memset(cnt, -1, sizeof(cnt));
        for(int i = 0; i<n; i++){
            cout << rec(i,arr) << " ";
        }
        cout << endl;
        for(int i = 0; i<n; i++){
            cout << cnt[i] << " ";
        }

        int maxlen = 0;
        int ans = 0;
        for(int i = 0; i<n; i++){
            if(rec(i,arr) > maxlen){
                maxlen = rec(i,arr);
                ans = cnt[i];
            }
            else if(rec(i,arr) == maxlen) ans += cnt[i];
        }

        return ans;
    }
};
15600