scala
scala\nimport scala.collection.mutable\n\nobject Solution {\n  def countSubMultisets(nums: List[Int], l: Int, r: Int): Int = {\n    val M = 1000000007\n    var 
```scala\nimport scala.collection.mutable\n\nobject Solution {\n  def countSubMultisets(nums: List[Int], l: Int, r: Int): Int = {\n    val M = 1000000007\n    var total = 0\n    val cnt = mutable.HashMap.empty[Int, Int].withDefaultValue(0)\n    nums.foreach(x => {\n      total += x\n      cnt(x) += 1\n    })\n    if (l > total) return 0\n\n    val r1 = r.min(total)\n    val f = Array.fill[Int](r1 + 1)(0)\n    f(0) = cnt.getOrElse(0, 0) + 1\n    cnt.remove(0)\n\n    var sum = 0\n    cnt.foreach { case (x, c) =>\n      val newF = f.clone()\n      sum = math.min(sum + x * c, r1)\n      (x to sum).foreach(j => {\n        newF(j) = (newF(j) + newF(j - x)) % M\n        if (j >= (c + 1) * x) newF(j) = (newF(j) - f(j - (c + 1) * x) + M) % M\n      })\n      Array.copy(newF, 0, f, 0, newF.length)\n    }\n\n    var res = 0\n    (l to r1).foreach(i => res = (res + f(i)) % M)\n    res\n  }\n}\n```
['Scala']