#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < (n); i++) cin >> a[i];
  for (int i = 0; i < (m - 2); i++) {
    if (a[i + 1] == a[i] + 1 && a[i + 2] == a[i + 1] + 1) {
      cout << 0 << endl;
      return 0;
    }
  }
  int mul = 1;
  for (int i = 1; i < m; i++) {
    int x = 3;
    if (a[i - 1] != i - 1) continue;
    if (i == a[i]) x--;
    if (i + 1 == a[i + 1]) x--;
    mul *= x;
    mul %= 1000000007;
  }
  cout << mul << endl;
}
