#include <bits/stdc++.h>
using namespace std;
void read(int &a) {
  a = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    a = (a << 1) + (a << 3) + (c ^ 48);
    c = getchar();
  }
}
const int Maxn = 5000;
const int Mod = 1000000007;
int n, m;
int s[Maxn + 5];
struct Cow {
  int f, h;
} a[Maxn + 5];
int sum[Maxn + 5][Maxn + 5];
vector<int> v[Maxn + 5];
struct Node {
  int maxn;
  int sum;
  Node(int _maxn = -1, int _sum = 0) {
    maxn = _maxn;
    sum = _sum;
  }
  friend Node operator+(Node p, Node q) {
    if (p.maxn == q.maxn) {
      return Node(p.maxn, (p.sum + q.sum) % Mod);
    }
    if (p.maxn > q.maxn) {
      return p;
    }
    return q;
  }
};
Node work(int x) {
  bool flag = 0;
  if (x == 0) {
    flag = 1;
  }
  for (int i = 1; i <= m; i++) {
    if (a[i].f == s[x] && a[i].h == sum[x][a[i].f]) {
      flag = 1;
      break;
    }
  }
  if (!flag) {
    return Node(-1, 0);
  }
  Node ans;
  ans.maxn = 0;
  ans.sum = 1;
  for (int i = 1; i <= n; i++) {
    int cnt_1 = 0, cnt_2 = 0, cnt_3 = 0;
    for (int j = 0; j < (int)v[i].size(); j++) {
      int pos = v[i][j];
      if (a[pos].f == s[x] && a[pos].h == sum[x][a[pos].f]) {
        cnt_1++;
        continue;
      }
      bool flag_1 = 0, flag_2 = 0;
      if (a[pos].f != s[x] && a[pos].h <= sum[x][a[pos].f]) {
        flag_1 = 1;
      }
      if (a[pos].h <= sum[n][a[pos].f] - sum[x][a[pos].f]) {
        flag_2 = 1;
      }
      if (flag_1 && (!flag_2)) {
        cnt_1++;
      }
      if ((!flag_1) && flag_2) {
        cnt_2++;
      }
      if (flag_1 && flag_2) {
        cnt_3++;
      }
    }
    if ((cnt_1 > 0 && cnt_2 > 0) || (cnt_1 > 0 && cnt_3 > 0) ||
        (cnt_2 > 0 && cnt_3 > 0) || (cnt_3 > 1)) {
      ans.maxn += 2;
      ans.sum = 1ll * ans.sum *
                ((cnt_1 * cnt_2 + cnt_1 * cnt_3 + cnt_2 * cnt_3 +
                  cnt_3 * (cnt_3 - 1)) %
                 Mod) %
                Mod;
    } else if (cnt_1 > 0 || cnt_2 > 0 || cnt_3 > 0) {
      ans.maxn++;
      ans.sum = 1ll * ans.sum * ((cnt_1 + cnt_2 + (cnt_3 << 1)) % Mod) % Mod;
    }
  }
  return ans;
}
int main() {
  read(n), read(m);
  for (int i = 1; i <= n; i++) {
    read(s[i]);
    for (int j = 1; j <= n; j++) {
      sum[i][j] = sum[i - 1][j];
    }
    sum[i][s[i]]++;
  }
  for (int i = 1; i <= m; i++) {
    read(a[i].f), read(a[i].h);
    v[a[i].f].push_back(i);
  }
  Node ans;
  for (int i = 0; i <= n; i++) {
    ans = ans + work(i);
  }
  printf("%d %d\n", ans.maxn, ans.maxn == 0 ? 1 : ans.sum);
  return 0;
}
