import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

enum m = 1_000_000_007;

struct C {
  int a, b, c, v;
}

int add (int x, int y) {
  x += y;
  if (x >= m) x -= m;
  return x;
}

void main() {
  immutable hb = 1L << 32;
  int n;
  readf (" %d", &n);
  auto a = new int[n];
  foreach (i; 0 .. n) {
    readf (" %d", &a[i]);
  }
  int[long] q;
  q[0] = 1;
  foreach (i; 0 .. n) {
    immutable t = a[i];
    int[long] w;
    foreach (x, val; q) {
      int a = (x & 0x7fffffff).to!int; 
      int b = (x >> 32).to!int;
      int c = i - (a + b);
      if (t == a) {
        w[x+1] = add (w.get (x + 1, 0), val);
      }
      if (t == b) {
        w[x+hb] = add (w.get (x + hb, 0), val);
      }
      if (t == c) {
        w[x] = add (w.get (x, 0), val);
      }
    }
    q = w;
  }
  int res = 0;
  foreach (x, val; q) {
    res = add (res, val);
  }
  writeln (res);
}

