#include <bits/stdc++.h>
int inp() {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  int sum = 0;
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum;
}
int s[5010], f[5010], h[5010], d[5010], n, m;
int sum[5010][5010], l[5010], r[5010], cnt1[5010], cnt2[5010], cc;
long long solve(int lb, int rb) {
  memset(cnt1, 0, sizeof(cnt1));
  memset(cnt2, 0, sizeof(cnt2));
  for (int j = 1; j <= m; j++) {
    if (l[j] <= lb && r[j] >= rb)
      cnt2[f[j]]++;
    else if (l[j] <= lb || r[j] >= rb)
      cnt1[f[j]]++;
  }
  cc = 0;
  long long cnt = 1;
  for (int j = 1; j <= n; j++) {
    if (cnt2[j] && (cnt1[j] + cnt2[j] > 1)) {
      cnt *= (cnt2[j] * (cnt2[j] - 1) + cnt2[j] * cnt1[j]);
      cnt %= 1000000007;
      cc += 2;
    } else if (cnt2[j] + cnt1[j]) {
      cnt *= ((cnt2[j] << 1) + cnt1[j]);
      cnt %= 1000000007;
      cc++;
    }
  }
  return cnt;
}
int main() {
  n = inp();
  m = inp();
  for (int i = 1; i <= n; i++) s[i] = inp();
  for (int i = 1; i <= m; i++) {
    f[i] = inp();
    h[i] = inp();
  }
  for (int i = 1; i <= m; i++) {
    l[i] = 1;
    int sum = 0;
    while (sum < h[i] && l[i] <= n + 1) {
      sum += (f[i] == s[l[i]]);
      l[i]++;
    }
    l[i]--;
    r[i] = n;
    sum = 0;
    while (sum < h[i] && r[i] >= 0) {
      sum += (f[i] == s[r[i]]);
      r[i]--;
    }
    r[i]++;
  }
  int ans = 0;
  long long ans2 = 0;
  for (int i = 0; i <= n; i++) {
    memset(cnt1, 0, sizeof(cnt1));
    memset(cnt2, 0, sizeof(cnt2));
    for (int j = 1; j <= m; j++) {
      if (l[j] <= i && r[j] > i)
        cnt2[f[j]]++;
      else if (l[j] <= i || r[j] > i)
        cnt1[f[j]]++;
    }
    long long cnt = solve(i, i + 1);
    int sum = cc;
    solve(i - 1, i + 1);
    if (sum == cc && i > 0) {
      cnt -= solve(i - 1, i + 1);
      cnt += 1000000007;
      cnt %= 1000000007;
    }
    if (sum > ans) {
      ans = sum;
      ans2 = cnt;
    } else if (sum == ans) {
      ans2 += cnt;
      ans2 %= 1000000007;
    }
  }
  printf("%d %lld\n", ans, ans2);
}
