#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const int N = 5010;
long long int b[N][N];
long long int p2(long long int n) { return n * (n - 1) % mod; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int T = clock();
  long long int n, m;
  cin >> n >> m;
  vector<long long int> a(n);
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (long long int i = 0; i < m; i++) {
    long long int x, y;
    cin >> x >> y;
    x--;
    b[x][y]++;
  }
  long long int ans = 0;
  vector<long long int> f(n, 0);
  vector<long long int> mx(n), sum(n);
  for (long long int i = 0; i < n; i++) {
    f[a[i]]++;
    if (b[a[i]][f[a[i]]] == 0) continue;
    long long int tot = 1, c = b[a[i]][f[a[i]]];
    b[a[i]][f[a[i]]]--;
    vector<long long int> p(n), s(n);
    for (long long int j = 0; j < i; j++) p[a[j]]++;
    for (long long int j = n - 1; j > i; j--) s[a[j]]++;
    for (long long int j = 0; j < n; j++) {
      if (j == a[i]) continue;
      long long int l = min(p[j], s[j]), r = max(p[j], s[j]);
      long long int c1 = 0, c2 = 0;
      for (long long int k = 1; k <= l; k++) c1 = (c1 + b[j][k]) % mod;
      for (long long int k = l + 1; k <= r; k++) c2 = (c2 + b[j][k]) % mod;
      if (c1 == 0 && c2 == 0) continue;
      long long int add;
      if (c1 == 0) {
        add = c2;
        tot++;
      } else {
        if (c1 == 1 && c2 == 0)
          add = 2, tot++;
        else
          add = (p2(c1) + c2 * c1 % mod) % mod, tot += 2;
      }
      if (add > 0) c = c * add % mod;
    }
    long long int c1 = 0;
    for (long long int k = 1; k <= s[a[i]]; k++) c1 = (c1 + b[a[i]][k]) % mod;
    if (c1 > 0) tot++, c = c * c1 % mod;
    mx[i] = tot;
    ans = max(ans, tot);
    sum[i] = c;
    b[a[i]][f[a[i]]]++;
  }
  long long int fin = 0;
  for (long long int i = 0; i < n; i++) {
    if (mx[i] == ans) fin = (fin + sum[i]) % mod;
  }
  {
    long long int tot = 0, c = 1;
    vector<long long int> s(n);
    for (long long int j = n - 1; j >= 0; j--) s[a[j]]++;
    for (long long int j = 0; j < n; j++) {
      long long int c1 = 0;
      for (long long int k = 1; k <= s[j]; k++) c1 = (c1 + b[j][k]) % mod;
      if (c1 > 0) tot++, c = c * c1 % mod;
    }
    if (tot == ans) fin = (fin + c) % mod;
  }
  cout << ans << " " << fin;
  cerr << "\n\nTIME: " << (double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
  T = clock();
  return 0;
}
