<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:
        """
        1. 定義狀態
        dp(step, position) --> 耗費 step 步且停在 position 的方法數量
        target --> dp(step, position=0)

        2. 轉換方程
        dp(step, position)
        =前一步從左邊走過來 + 前一步從右邊走過來 + 前一步剛好同一格
        =dp(step-1, position=左) + dp(step-1, position=右) + dp(step-1, position=原地)
        = dp(step-1, position-1) + dp(step-1, position+1) + dp(step-1, position)

        3. 定義初始
        dp(step=0, position=0) = 1
        dp(step<0, position) = 0 ## 用盡步數，不能再走
        dp(step0, position<0 or position >= arrlen) = 0 ## 超出邊界不能走

        """
        ### Top Down (DP Table)
        dp_table = dict()
        contraint = 10**9 + 7

        def dp_count(step: int, position: int) -> int:
            if (step, position) in dp_table:
                ### Look Up 
                return dp_table[step, position]
            
            if step < 0 or not(0 <= position < arrLen):
                ## process edge case
                return 0
            
            if step == 0 and position == 0:
                ## base case
                return 1
            
            ## general case
            curr = (
                dp_count(step-1, position-1) +
                dp_count(step-1, position+1) + 
                dp_count(step-1, position)
            ) % contraint

            dp_table[step, position] = curr

            return curr
        
        return dp_count(step=steps, position=0)

79396