#include <bits/stdc++.h>
using namespace std;
pair<int, long long> ans(0, 1);
void updateAns(int n, long long w) {
  if (n > ans.first)
    ans = make_pair(n, w);
  else if (n == ans.first)
    ans.second = (ans.second + w) % 1000000007;
}
int main() {
  int N, M, f, h;
  int S[5000];
  vector<pair<int, int>> sleep[5000 + 1];
  int cnt[5000 + 1] = {0}, cursor[5000 + 1] = {0};
  cin >> N >> M;
  for (int i = 0; i < N; i++) cin >> S[i];
  for (int i = 0; i < M; i++) {
    cin >> f >> h;
    sleep[f].emplace_back(h, 0);
  }
  for (int i = 1; i <= N; i++) sort(sleep[i].begin(), sleep[i].end());
  for (int i = N - 1; i >= 0; i--) {
    int s = S[i];
    cnt[s]++;
    if (cursor[s] < sleep[s].size() && sleep[s][cursor[s]].first == cnt[s])
      sleep[s][cursor[s]++].second = i;
  }
  memset(cnt, 0, sizeof(cnt));
  memset(cursor, 0, sizeof(cursor));
  for (int i = 0; i < N; i++) {
    int s = S[i];
    cnt[s]++;
    if (cursor[s] < sleep[s].size() && sleep[s][cursor[s]].first == cnt[s])
      sleep[s][cursor[s]++].first = i;
  }
  for (int i = 1; i <= N; i++)
    sleep[i].erase(sleep[i].begin() + cursor[i], sleep[i].end());
  memset(cnt, 0, sizeof(cnt));
  for (int i = 1; i <= N; i++)
    if (cursor[i]) {
      ans.first++;
      ans.second = ans.second * cursor[i] % 1000000007;
    }
  for (int i = 0; i < N; i++) {
    int s = S[i];
    if (cnt[s] >= sleep[s].size() || sleep[s][cnt[s]].first != i) continue;
    cnt[s]++;
    int cow_cnt = 1;
    long long ways = 1;
    for (int j = 1; j <= N; j++) {
      while (cursor[j] > 0 && sleep[j][cursor[j] - 1].second <= i) cursor[j]--;
      if (j == s) {
        int r = cursor[j] - (cursor[j] >= cnt[j]);
        cow_cnt += (r > 0);
        ways = ways * max(1, r) % 1000000007;
      } else if (cursor[j] == 1 && cnt[j] == 1) {
        cow_cnt++;
        ways = ways * 2 % 1000000007;
      } else {
        cow_cnt += (cursor[j] > 0) + (cnt[j] > 0);
        ways = ways *
               (max(1, cnt[j]) * max(1, cursor[j]) - min(cnt[j], cursor[j])) %
               1000000007;
      }
    }
    updateAns(cow_cnt, ways);
  }
  cout << ans.first << " " << ans.second << endl;
}
