#include <bits/stdc++.h>
using namespace std;
long long gesas;
long long inf = 100000000000000000LL;
long long modulo = pow(10, 9) + 7;
double eps = 1e-6;
const long long int mod = 998244353;
long long n;
const int kons = 500001;
vector<long long> valuesforcows[5005];
long long min(long long a, long long b) { return a < b ? a : b; }
const int N = 3e5 + 7;
int main() {
  long long k, z, d, g, m, t, b, c, e, x, y, x1, y1, x2, y2;
  string he = "YES", yox = "NO";
  int n, a;
  cin >> n >> m;
  map<pair<int, int>, int> cowmap;
  int f[m], h[m], s[n + 1];
  s[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  for (int k = 0; k < m; k++) {
    cin >> f[k] >> h[k];
    valuesforcows[f[k]].push_back(h[k]);
    cowmap[{f[k], h[k]}]++;
  }
  cowmap[{0, 1}]++;
  for (int l = 0; l <= n; ++l)
    sort(valuesforcows[l].begin(), valuesforcows[l].end());
  long long sayansmax = 0, numberofways = 1;
  for (int i = 0; i < n + 1; i++) {
    vector<int> lefcounter(n + 1, 0), rigcounter(n + 1, 0);
    for (int j = 0; j <= i; j++) lefcounter[s[j]]++;
    for (int j = i + 1; j < n + 1; j++) rigcounter[s[j]]++;
    if (!cowmap.count({s[i], lefcounter[s[i]]})) continue;
    long long umberofways = 0, cavab = 0,
              upbon =
                  (upper_bound(valuesforcows[s[i]].begin(),
                               valuesforcows[s[i]].end(), rigcounter[s[i]]) -
                   valuesforcows[s[i]].begin());
    if (lefcounter[s[i]] <= rigcounter[s[i]]) upbon--;
    if (upbon)
      cavab += 2, (umberofways += upbon) %= modulo;
    else
      cavab++, (umberofways = umberofways + 1) %= modulo;
    if (!i) cavab--;
    for (int ind = 1; ind <= n; ind++) {
      long long zupbon =
                    (upper_bound(valuesforcows[ind].begin(),
                                 valuesforcows[ind].end(), rigcounter[ind]) -
                     valuesforcows[ind].begin()),
                juppon =
                    (upper_bound(valuesforcows[ind].begin(),
                                 valuesforcows[ind].end(), lefcounter[ind]) -
                     valuesforcows[ind].begin());
      if (!zupbon && !juppon) continue;
      if (ind == s[i]) continue;
      long long kuppon = zupbon;
      zupbon = zupbon * juppon - (min(zupbon, juppon));
      if (zupbon) {
        (umberofways *= zupbon) %= modulo;
        cavab += 2;
        continue;
      }
      (kuppon += juppon) %= modulo;
      (umberofways *= kuppon) %= modulo;
      cavab++;
    }
    if (cavab > sayansmax) {
      sayansmax = cavab;
      numberofways = (umberofways) % modulo;
    } else if (cavab == sayansmax) {
      (numberofways += umberofways) %= modulo;
    }
  }
  if (!sayansmax) numberofways = 1;
  cout << sayansmax << ' ' << numberofways << "\n";
}
