Scala
\nimport scala.util.chaining.scalaUtilChainingOps\n\nobject Solution {\n  private object Color extends Enumeration {\n    type Color = Value\n    val Red, Blue,
```\nimport scala.util.chaining.scalaUtilChainingOps\n\nobject Solution {\n  private object Color extends Enumeration {\n    type Color = Value\n    val Red, Blue, Green = Value\n  }\n  import Color._\n\n  def colorTheGrid(m: Int, n: Int): Int = {\n    val validCols = (1 to m).foldLeft(Iterable(List[Color]())) {\n      case (cols, _) => cols.flatMap {\n        case Nil => Color.values.iterator.map(List(_))\n        case head :: tail => Color.values.iterator.collect {\n          case color if color != head => color :: head :: tail\n        }\n      }\n    }\n\n    (2 to n)\n      .foldLeft(validCols.map(_ -> 1)) {\n        case (counts, _) => validCols.map { col1 =>\n          counts\n            .collect {\n              case (col2, count) if col1.iterator.zip(col2).forall { case (color1, color2) => color1 != color2 } =>\n                count\n            }\n            .fold(0)(add)\n            .pipe(col1 -> _)\n        }\n      }\n      .map(_._2)\n      .fold(0)(add)\n  }\n\n  private def add(x: Int, y: Int) = (x + y) % 1000000007\n}\n```
['Dynamic Programming', 'Scala']