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


// breakDown Hard to easy [recursion]
// Trust on yourself - you are doing better 99% students 

// point-1 You have a pointer at index 0 in an array of size arrLen.
// point-2 each steps, 
// you can move left by 1 position
// you can move right by 1 position
// you can stay into same position 
// you have number of steps that you can take

// helper(index,steps) => helper(0,0) (how many times hits- the number of ways such that your pointer is still at index 0 after exactly step)

// Striver DP Series [rules]
// Try all the possible ways
//1. express everything in terms of indexes
//2. explore all paths
//3. sum up all possible ways [paths]
        

class Solution {
private:
// Since the answer may be too large, return it modulo 109 + 7.
   int mod = 1e9+7;
   int helper(int index,int steps, int n, vector<vector<long long int>> & dp){
      // base case 1
      // left side ja rahe ho (negative mat jana)
      // right side ja rahe ho (arrayLen se mat jana)
       if(index < 0 || index >= n ){
           return 0;
       }
       // base case-2 
       // there are no number of steps
       if(steps < 0) return 0;

       // base case-2
       //  your pointer is still at index 0 
       if(index == 0 && steps == 0){
          return 1;
       }

       // memorization of recursive calls
       // time complexity 0(1)
       if(dp[steps][index] != -1){
          return dp[steps][index];
       }

      // large number of value (store used long long int)
      // every recusive need to do modificatin by large number MOD
      long long int left = helper(index-1,steps-1,n,dp) % mod;
      long long int right = helper(index+1,steps-1,n,dp) % mod;
      long long int staySame = helper(index,steps-1,n,dp) % mod;
      long long int TotalNumberWays = (left + right + staySame) % mod;
      return dp[steps][index] = TotalNumberWays;
   }
public:
    int numWays(int steps, int arrLen) {
         if(arrLen > steps) 
               arrLen = steps;
        int n = arrLen;
        int index = 0;
        // TLE
        // DP matrix used -> long long int (to store the large value)
        vector<vector<long long int>> dp(steps+1,vector<long long int>(arrLen+1,-1));
        return helper(index,steps,n,dp);
    }
};














19850