fun main(args: Array<String>) {
    sumitrust2019e()
}

private const val MOD = (1000000007).toLong()

private enum class Color { R, G, B }

fun sumitrust2019e() {
    val n = readLine()!!.toInt()
    val aList = readLine()!!.split(' ').map { it.toLong() }.toLongArray()

    var answer = 1L
    val left = mutableMapOf<Color, Long>(Color.R to 0, Color.G to 0, Color.B to 0)
    aList.forEachIndexed { index, i ->
        val candidate = left.filterValues { it == i }
        answer = answer * candidate.size % MOD

        var changed = false
        candidate.forEach {
            val (t, u) = it
            if (!changed) left[t] = left[t]!! + 1
            changed = true
        }
    }

    println(answer)
}
