#include <bits/stdc++.h>
using namespace std;
namespace cowtreats {
int n, m;
int s[5010];
int f[5010];
int h[5010];
int tot[5010];
bool pos[5010];
long long ways[5010];
vector<int> huns[5010];
const long long mod = 1000000007;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    tot[s[i]]++;
  }
  for (int i = 0; i < m; i++) {
    cin >> f[i] >> h[i];
    huns[f[i]].push_back(h[i]);
  }
  for (int j = 1; j <= n; j++) {
    sort(huns[j].begin(), huns[j].end());
  }
  for (int i = 0; i <= n; i++) {
    int num = 0;
    long long numways = 1;
    int sofar[5010];
    memset(sofar, 0, sizeof sofar);
    bool lefttight = false;
    int lefttype;
    if (i == 0) {
      lefttype = -1;
      lefttight = true;
    } else {
      lefttype = s[i - 1];
    }
    for (int j = 0; j < i; j++) {
      sofar[s[j]]++;
    }
    for (int j = 1; j <= n; j++) {
      if (j == lefttype) {
        if (huns[j].empty()) {
          break;
        }
        int x = 0;
        while (x < huns[j].size() && huns[j][x] != sofar[j]) {
          x++;
        }
        if (x == huns[j].size()) {
          break;
        }
        int y = 0;
        while (y < huns[j].size() && huns[j][y] <= tot[j] - sofar[j]) {
          y++;
        }
        lefttight = true;
        if (y == 0) {
          num++;
          continue;
        }
        if (y == 1 && x == 0) {
          num++;
          continue;
        }
        if (y < x + 1) {
          num += 2;
          numways *= y;
        } else {
          num += 2;
          numways *= (y - 1);
        }
        continue;
      }
      if (huns[j].empty()) {
        continue;
      }
      int x = 0;
      while (x < huns[j].size() && huns[j][x] <= sofar[j]) {
        x++;
      }
      int y = 0;
      while (y < huns[j].size() && huns[j][y] <= tot[j] - sofar[j]) {
        y++;
      }
      if (x == 0 && y == 0) {
        continue;
      }
      if (x * y == 0 || x * y == 1) {
        num++;
        numways *= (long long)(x + y);
        numways %= mod;
        continue;
      }
      num += 2;
      numways *= (long long)(x * y - min(x, y));
      numways %= mod;
    }
    if (lefttight) {
      pos[num] = true;
      ways[num] += numways;
      ways[num] %= mod;
    }
  }
  for (int i = n; i >= 0; i--) {
    if (pos[i]) {
      cout << i << " " << ways[i] << endl;
      break;
    }
  }
  return 0;
}
}  // namespace cowtreats
int main() {
  cowtreats::main();
  return 0;
}
