fun intList() = readLine()?.split(" ")?.map(String::toInt) ?: TODO()
fun main() {
    val n = readLine()?.toInt() ?: return
    val a = intList()
    val m = 1000000007L
    var t = 3L
    if (a.first() != 0) {
        println("0")
        return
    }
    val numbers = hashMapOf<Int, Int>()
    numbers[0] = 1
    a.drop(1).forEach {
        val b = numbers.getOrPut(it) { 0 }
        numbers[it] = b + 1
        if (it == 0) {
            t = t * (3 - b) % m
        } else {
            t = t * (numbers.getOrDefault(it - 1, 1) - b) % m
        }
    }
    println(t.toString())
}
