input: from typing import List
  from functools import lru_cache as cache
  def count_valid_permutations(n: int, conditions: List[List[int]]) -> int:
      """
      Given a number n and a 2D list conditions, where conditions[i] = [end_i, cnt_i] specifies the endpoint and the inversion count for each condition.
  
      An inversion for indices (i, j) in an integer list nums is defined as:
  
      i < j and nums[i] > nums[j]
      Return the total permutations of [0, 1, 2, ..., n - 1] such that for every conditions[i], the sequence perm[0..endi] has exactly cnti inversions.
  
      The result could be extremely large, so return it modulo 10^9 + 7.
  
      Examples:
      >>> n = 3, conditions = [[2,2],[0,0]]
      2
      >>> n = 3, conditions = [[2,2],[1,1],[0,0]]
      1
  
      Constraints:
      2 <= n <= 300
      1 <= conditions.length <= n
      conditions[i] = [end_i, cnt_i]
      0 <= end_i <= n - 1
      0 <= cnt_i <= 400
      The input ensures there is at least one condition with end_i == n - 1.
      All end_i values in the input are distinct.
      """
output:     
      MOD = 10**9 + 7
      
      @cache
      def factorial(x):
          if x == 0:
              return 1
          return x * factorial(x - 1) % MOD
      
      @cache
      def inv_factorial(x):
          return pow(factorial(x), MOD - 2, MOD)
      
      def comb(n, k):
          if k > n or k < 0:
              return 0
          return factorial(n) * inv_factorial(k) % MOD * inv_factorial(n - k) % MOD
      
      @cache
      def count_inversions(n, k):
          if k == 0:
              return 1
          if n <= 1:
              return 0
          result = 0
          for i in range(n):
              if k - i >= 0:
                  result += count_inversions(n - 1, k - i) * comb(n - 1, i) % MOD
          return result % MOD
      
      dp = [0] * (n + 1)
      for end, inv in conditions:
          dp[end + 1] = inv
      
      def valid_permutations(current, inversion, dp):
          if inversion < 0:
              return 0
          if inversion == 0 and dp[current] == 0:
              return 1
          result = 0
          for i in range(current):
              if dp[current] - dp[i] >= 0:
                  result += valid_permutations(i, inversion - dp[current] + dp[i], dp) % MOD
          return result % MOD
      
      return valid_permutations(n, dp[-1], dp)
  