#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
const int N = 5005;
const int mod = 1e9 + 7;
int n, m;
int s[N];
int f[N], h[N];
vector<int> hf[N];
int val[N];
int psum[N][N];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", s + i);
  }
  s[n] = n + 1;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", f + i, h + i);
    hf[f[i]].push_back(h[i]);
  }
  for (int i = 1; i <= n; i++) {
    sort(hf[i].begin(), hf[i].end());
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    psum[i][0] = s[0] == i;
    for (int j = 1; j <= n; j++) {
      psum[i][j] = psum[i][j - 1] + (s[j] == i);
    }
  }
  for (int mid = 0; mid <= n; mid++) {
    int tt = 0;
    for (int i = 1; i <= n; i++) {
      int lef = mid ? psum[i][mid - 1] : 0;
      int rig = psum[i][n] - (mid ? psum[i][mid - 1] : 0);
      if (hf[i].size() >= 2 && min(lef, rig) >= hf[i][0] &&
          max(lef, rig) >= hf[i][1]) {
        tt += 2;
      } else if (hf[i].size() && max(lef, rig) >= hf[i][0]) {
        tt += 1;
      }
    }
    val[mid] = tt;
    ans = max(ans, tt);
  }
  ll cnt = 0;
  for (int mid = 0; mid <= n; mid++) {
    if (val[mid] != ans) continue;
    ll at = 1;
    for (int i = 1; i <= n; i++) {
      if (hf[i].size() && (mid ? s[mid - 1] != i : true)) {
        int lef = mid ? psum[i][mid - 1] : 0;
        int rig = psum[i][n] - (mid ? psum[i][mid - 1] : 0);
        auto it = upper_bound(hf[i].begin(), hf[i].end(), lef);
        int clef = distance(hf[i].begin(), it);
        it = upper_bound(hf[i].begin(), hf[i].end(), rig);
        int crig = distance(hf[i].begin(), it);
        if (clef == 0 && crig == 0) {
          continue;
        }
        if (clef == 1 && crig == 1) {
          at *= 2;
          at %= mod;
        } else if (clef == 0) {
          at = (at * crig) % mod;
        } else if (crig == 0) {
          at = (at * clef) % mod;
        } else {
          if (clef < crig) {
            swap(clef, crig);
          }
          ll aux = ((crig * (crig - 1)) % mod);
          if (crig != clef) {
            int d = clef - crig;
            aux += crig * d;
          }
          at = (at * aux) % mod;
          continue;
          ll a = crig;
          ll b = clef;
          if (a * b - a > 0) {
            at *= (a * b - a) % mod;
            at %= mod;
          }
        }
      }
    }
    if (mid == 0) {
      cnt = (cnt + at) % mod;
    } else {
      for (int i = s[mid - 1]; i <= s[mid - 1]; i++) {
        if (hf[i].size()) {
          int lef = mid ? psum[i][mid - 1] : 0;
          int rig = psum[i][n] - (mid ? psum[i][mid - 1] : 0);
          auto it = upper_bound(hf[i].begin(), hf[i].end(), lef);
          int clef = distance(hf[i].begin(), it);
          it = upper_bound(hf[i].begin(), hf[i].end(), rig);
          int crig = distance(hf[i].begin(), it);
          it = lower_bound(hf[i].begin(), hf[i].end(), lef);
          if (it != hf[i].end() && *it == lef) {
            if (crig > 1) {
              cnt = (cnt + ((at * (crig - (crig >= clef))) % mod)) % mod;
            } else {
              cnt = (cnt + at) % mod;
            }
          }
        }
      }
    }
  }
  printf("%d %lld\n", ans, ans == 0 ? 1 : cnt);
  return 0;
}
