fun main(args: Array<String>) {
    val mod = 1000000007L
    
    val N = readInputLine().toInt()
    
    val AList = readInputLine().split(" ").map { it.toInt() }
    
    val hatNum = IntArray(3)
    
    var ans = 1L
    
    for (A in AList) {
        var cnt = 0L
        for (h in hatNum) {
            if (A == h) {
                cnt++
            }
        }
        
        ans *= cnt
        ans %= mod
        
        for (i in 0..2) {
            if (A == hatNum[i]) {
                hatNum[i]++
                break
            }
        }
    }
    
    println(ans)
}

fun readInputLine(): String {
    return readLine()!!
}
