<p>You have a pointer at index <code>0</code> in an array of size <code>arrLen</code>. At each step, you can move 1 position to the left, 1 position to the right in the array, or stay in the same place (The pointer should not be placed outside the array at any time).</p>

<p>Given two integers <code>steps</code> and <code>arrLen</code>, return the number of ways such that your pointer is still at index <code>0</code> after <strong>exactly</strong> <code>steps</code> steps. Since the answer may be too 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>

<pre>
<strong>Input:</strong> steps = 3, arrLen = 2
<strong>Output:</strong> 4
<strong>Explanation: </strong>There are 4 differents ways to stay at index 0 after 3 steps.
Right, Left, Stay
Stay, Right, Left
Right, Stay, Left
Stay, Stay, Stay
</pre>

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

<pre>
<strong>Input:</strong> steps = 2, arrLen = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are 2 differents ways to stay at index 0 after 2 steps
Right, Left
Stay, Stay
</pre>

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

<pre>
<strong>Input:</strong> steps = 4, arrLen = 2
<strong>Output:</strong> 8
</pre>

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

<ul>
	<li><code>1 &lt;= steps &lt;= 500</code></li>
	<li><code>1 &lt;= arrLen &lt;= 10<sup>6</sup></code></li>
</ul>

class Solution:
    def numWays(self, steps: int, arrLen: int) -> int:
        if arrLen == 1:
            return 1

        MOD = 10**9 + 7
        # the maxium of index we can reach
        # this also reduce the step
        n = min(steps // 2 + 1, arrLen)
        # 1. State
        # dp[i][j]: number of total case that after i-th step, we are at index j
        dp = [[0] * n for _ in range(steps + 1)]
        # 2. initialization
        # at 0-th step, we only have 1 possible way to get index 0, which is stay
        dp[0][0] = 1

        # 3. function
        # for each i-th, the way to get to index j come from 
        # 3.1 stay: dp[i-1][j]
        # 3.2 move to left: dp[i-1][j+1]
        # 3.3 move to the right: dp[i-1][j-1]
        # edge case: if we are out of the boudary, then 
        # dp[i-1][j+1] = dp[i-1][j-1] = 0
        for step in range(1, steps + 1):
            for index in range(n):
                # if we stay
                dp[step][index] = dp[step-1][index]
                if index > 0:
                    # we are sure we can come back
                    # since n is min(steps//2 + 1, arrLen)
                    dp[step][index] += dp[step-1][index-1]
                if index < n - 1:
                    dp[step][index] += dp[step-1][index+1]

        
        return dp[steps][0] % MOD
                



25020