#include <bits/stdc++.h>
using namespace std;
const long long md = 1e9 + 7;
long long n, m, s[5050], f[5050], h[5050], l[5050], r[5050];
vector<long long> v[5050];
pair<long long, long long> sl(long long x) {
  if (l[x] == n + 1) {
    return make_pair(0, 0);
  }
  long long mx, cnt = 1;
  if (x == 0) {
    mx = 0;
  } else {
    mx = 1;
  }
  long long nw = l[x];
  for (long long i = 1; i <= n; i++) {
    if (v[i].size()) {
      if (i == f[x]) {
        long long cn = 0;
        for (long long j = 0; j < v[i].size(); j++) {
          long long xx = v[i][j];
          if (xx != x && r[xx] > nw) {
            cn++;
          }
        }
        if (cn) {
          mx++;
          cnt = cnt * cn % md;
          cnt %= md;
        }
      } else {
        long long ct1 = 0, ct2 = 0;
        for (long long j = 0; j < v[i].size(); j++) {
          long long xx = v[i][j];
          if (l[xx] < nw) {
            ct1++;
          }
          if (r[xx] > nw) {
            ct2++;
          }
        }
        if (ct1 == 0 && ct2 == 0) {
          continue;
        }
        if (ct1 != 0 && ct2 != 0 && ct1 + ct2 >= 3) {
          mx += 2;
          cnt = cnt * min(ct1, ct2) % md * (max(ct1, ct2) - 1) % md;
          cnt %= md;
        } else {
          mx++;
          cnt = cnt * (ct1 + ct2) % md;
          cnt %= md;
        }
      }
    }
  }
  return make_pair(mx, cnt);
}
signed main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (long long i = 1; i <= m; i++) {
    cin >> f[i] >> h[i];
    v[f[i]].push_back(i);
    long long nw = h[i];
    for (l[i] = 1; l[i] <= n; l[i]++) {
      if (s[l[i]] == f[i]) {
        nw--;
      }
      if (nw == 0) {
        break;
      }
    }
    nw = h[i];
    for (r[i] = n; r[i] >= 1; r[i]--) {
      if (s[r[i]] == f[i]) {
        nw--;
      }
      if (nw == 0) {
        break;
      }
    }
  }
  bool ok = true;
  for (long long i = 1; i <= m; i++) {
    if (r[i]) {
      ok = false;
      break;
    }
  }
  if (ok == true) {
    cout << "0 1" << endl;
    return 0;
  }
  long long mx = 0, cnt = 1;
  for (long long i = 0; i <= m; i++) {
    pair<long long, long long> p = sl(i);
    if (p.first == mx) {
      cnt += p.second;
      cnt %= md;
    }
    if (p.first > mx) {
      mx = p.first;
      cnt = p.second;
    }
  }
  cout << mx << ' ' << cnt << endl;
  return 0;
}
