<p>You are given an array of <strong>distinct</strong> positive integers locations where <code>locations[i]</code> represents the position of city <code>i</code>. You are also given integers <code>start</code>, <code>finish</code> and <code>fuel</code> representing the starting city, ending city, and the initial amount of fuel you have, respectively.</p>

<p>At each step, if you are at city <code>i</code>, you can pick any city <code>j</code> such that <code>j != i</code> and <code>0 &lt;= j &lt; locations.length</code> and move to city <code>j</code>. Moving from city <code>i</code> to city <code>j</code> reduces the amount of fuel you have by <code>|locations[i] - locations[j]|</code>. Please notice that <code>|x|</code> denotes the absolute value of <code>x</code>.</p>

<p>Notice that <code>fuel</code> <strong>cannot</strong> become negative at any point in time, and that you are <strong>allowed</strong> to visit any city more than once (including <code>start</code> and <code>finish</code>).</p>

<p>Return <em>the count of all possible routes from </em><code>start</code> <em>to</em> <code>finish</code>. Since the answer may be too large, return it modulo <code>10<sup>9</sup> + 7</code>.</p>

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

<pre>
<strong>Input:</strong> locations = [2,3,6,8,4], start = 1, finish = 3, fuel = 5
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following are all possible routes, each uses 5 units of fuel:
1 -&gt; 3
1 -&gt; 2 -&gt; 3
1 -&gt; 4 -&gt; 3
1 -&gt; 4 -&gt; 2 -&gt; 3
</pre>

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

<pre>
<strong>Input:</strong> locations = [4,3,1], start = 1, finish = 0, fuel = 6
<strong>Output:</strong> 5
<strong>Explanation:</strong> The following are all possible routes:
1 -&gt; 0, used fuel = 1
1 -&gt; 2 -&gt; 0, used fuel = 5
1 -&gt; 2 -&gt; 1 -&gt; 0, used fuel = 5
1 -&gt; 0 -&gt; 1 -&gt; 0, used fuel = 3
1 -&gt; 0 -&gt; 1 -&gt; 0 -&gt; 1 -&gt; 0, used fuel = 5
</pre>

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

<pre>
<strong>Input:</strong> locations = [5,2,1], start = 0, finish = 2, fuel = 3
<strong>Output:</strong> 0
<strong>Explanation:</strong> It is impossible to get from 0 to 2 using only 3 units of fuel since the shortest route needs 4 units of fuel.
</pre>

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

<ul>
	<li><code>2 &lt;= locations.length &lt;= 100</code></li>
	<li><code>1 &lt;= locations[i] &lt;= 10<sup>9</sup></code></li>
	<li>All integers in <code>locations</code> are <strong>distinct</strong>.</li>
	<li><code>0 &lt;= start, finish &lt; locations.length</code></li>
	<li><code>1 &lt;= fuel &lt;= 200</code></li>
</ul>

class Solution:
    def __init__(self) -> None:
        """
        Initialize a cache which will store the number of paths which include a 
        particular location with a particular amount of fuel
        """

        self.cache = {} # key: (location, fuel), val : routes

    def countRoutes(self, locations: List[int], start: int, finish: int, fuel: int) -> int:
        """
        Returns the number of routes possible from start location to the finish location
        with a particular amount of fuel

        Args:
            locations: List[int] = fuel costs for travelling between locations which are the indices
            start: int = starting location represented by an index
            finish: int = finishing location represented by an index
            fuel: int = amount of fuel at the start of the journey
        
        Returns:
            routes: int = number of possible routes
        """
        
        # Store the modulus in a variable for convenience
        mod = 10 ** 9 + 7

        # Calculate the number of routes from start to finish with starting fuel
        routes = self._travel(locations, start, finish, fuel)

        # Return the number of routes module 10**9 + 7
        return routes % mod

    def _travel(self, locations: List[int], index: int, finish: int, fuel: int) -> int:
        """
        Returns the number of routes possible from start location to the finish location
        with a particular amount of fuel

        Args:
            locations: List[int] = fuel costs for travelling between locations which are the indices
            start: int = starting location represented by an index
            finish: int = finishing location represented by an index
            fuel: int = amount of fuel at the start of the journey
        
        Returns:
            total: int = number of possible routes
        """

        # Initialize the total number of routes 
        total = 0

        # If we are out of fuel, then this current route is invalid and therefore
        # can't be considered in our total
        if fuel < 0:
            return 0

        # if we have already been to this current location with the current fuel level,
        # then we don't need to recalulate the number of valid paths from this state since
        # we have stored that result in our cache and we can return that
        if (index, fuel) in self.cache:
            return self.cache[(index, fuel)]
        
        # If we have a valid amount of fuel and are currently at the finishing point, 
        # this route is valid. However, since we can revisit some other locations, we
        # just increment our total route count and continue visiting the other locations
        if index == finish:
            total += 1

        # Storing the current locations fuel cost for convenience
        currentCost = locations[index]

        # Iterate over the other locations in the array while keeping track of that location's
        # fuel cost
        for nextStop, cost in enumerate(locations):
            # We only explore locations different than the current locationn
            if nextStop != index:
                # We subtract the fuel used to get to the next location
                fuelLeft = fuel - abs(cost - currentCost)
                # We increment total to include all paths which lead to the finish 
                # from the next location
                total += self._travel(locations, nextStop, finish, fuelLeft)
        
        # We store the number of routes for this state in case it is needed later
        # and return the total number of routes
        self.cache[(index, fuel)] = total 
        return total

    
18000