You have a pointer at index 0 in an array of size arrLen. 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).

Given two integers steps and arrLen, return the number of ways such that your pointer still at index 0 after exactly steps steps. Since the answer may be too large, return it modulo 109 + 7.

&nbsp;
Example 1:

Input: steps = 3, arrLen = 2
Output: 4
Explanation: 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


Example 2:

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


Example 3:

Input: steps = 4, arrLen = 2
Output: 8


&nbsp;
Constraints:


	1 &lt;= steps &lt;= 500
	1 &lt;= arrLen &lt;= 106


class Solution:
    def numWays(self, steps: int, arrLen: int) -> int:
        
        # obtain maximum index we can reach
        maxLen = min(steps+1, arrLen)
        dp = [0]*maxLen
        dp[0] = 1
        
        for step in range(1, steps+1):
            dp2 = [0]*maxLen
            for pos in range(maxLen):
                # dp[step][pos] = dp[step-1][pos] + dp[step-1][pos-1] + dp[step-1][pos+1] 
                temp1 = 0 if pos == 0 else dp[pos-1]
                temp2 = 0 if pos == maxLen-1 else dp[pos+1]
                dp2[pos] = (dp[pos] + temp1 + temp2)%(10**9+7)
                
            dp = dp2
        return dp[0]
class Solution {
    
    HashMap<String,Long> map = new HashMap();
    
    public int numWays(int steps, int arrLen) {
        
        return (int) (ways(steps,arrLen,0) % ((Math.pow(10,9)) +7));
    }
    
    public long ways(int steps,int arrLen,int index){
        String curr = index + "->" + steps;
        
        if(index == 0 && steps == 0){
            return 1;
        }else if(index < 0 || (index >= arrLen) || steps == 0){
            return 0;
        }
        
        if(map.containsKey(curr)){
            return map.get(curr);
        }
        long stay = ways(steps-1,arrLen,index);
        long right = ways(steps-1,arrLen,index+1);
        long left = ways(steps-1,arrLen,index-1);
        
        map.put(curr , (stay+right+left) % 1000000007);
        
        return (right + left + stay) % 1000000007;
    }
}
class Solution {
public:
    int n, MOD = 1e9 + 7;
    int numWays(int steps, int arrLen) {
        n = arrLen;
        vector<vector<int>> memo(steps / 2 + 1, vector<int>(steps + 1, -1));
        return dp(memo, 0, steps) % MOD;
    }
    long dp(vector<vector<int>>& memo, int i, int steps){
        if(steps == 0)
            return i == 0;
        if(i < 0 || i == n || i > steps)
            return 0;
        if(memo[i][steps] != -1)
            return memo[i][steps];
        return memo[i][steps] = (dp(memo, i, steps - 1) + dp(memo, i - 1, steps - 1) + dp(memo, i + 1, steps - 1)) % MOD;
    }
};
var numWays = function(steps, arrLen) {
  let memo = Array(steps + 1).fill(0).map(() => Array(steps + 1).fill(-1)), mod = 10 ** 9 + 7;
  return dp(0, steps);
  
  function dp(i, steps) {
    if (steps === 0) return i === 0 ? 1 : 0; // found a way
    if (i > steps || i < 0 || i >= arrLen) return 0; // out of bounds
    if (memo[i][steps] !== -1) return memo[i][steps]; // memoized
    
    let moveLeft = dp(i - 1, steps - 1);
    let moveRight = dp(i + 1, steps - 1);
    let stay = dp(i, steps - 1);
    return memo[i][steps] = (moveLeft + moveRight + stay) % mod;
  }
};
Number of Ways to Stay in the Same Place After Some Steps