<p>You are given two <strong>positive</strong> integers <code>startPos</code> and <code>endPos</code>. Initially, you are standing at position <code>startPos</code> on an <strong>infinite</strong> number line. With one step, you can move either one position to the left, or one position to the right.</p>

<p>Given a positive integer <code>k</code>, return <em>the number of <strong>different</strong> ways to reach the position </em><code>endPos</code><em> starting from </em><code>startPos</code><em>, such that you perform <strong>exactly</strong> </em><code>k</code><em> steps</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>Two ways are considered different if the order of the steps made is not exactly the same.</p>

<p><strong>Note</strong> that the number line includes negative integers.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> startPos = 1, endPos = 2, k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can reach position 2 from 1 in exactly 3 steps in three ways:
- 1 -&gt; 2 -&gt; 3 -&gt; 2.
- 1 -&gt; 2 -&gt; 1 -&gt; 2.
- 1 -&gt; 0 -&gt; 1 -&gt; 2.
It can be proven that no other way is possible, so we return 3.</pre>

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

<pre>
<strong>Input:</strong> startPos = 2, endPos = 5, k = 10
<strong>Output:</strong> 0
<strong>Explanation:</strong> It is impossible to reach position 5 from position 2 in exactly 10 steps.
</pre>

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

<ul>
	<li><code>1 &lt;= startPos, endPos, k &lt;= 1000</code></li>
</ul>

class Solution:
    def __init__(self):
        self.MOD = 10**9+7
        
    def numberOfWays__(self, startPos: int, endPos: int, k: int) -> int:
        allPaths = []
        k += 1
        def BT(currPos, path):
            # print(currPos, path)
            if (currPos == endPos) and (len(path) == k):
                # print("here")
                allPaths.append(path)
                return
            if (len(path) < k):
                BT(currPos - 1, path+[currPos-1]) 
                BT(currPos + 1, path+[currPos+1]) 
        BT(startPos, [startPos])
        return len(allPaths)

    def numberOfWays(self, startPos: int, endPos: int, k: int) -> int:
        # allPaths = []
        k += 1
        @cache
        def BT(currPos, pathlen):
            # print(currPos, path)
            if (currPos == endPos) and (pathlen == k):
                # print("here")
                # allPaths.append(pathlen)
                return 1
            if (pathlen > k): # or (currPos < 0):
                return 0
            return BT(currPos - 1, pathlen+1) + BT(currPos + 1, pathlen+1) 
        # BT(startPos, 1)
        return BT(startPos, 1) % self.MOD
    def numberOfWays__(self, startPos: int, endPos: int, k: int) -> int:
        memo = {} # {0:1, 1:1}
        val = self.helper(startPos, endPos, k, memo)
        # print(memo)
        return val
    def helper(self, startPos, endPos, k, memo):
        if (k == 0) and (startPos == endPos):
            # print("here")
            return 1
        if (k <= 0):
            return 0
        # print(startPos, k, memo)
        if (startPos, k) not in memo:
            memo[(startPos, k)] = self.helper(startPos - 1, endPos, k-1, memo) + self.helper(startPos + 1, endPos, k-1, memo) 
            memo[(startPos, k)] = memo[(startPos, k)] % self.MOD
        return memo[(startPos, k)]
1639