import java.io.{BufferedReader, InputStreamReader}

object Main {
  def main(args: Array[String]): Unit = {
    val in = new BufferedReader(new InputStreamReader(System.in))
    val n = in.readLine().toInt
    val as = in.readLine().split("\\s+").map(_.toInt)

    val lasts = Array.ofDim[Int](3)
    var pattern = 1L

    for (
      i <- 0 until n
    ) {
      pattern = pattern * lasts.count(_ == as(i)) % 1000000007
      val index = lasts.indexOf(as(i))
      if ( index != -1) {
        lasts(lasts.indexOf(as(i))) = as(i) + 1
      }
    }

    System.out.println(pattern)
  }
}
