val MOD = 1000000007

fun main(args : Array<String>) {
    val n = readLine()!!.toInt()
    val aa = readLine()!!.split(" ").map { it.toInt() }
    val hats = IntArray(3) { 0 }

    var sum = 1L

    for (a in aa) {
        val pattern = hats.count { it == a }
        if (pattern == 0) {
            println(0)
            return
        }

        hats[hats.indexOfFirst { it == a }]++
        sum *= pattern
        sum %= MOD
    }

    println(sum)
}