#include <bits/stdc++.h>
using namespace std;
using INT = long long;
using pii = pair<int, int>;
int s[5100];
int f[5100], h[5100];
int n, m;
int L[5100], R[5100];
vector<int> res[5100];
pii solve(int u) {
  if (L[u] == n + 1) return {0, 0};
  int ma = 1, cnt = 1;
  if (!u) ma = 0;
  int md = L[u];
  for (int i = 1; i < 5100; i++)
    if (res[i].size()) {
      if (i == f[u]) {
        int mul = 0;
        for (auto x : res[i])
          if (x != u and R[x] > md) {
            mul++;
          }
        if (mul) ma++, cnt = (INT)cnt * mul % 1000000007;
        continue;
      }
      int lcnt = 0, rcnt = 0;
      for (int x : res[i]) {
        if (L[x] <= md) lcnt++;
        if (R[x] > md) rcnt++;
      }
      if (lcnt + rcnt == 0) continue;
      if (lcnt and rcnt and lcnt + rcnt >= 3) {
        ma += 2;
        cnt = (INT)cnt * min(lcnt, rcnt) % 1000000007 * (max(lcnt, rcnt) - 1) %
              1000000007;
      } else {
        ma++;
        cnt = (INT)cnt * (lcnt + rcnt) % 1000000007;
      }
    }
  return {ma, cnt};
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) scanf("%d", s + i);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", f + i, h + i);
    res[f[i]].push_back(i);
    int cnt = h[i];
    for (L[i] = 1; L[i] <= n; L[i]++) {
      if (s[L[i]] == f[i]) cnt--;
      if (cnt == 0) break;
    }
    cnt = h[i];
    for (R[i] = n; R[i]; R[i]--) {
      if (s[R[i]] == f[i]) cnt--;
      if (cnt == 0) break;
    }
  }
  int vst = 0;
  for (int i = 1; i <= m; i++) {
    if (R[i]) vst = 1;
  }
  if (vst == 0) {
    return puts("0 1"), 0;
  }
  int ma = 0, cnt = 1;
  for (int i = 0; i <= m; i++) {
    auto p = solve(i);
    if (p.first == ma) {
      cnt += p.second;
      cnt %= 1000000007;
    }
    if (p.first > ma) ma = p.first, cnt = p.second;
  }
  cout << ma << ' ' << cnt << endl;
  return 0;
}
