#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct cw {
  int sl, sr;
} tmp;
int n, m, u, v, s[5010], wy[2], tg, tt, numc, tag, ans1, ans2;
vector<cw> c[5010];
bool cmp(cw x, cw y) { return x.sl < y.sl; }
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", &u, &v);
    tmp.sl = 1, tmp.sr = n, tt = 0;
    while (tmp.sl <= n && (tt += (s[tmp.sl] == u)) < v) ++tmp.sl;
    if (tmp.sl > n) continue;
    tt = 0;
    while (tmp.sr >= 1 && (tt += (s[tmp.sr] == u)) < v) --tmp.sr;
    if (tmp.sr < 1) continue;
    c[u].push_back(tmp);
  }
  for (int i = 1; i <= n; ++i)
    if (c[i].size()) sort(c[i].begin(), c[i].end(), cmp);
  for (int i = 1; i <= n; ++i)
    if (c[s[i]].size() && c[s[i]][0].sl <= i) {
      tt = 0, wy[tg] = 1;
      for (int j = 1; j <= n && wy[tg]; ++j)
        if (c[j].size()) {
          tg ^= 1, tag = numc = wy[tg] = 0;
          if (j != s[i]) {
            for (int k = 0; k < c[j].size(); ++k) numc += (c[j][k].sr > i);
            if (!numc && c[j][0].sl > i) {
              tg ^= 1;
              continue;
            }
            ++tt;
            for (int k = 0; k < c[j].size() && c[j][k].sl < i; ++k) {
              if (!tag && numc - (c[j][k].sr > i)) tag = 1, wy[tg] = 0;
              wy[tg] = (wy[tg] +
                        1ll * wy[tg ^ 1] * max(1, numc - (c[j][k].sr > i))) %
                       mod;
            }
            if (!tag) wy[tg] = (wy[tg] + 1ll * wy[tg ^ 1] * numc) % mod;
            tt += tag;
          } else {
            ++tt;
            for (int k = 0; k < c[j].size(); ++k)
              numc += (c[j][k].sr > i && c[j][k].sl != i),
                  tag |= (c[j][k].sl == i);
            if (!tag) goto End;
            wy[tg] = 1ll * wy[tg ^ 1] * max(1, numc) % mod, tt += !!numc;
          }
        }
      if (!wy[tg] || tt < ans1) continue;
      if (tt > ans1)
        ans1 = tt, ans2 = wy[tg];
      else if (tt == ans1)
        (ans2 += wy[tg]) >= mod ? ans2 -= mod : 0;
    End:;
    }
  tt = 0, wy[tg] = 1;
  for (int i = 1; i <= n; ++i)
    if (c[i].size()) ++tt, wy[tg] = 1ll * wy[tg] * c[i].size() % mod;
  if (tt > ans1)
    ans1 = tt, ans2 = wy[tg];
  else if (tt == ans1)
    (ans2 += wy[tg]) >= mod ? ans2 -= mod : 0;
  printf("%d %d", ans1, ans2);
  return 0;
}
