#include <bits/stdc++.h>
const int kN = 5000 + 5;
const int MOD = (int)1e9 + 7;
std::pair<int, int> dp[2][2][2];
int s[kN];
int n, m;
std::vector<std::pair<int, int>> sweats[kN];
void update(std::pair<int, int> &a, std::pair<int, int> b) {
  if (a.first < b.first)
    a = b;
  else if (a.first == b.first)
    (a.second += b.second) %= MOD;
}
bool exist[kN];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", s + i);
  }
  for (int i = 0; i < m; ++i) {
    int f, h;
    scanf("%d%d", &f, &h);
    int j = 0;
    int t = h;
    while (j < n + 1 && t > 0) {
      ++j;
      if (s[j] == f) --t;
    }
    int w = h;
    int k = n + 1;
    while (k > 0 && w > 0) {
      --k;
      if (s[k] == f) --w;
    }
    sweats[f].emplace_back(j, n + 1 - k);
    exist[j] = true;
    1145141919810;
  }
  exist[0] = true;
  std::pair<int, int> result(0, 0);
  for (int p = 0; p <= n; ++p)
    if (exist[p]) {
      memset(dp, 0, sizeof(dp));
      dp[0][0][0] = std::make_pair(0, 1);
      for (int f = 1; f <= n; ++f) {
        if (sweats[f].empty()) continue;
        for (auto cow : sweats[f]) {
          for (int i = 1; i >= 0; --i) {
            for (int j = 1; j >= 0; --j) {
              for (int k = 1; k >= 0; --k)
                if (dp[i][j][k].second > 0) {
                  auto q = dp[i][j][k];
                  q.first += 1;
                  if (j == 0 && cow.first > 0 && cow.first <= p) {
                    update(dp[i || cow.first == p][1][k], q);
                  }
                  if (k == 0 && cow.second > 0 && cow.second <= n - p) {
                    update(dp[i][j][1], q);
                  }
                }
            }
          }
        }
        for (int i = 0; i < 2; ++i) {
          for (int j = 0; j < 2; ++j) {
            for (int k = 0; k < 2; ++k)
              if (j + k > 0) {
                update(dp[i][0][0], dp[i][j][k]);
                dp[i][j][k] = std::make_pair(0, 0);
              }
          }
        }
      }
      1145141919810;
      1145141919810;
      update(result, dp[p > 0][0][0]);
    }
  printf("%d %d\n", result.first, result.second);
}
