#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5005;
const int mod = 1e9 + 7;
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int mul(int a, int b) { return int(1LL * a * b % mod); }
using data = pair<int, int>;
data comb(data a, data b) {
  if (a.first > b.first) return a;
  if (a.first < b.first) return b;
  a.second = add(a.second, b.second);
  return a;
}
int n, m;
int s[N], fa[N], h[N];
int p1[N], p2[N];
int ids[N];
int vtmp[N];
void comp_p1_p2() {
  for (int i = 1; i <= m; i++) {
    int tmp = h[i];
    for (int j = 1; j <= n; j++) {
      if (fa[i] == s[j]) tmp--;
      if (tmp == 0) {
        p1[i] = j;
        break;
      }
    }
    if (tmp) {
      p1[i] = n + 2, p2[i] = -1;
      continue;
    }
    tmp = h[i];
    for (int j = n; j >= 1; j--) {
      if (fa[i] == s[j]) tmp--;
      if (tmp == 0) {
        p2[i] = j;
        break;
      }
    }
  }
}
int is_p1[N];
data dp[N][3];
int mark[N][3], pass;
int lim, flim;
int nx[N];
data f(int i, int last) {
  if (i == m + 1) return data{0, 1};
  auto &ans = dp[i][last];
  if (mark[i][last] == pass) return ans;
  mark[i][last] = pass;
  ans = data{0, 0};
  { ans = f(i + 1, fa[i + 1] == fa[i] ? last : 0); }
  {
    if (fa[i] == flim) {
      if (!last and p2[i] > lim and p1[i] != lim) {
        auto tmp = f(nx[i], 0);
        tmp.first++;
        ans = comb(ans, tmp);
      }
    } else {
      int j = last ? nx[i] : i + 1;
      if (last != 1 and p1[i] <= lim) {
        auto tmp = f(j, fa[j] == fa[i] ? 1 : 0);
        tmp.first++;
        ans = comb(ans, tmp);
      }
      if (last != 2 and p2[i] > lim) {
        auto tmp = f(j, fa[j] == fa[i] ? 2 : 0);
        tmp.first++;
        ans = comb(ans, tmp);
      }
    }
  }
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", s + i);
  for (int i = 1; i <= m; i++) scanf("%d %d", fa + i, h + i);
  for (int i = 1; i <= m; i++) ids[i] = i;
  sort(ids + 1, ids + m + 1,
       [](int i, int j) { return tie(fa[i], h[i]) < tie(fa[j], h[j]); });
  for (int i = 1; i <= m; i++) vtmp[i] = fa[ids[i]];
  for (int i = 1; i <= m; i++) fa[i] = vtmp[i];
  for (int i = 1; i <= m; i++) vtmp[i] = h[ids[i]];
  for (int i = 1; i <= m; i++) h[i] = vtmp[i];
  comp_p1_p2();
  for (int i = 1; i <= m; i++) {
    int j = i;
    while (j + 1 <= m and fa[j + 1] == fa[i]) j++;
    while (i <= j) nx[i] = j + 1, i++;
    i--;
  }
  for (int i = 1; i <= m; i++) {
    ;
  }
  for (int i = 1; i <= m; i++) is_p1[p1[i]] = i;
  pair<int, int> ans{0, 0};
  for (int i = 1; i <= m; i++)
    if (p1[i] <= n) {
      pass++;
      lim = p1[i];
      flim = fa[i];
      auto tmp = f(1, 0);
      tmp.first++;
      ans = comb(ans, tmp);
      ;
    }
  lim = 0, flim = -1, pass++;
  ans = comb(ans, f(1, 0));
  printf("%d %d\n", ans.first, ans.second);
}
