#include <bits/stdc++.h>
using namespace std;
struct data {
  long long x, y;
} t[5005];
vector<long long> u[5005];
long long n, m, x, y, f[5005], cnt, id[5005], g[5005], ans, sum, val, v,
    mod = 1000000007ll;
int f1[5005][5005], f2[5005][5005], p[5005];
bool b[5005];
bool cmp(long long a, long long b) { return t[a].y < t[b].y; }
void find(long long a) {
  val = 0ll;
  for (long long i = 1ll; i <= cnt; i++) {
    x = 1ll * f1[a][id[i]];
    y = 1ll * f2[a + 1ll][id[i]];
    if (y < x) swap(x, y);
    if (x > 0ll && y > 1ll) {
      val = val + 2ll;
    } else if (y > 0ll) {
      val = val + 1ll;
    }
  }
  if (ans < val) ans = val;
}
void add(long long a) {
  if ((!b[f[a]]) && a != 0) return;
  val = 0ll;
  v = 1ll;
  for (long long i = 1ll; i <= cnt; i++) {
    x = 1ll * f1[a][id[i]];
    y = 1ll * f2[a + 1ll][id[i]];
    if (id[i] == f[a]) {
      if (a == 0ll) {
        if (y != 0) {
          v = (v * y) % mod;
          val++;
        }
      } else {
        x = x - 1ll * f1[a - 1ll][id[i]];
        if (x == 0ll)
          v = 0ll;
        else {
          if ((y > 1ll) || (y > 0ll && 1ll * f1[a][id[i]] > y)) {
            val = val + 2ll;
            v = (v * x) % mod;
            if (f1[a][id[i]] <= y)
              v = (v * (y - 1ll)) % mod;
            else
              v = (v * y) % mod;
          } else {
            val = val + 1ll;
            v = (v * x) % mod;
          }
        }
      }
    } else {
      if (y < x) swap(x, y);
      if (x > 0ll && y > 1ll) {
        val = val + 2ll;
        v = (v * x) % mod;
        v = (v * (y - 1ll)) % mod;
      } else if (y > 0ll) {
        if (x == 1ll) y = max(y, 2ll);
        val = val + 1ll;
        v = (v * y) % mod;
      }
    }
  }
  if (val == ans) {
    sum = (sum + v) % mod;
  }
}
int main() {
  scanf("%lld%lld", &n, &m);
  for (long long i = 1ll; i <= n; i++) scanf("%lld", &f[i]);
  for (long long i = 1ll; i <= m; i++) {
    scanf("%lld%lld", &t[i].x, &t[i].y);
    if (!b[t[i].x]) {
      b[t[i].x] = 1ll;
      cnt++;
      id[cnt] = t[i].x;
    }
    u[t[i].x].push_back(i);
  }
  for (long long i = 1ll; i <= cnt; i++) {
    sort(u[id[i]].begin(), u[id[i]].end(), cmp);
  }
  for (long long i = 1ll; i <= cnt; i++) {
    p[id[i]] = -1ll;
    g[id[i]] = 0ll;
  }
  for (long long i = 1ll; i <= n; i++) {
    sum = u[f[i]].size();
    g[f[i]]++;
    while (p[f[i]] < sum - 1ll) {
      if (t[u[f[i]][p[f[i]] + 1ll]].y <= g[f[i]]) {
        p[f[i]]++;
      } else
        break;
    }
    for (long long j = 1ll; j <= cnt; j++) f1[i][id[j]] = p[id[j]] + 1;
  }
  for (long long i = 1ll; i <= cnt; i++) {
    p[id[i]] = -1ll;
    g[id[i]] = 0ll;
  }
  for (long long i = n; i >= 1ll; i--) {
    sum = u[f[i]].size();
    g[f[i]]++;
    while (p[f[i]] < sum - 1ll) {
      if (t[u[f[i]][p[f[i]] + 1ll]].y <= g[f[i]]) {
        p[f[i]]++;
      } else
        break;
    }
    for (long long j = 1ll; j <= cnt; j++) f2[i][id[j]] = p[id[j]] + 1;
  }
  sum = 0ll;
  for (long long i = 0ll; i <= n; i++) find(i);
  for (long long i = 0ll; i <= n; i++) add(i);
  if (ans == 0ll) sum = 1ll;
  printf("%lld %lld\n", ans, sum);
}
