data class RGB(
  var r: Int = 0,
  var g: Int = 0,
  var b: Int = 0
)

const val MOD = 1000000007

fun main(args: Array<String>) {
  val N = readInt()
  val A = readInts()

  var rgbMap = mutableMapOf<RGB, Int>()
  rgbMap[RGB()] = 1
  A.forEach { a ->
    var tmpMap = mutableMapOf<RGB, Int>()
    for ((rgb, count) in rgbMap) {
      if (rgb.r == a) {
        val key = rgb.copy(r = a + 1)
        val v = tmpMap[key]
        if (v == null) {
          tmpMap[key] = count
        } else {
          tmpMap[key] = (v + count) % MOD
        }
      }
      if (rgb.g == a) {
        val key = rgb.copy(g = a + 1)
        val v = tmpMap[key]
        if (v == null) {
          tmpMap[key] = count
        } else {
          tmpMap[key] = (v + count) % MOD
        }
      }
      if (rgb.b == a) {
        val key = rgb.copy(b = a + 1)
        val v = tmpMap[key]
        if (v == null) {
          tmpMap[key] = count
        } else {
          tmpMap[key] = (v + count) % MOD
        }
      }
    }
    rgbMap = tmpMap
  }
  var result = 0
  rgbMap.values.forEach {
    result = (result + it) % MOD
  }
  println(result)
}

fun readString() = readLine()!!
fun readStrings() = readString().split(' ')
fun readInt() = readString().toInt()
fun readInts() = readStrings().map { it.toInt() }
