import java.util.*;

public class Antti {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = input.nextInt();
        long M = 1000000007;
        long c1 = 0;
        long c2 = 0;
        long c3 = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] == 1) c1++;
            if (a[i] == 2) c2 = 2*c2+c1;
            if (a[i] == 3) c3 += c2;
            c1 %= M;
            c2 %= M;
            c3 %= M;
        }
        System.out.println(c3);
    }
}
