#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
long long POW(long long a, long long b, long long MMM = MOD) {
  long long ret = 1;
  for (; b; b >>= 1, a = (a * a) % MMM)
    if (b & 1) ret = (ret * a) % MMM;
  return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) {
  if (a == 0 || b == 0) return a + b;
  return a * (b / gcd(a, b));
}
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int n, m;
int a[5005];
vector<int> v[5005], vv[5005];
pair<int, int> res[5005];
int main() {
  scanf("%d%d", &n, &m);
  for (int(i) = (1); (i) <= (n); (i) += (1)) {
    scanf("%d", a + i);
    v[a[i]].push_back(i);
  }
  for (int(i) = (1); (i) <= (m); (i) += (1)) {
    int x, y;
    scanf("%d%d", &x, &y);
    vv[x].push_back(y);
  }
  for (int(i) = (1); (i) <= (n); (i) += (1)) sort(vv[i].begin(), vv[i].end());
  for (int(i) = (0); (i) <= (n); (i) += (1)) {
    bool ok = 1;
    pair<long long, long long> rr = make_pair(0, 1);
    for (int(k) = (1); (k) <= (n); (k) += (1)) {
      int L = 0, R = 0;
      for (int x : v[k]) {
        if (x <= i)
          L++;
        else
          R++;
      }
      if (k == a[i]) {
        int l = 0, r = 0;
        for (int x : vv[k]) {
          if (x == L)
            l++;
          else if (x <= R)
            r++;
        }
        if (l == 0) {
          ok = 0;
          break;
        }
        if (r) {
          rr.first += 2;
          rr.second = (rr.second * r) % MOD;
        } else {
          rr.first++;
        }
      } else {
        int l = 0, r = 0, b = 0;
        for (int x : vv[k]) {
          if (x <= L && x <= R)
            b++;
          else if (x <= L)
            l++;
          else if (x <= R)
            r++;
        }
        if (!l && !r && !b) continue;
        if (!l && !b) {
          rr.first++;
          rr.second = (rr.second * r) % MOD;
        } else if (!r && !b) {
          rr.first++;
          rr.second = (rr.second * l) % MOD;
        } else if (!l && !r && b == 1) {
          rr.first++;
          rr.second = (rr.second * 2) % MOD;
        } else {
          rr.first += 2;
          long long tmp = 0;
          tmp += (1LL * l * b) % MOD;
          tmp += (1LL * r * b) % MOD;
          tmp += (1LL * b * (b - 1)) % MOD;
          tmp %= MOD;
          rr.second = (rr.second * tmp) % MOD;
        }
      }
    }
    if (!ok) continue;
    res[i] = rr;
  }
  pair<int, int> ans = make_pair(0, 1);
  for (int(i) = (0); (i) <= (n); (i) += (1)) {
    if (res[i].first > ans.first)
      ans = res[i];
    else if (res[i].first == ans.first)
      ans.second = (ans.second + res[i].second) % MOD;
  }
  if (ans.first == 0) ans.second = 1;
  printf("%d %d\n", ans.first, ans.second);
}
