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

import numpy as np

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

        dp0 = np.zeros((zero +1, one +1), dtype=np.int64)
        dp1 = np.zeros((zero +1, one +1), dtype=np.int64)

        # Base cases
        dp0[1:min(zero, limit)+1, 0] = 1
        dp1[0, 1:min(one, limit)+1] = 1

        prefix0 = np.cumsum(dp0, axis=1) % MOD
        prefix1 = np.cumsum(dp1, axis=0) % MOD

        for z in range(zero +1):
            for o in range(one +1):
                if (z <= limit and o ==0) or (o <= limit and z ==0):
                    continue

                if z >0:
                    k_max = min(z, limit)
                    sum1 = prefix1[z -1, o]
                    if z -k_max -1 >=0:
                        sum1 = (sum1 - prefix1[z -k_max -1, o]) % MOD
                    dp0[z, o] = (dp0[z, o] + sum1) % MOD

                if o >0:
                    k_max = min(o, limit)
                    sum0 = prefix0[z, o -1]
                    if o -k_max -1 >=0:
                        sum0 = (sum0 - prefix0[z, o -k_max -1]) % MOD
                    dp1[z, o] = (dp1[z, o] + sum0) % MOD

                # Update prefix0 and prefix1
                if o >0:
                    prefix0[z, o] = (prefix0[z, o -1] + dp0[z, o]) % MOD
                else:
                    prefix0[z, o] = dp0[z, o]

                if z >0:
                    prefix1[z, o] = (prefix1[z -1, o] + dp1[z, o]) % MOD
                else:
                    prefix1[z, o] = dp1[z, o]

        return (dp0[zero, one] + dp1[zero, one]) % MOD

67500