<p>A die simulator generates a random number from <code>1</code> to <code>6</code> for each roll. You introduced a constraint to the generator such that it cannot roll the number <code>i</code> more than <code>rollMax[i]</code> (<strong>1-indexed</strong>) consecutive times.</p>

<p>Given an array of integers <code>rollMax</code> and an integer <code>n</code>, return <em>the number of distinct sequences that can be obtained with exact </em><code>n</code><em> rolls</em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>Two sequences are considered different if at least one element differs from each other.</p>

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

<pre>
<strong>Input:</strong> n = 2, rollMax = [1,1,2,2,2,3]
<strong>Output:</strong> 34
<strong>Explanation:</strong> There will be 2 rolls of die, if there are no constraints on the die, there are 6 * 6 = 36 possible combinations. In this case, looking at rollMax array, the numbers 1 and 2 appear at most once consecutively, therefore sequences (1,1) and (2,2) cannot occur, so the final answer is 36-2 = 34.
</pre>

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

<pre>
<strong>Input:</strong> n = 2, rollMax = [1,1,1,1,1,1]
<strong>Output:</strong> 30
</pre>

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

<pre>
<strong>Input:</strong> n = 3, rollMax = [1,1,1,2,2,3]
<strong>Output:</strong> 181
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 5000</code></li>
	<li><code>rollMax.length == 6</code></li>
	<li><code>1 &lt;= rollMax[i] &lt;= 15</code></li>
</ul>

"""
solution 1: backtrack - O(6^N) in worst case
"""
class Solution:
    def dieSimulator(self, n: int, rollMax: List[int]) -> int:
        def backtrack(curr_roll, curr_comb):
            if curr_roll == n:
                self.res += 1
                return
            
            for num in [1, 2, 3, 4, 5, 6]:
                if len(curr_comb) >= rollMax[num-1] and \
                all(cons_num == num for cons_num in curr_comb[-rollMax[num-1]:]):
                    continue      
                curr_comb.append(num)
                backtrack(curr_roll + 1, curr_comb)
                curr_comb.pop()
                
        
        self.res = 0
        backtrack(0, [])
        return self.res
        
        
"""
solution 1: a little modified backtrack - O(6^N) in worst case.
"""
class Solution:
    def dieSimulator(self, n: int, rollMax: List[int]) -> int:
        def backtrack(curr_roll, last_num, repeat_time):
            """
            we only need last_num and the repeat time of last_num
            """
            if curr_roll == n:
                self.res += 1
                return
            
            for num in [1, 2, 3, 4, 5, 6]:
                if num == last_num:
                    if repeat_time >= rollMax[num-1]:     
                        continue                        
                    backtrack(curr_roll + 1, num, repeat_time + 1)
                else:
                    backtrack(curr_roll + 1, num, 1)
                
        
        self.res = 0
        backtrack(0, -1, -1)    
        return self.res
        
        
"""
solution 2: backtrack + memo - O(n^2).
"""
class Solution:
    def dieSimulator(self, n: int, rollMax: List[int]) -> int:
        def backtrack(curr_roll, last_num, repeat_time):
            if (curr_roll, last_num, repeat_time) in memo:
                return memo[(curr_roll, last_num, repeat_time)]
            
            if curr_roll == n:
                return 1
            if curr_roll > n:
                return 0
            
            res = 0
            for num in [1, 2, 3, 4, 5, 6]:
                if num == last_num:
                    if repeat_time >= rollMax[num-1]:     
                        continue                       
                    res = (res + backtrack(curr_roll + 1, num, repeat_time + 1) % MOD) % MOD 
                else:
                    res = (res + backtrack(curr_roll + 1, num, 1) % MOD) % MOD
            
            memo[(curr_roll, last_num, repeat_time)] = res
            return res
                
        
        MOD = 10**9 + 7
        memo = collections.defaultdict(int)     # (curr_roll, last_num, repeat_time) --> how many ways
        return backtrack(0, 0, 0)
73131