<p>You are given 3 positive integers <code>zero</code>, <code>one</code>, and <code>limit</code>.</p>

<p>A <span data-keyword="binary-array">binary array</span> <code>arr</code> is called <strong>stable</strong> if:</p>

<ul>
	<li>The number of occurrences of 0 in <code>arr</code> is <strong>exactly </strong><code>zero</code>.</li>
	<li>The number of occurrences of 1 in <code>arr</code> is <strong>exactly</strong> <code>one</code>.</li>
	<li>Each <span data-keyword="subarray-nonempty">subarray</span> of <code>arr</code> with a size greater than <code>limit</code> must contain <strong>both </strong>0 and 1.</li>
</ul>

<p>Return the <em>total</em> number of <strong>stable</strong> binary arrays.</p>

<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 1, limit = 2</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>The two possible stable binary arrays are <code>[1,0]</code> and <code>[0,1]</code>.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 1, one = 2, limit = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">1</span></p>

<p><strong>Explanation:</strong></p>

<p>The only possible stable binary array is <code>[1,0,1]</code>.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">zero = 3, one = 3, limit = 2</span></p>

<p><strong>Output:</strong> <span class="example-io">14</span></p>

<p><strong>Explanation:</strong></p>

<p>All the possible stable binary arrays are <code>[0,0,1,0,1,1]</code>, <code>[0,0,1,1,0,1]</code>, <code>[0,1,0,0,1,1]</code>, <code>[0,1,0,1,0,1]</code>, <code>[0,1,0,1,1,0]</code>, <code>[0,1,1,0,0,1]</code>, <code>[0,1,1,0,1,0]</code>, <code>[1,0,0,1,0,1]</code>, <code>[1,0,0,1,1,0]</code>, <code>[1,0,1,0,0,1]</code>, <code>[1,0,1,0,1,0]</code>, <code>[1,0,1,1,0,0]</code>, <code>[1,1,0,0,1,0]</code>, and <code>[1,1,0,1,0,0]</code>.</p>
</div>

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

<ul>
	<li><code>1 &lt;= zero, one, limit &lt;= 1000</code></li>
</ul>

class Solution:
    def numberOfStableArrays(self, zero: int, one: int, limit: int) -> int:
        MOD = 10**9+7

        memo = [[None]*(one+1) for _ in range(zero+1)]
        def count_sum_zeros(zeros, ones):
            if zeros>zero or ones>one:
                return (0,0)
            if memo[zeros][ones] is None:
                memo[zeros][ones] = count_sum(zeros, ones)
            return memo[zeros][ones]
            
        def count_sum(zeros, ones):
            if ones==one:
                ans0 = 1 if 1+zeros+limit>zero else 0
                for new_zeros in range(zeros+1,zero):
                    val = count_sum_zeros(new_zeros, ones)[1]
                    ans0 += val
            else:
                ans0 = count_sum_zeros(zeros+1,ones)[0]\
                -count_sum_zeros(1+zeros+limit, ones)[1]\
                +count_sum_zeros(zeros+1,ones)[1]
            
            ans1 = 0
            if zeros==zero:
                for new_ones in range(ones+1,ones+limit+1):
                    if new_ones==one and zeros==zero:
                        val = 1
                    else:
                        val = count_sum_zeros(zeros, new_ones)[0]
                    ans1 += val
            else:
                ans1 = count_sum_zeros(zeros,ones+1)[1]\
                -count_sum_zeros(zeros, 1+ones+limit)[0]\
                +count_sum_zeros(zeros,ones+1)[0]
            return (ans0%MOD,ans1%MOD)
        ans = count_sum_zeros(0,0)
        return (ans[0]+ans[1])%(MOD)
450100