#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005, mod = 1e9 + 7;
int n, m, a[maxn], psa[maxn][maxn];
pair<int, long long> res;
vector<int> cows[maxn];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf(" %d", &a[i]);
    for (int k = 1; k <= n; k++) psa[i][k] = psa[i - 1][k];
    psa[i][a[i]]++;
  }
  for (int i = 1; i <= m; i++) {
    int f, h;
    scanf(" %d %d", &f, &h);
    cows[f].push_back(h);
  }
  for (int i = 1; i <= n; i++) sort(cows[i].begin(), cows[i].end());
  int cnt1 = 0, ww = 1;
  for (int i = 1; i <= n; i++) {
    int ava = upper_bound(cows[i].begin(), cows[i].end(), psa[n][i]) -
              cows[i].begin();
    if (ava) {
      cnt1++;
      ww = (ww * ava) % mod;
    }
  }
  res = make_pair(cnt1, ww);
  for (int i = 1; i <= n; i++) {
    auto it = lower_bound(cows[a[i]].begin(), cows[a[i]].end(), psa[i][a[i]]);
    if (it != cows[a[i]].end() && *it == psa[i][a[i]]) {
      long long ways = 1;
      int cnt = 1;
      int ava = upper_bound(cows[a[i]].begin(), cows[a[i]].end(),
                            psa[n][a[i]] - psa[i][a[i]]) -
                cows[a[i]].begin();
      if (ava && cows[a[i]][ava - 1] >= psa[i][a[i]]) ava--;
      if (ava) {
        cnt++;
        ways = (ways * ava) % mod;
      }
      for (int c = 1; c <= n; c++) {
        if (c == a[i]) continue;
        int am1 = upper_bound(cows[c].begin(), cows[c].end(), psa[i][c]) -
                  cows[c].begin();
        int am2 =
            upper_bound(cows[c].begin(), cows[c].end(), psa[n][c] - psa[i][c]) -
            cows[c].begin();
        if (am1 && am2 && max(am1, am2) >= 2)
          cnt += 2;
        else if (am1 || am2)
          cnt++;
        if (am1 == 1 && am2 == 1)
          ways = (ways * 2) % mod;
        else if (am1 && am2 && max(am1, am2) >= 2) {
          ways = (ways * min(am1, am2)) % mod;
          ways = (ways * (max(am1, am2) - 1)) % mod;
        } else if (am1 == 0 && am2 || am2 == 0 && am1) {
          ways = (ways * max(am1, am2)) % mod;
        }
      }
      if (cnt > res.first)
        res = make_pair(cnt, ways);
      else if (cnt == res.first)
        res.second = (res.second + ways) % mod;
    }
  }
  printf("%d %lld\n", res.first, res.second);
}
