#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e4 + 100;
const int mod = (int)1e9 + 7;
int n, m, l[maxn], r[maxn], mx, a[maxn];
long long ans;
vector<int> v[maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (auto i = (1); i <= (n); ++i) scanf("%d", &a[i]);
  int f, h;
  for (auto i = (1); i <= (m); ++i) scanf("%d%d", &f, &h), v[f].push_back(h);
  for (auto i = (1); i <= (n); ++i) sort(v[i].begin(), v[i].end());
  for (auto i = (0); i <= (n); ++i) {
    for (auto j = (0); j <= (n); ++j) l[j] = r[j] = 0;
    for (auto j = (1); j <= (i); ++j) l[a[j]]++;
    for (auto j = (i + 1); j <= (n); ++j) r[a[j]]++;
    int ok = i == 0, num = i != 0;
    long long s = 1;
    for (auto x : v[a[i]])
      if (x == l[a[i]]) {
        ok = 1;
        break;
      }
    if (!ok) continue;
    for (auto j = (1); j <= (n); ++j) {
      int x = 0, y = 0, sz = (int)v[j].size();
      while (x < sz && v[j][x] <= l[j]) x++;
      while (y < sz && v[j][y] <= r[j]) y++;
      if (j == a[i]) x = 0, y -= (r[j] >= l[j]);
      if (x + y == 0) continue;
      if (x > y) swap(x, y);
      if (!x)
        num++, s = s * y % mod;
      else if (y == 1)
        num++, s = s * 2 % mod;
      else
        num += 2, s = s * x * (y - 1) % mod;
    }
    if (num > mx) mx = num, ans = 0;
    if (num == mx) ans = (ans + s) % mod;
  }
  printf("%d %lld\n", mx, ans);
}
