import scala.io.StdIn object Solution { def main(args: Array[String]): Unit = { val n = StdIn.readInt() val m = StdIn.readLine().split(" ").map(_.toInt) val scores = (n - 1 to 0 by -1).foldLeft(Vector.fill(n)(IndexedSeq.empty[Int])) { (s, i) => val v = (1 to limit(m, i) - i).map { j => if (i + j == n) 1 else { val max = j min s(i + j).length val t = (0 until max).foldLeft((0, 1)) { (state, k) => val a = mult(state._2, j - k) (sum(state._1, mult(s(i + j)(k), a)), a) } t._1 } } s.updated(i, v) } println(scores(0).foldLeft(0) { (total, score) => sum(total, score) }) } def limit(m: IndexedSeq[Int], i: Int): Int = { if (i == m.size - 1 || m(i + 1) < m(i)) i + 1 else limit(m, i + 1) } def mult(a: Int, b: Int): Int = (a.toLong * b % 1000000007).toInt def sum(a: Int, b: Int): Int = (a + b) % 1000000007 }