#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long inf = 5e18;
const int N = 5e3 + 100;
int n, m;
int sweet[N], sumLeft[N], sumRight[N];
pair<int, int> cow[N];
vector<int> a[N];
int pfx[N][N];
map<int, long long> mp;
long long np2(long long n) { return (n * (n - 1)); }
void solve(vector<int> left, vector<int> right, int bp) {
  long long ans = 0, ansCnt = 1;
  bool superFlag = false;
  for (int sweetness = 1; sweetness <= n; sweetness++) {
    int leftCow = 0, rightCow = 0;
    bool leftCowMust = false, rightCowMust = false;
    for (int hunger : a[sweetness]) {
      if (hunger <= sumLeft[sweetness]) {
        leftCow++;
      }
      if (hunger == sumLeft[sweetness] and (left.back() == sweetness)) {
        leftCowMust = true;
        superFlag = true;
      }
      if (hunger <= sumRight[sweetness]) {
        rightCow++;
      }
    }
    if (leftCow == 0 and rightCow > 0) {
      ans++;
      ansCnt = (ansCnt * (rightCow)) % mod;
    } else if (rightCow == 0 and leftCow > 0) {
      ans++;
      if (!leftCowMust) {
        ansCnt = (ansCnt * leftCow) % mod;
      }
    } else if (leftCow > 0 and rightCow > 0) {
      if (leftCow == 1 and rightCow == 1) {
        ans++;
        if (!leftCowMust) {
          ansCnt = (ansCnt * 2) % mod;
        }
      } else {
        ans += 2;
        if (leftCowMust) {
          if (leftCow > rightCow) {
            ansCnt = (ansCnt * max(1, rightCow));
          } else {
            ansCnt = (ansCnt * max(1, rightCow - 1));
          }
        } else {
          if (leftCow > rightCow) {
            swap(leftCow, rightCow);
          }
          ansCnt =
              (ansCnt * (np2(leftCow) + leftCow * (rightCow - leftCow))) % mod;
        }
      }
    }
  }
  if (superFlag or bp == 0) {
    ;
    mp[ans] += ansCnt;
    mp[ans] %= mod;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> sweet[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> cow[i].first >> cow[i].second;
    a[cow[i].first].push_back(cow[i].second);
  }
  for (int i = 1; i <= n; i++) {
    sort(a[i].begin(), a[i].end());
  }
  for (int bp = 0; bp <= n; bp++) {
    vector<int> left, right;
    memset(sumLeft, 0, sizeof(sumLeft));
    memset(sumRight, 0, sizeof(sumRight));
    for (int i = 1; i <= n; i++) {
      if (i > bp) {
        right.push_back(sweet[i]);
        sumRight[sweet[i]]++;
      } else {
        left.push_back(sweet[i]);
        sumLeft[sweet[i]]++;
      }
    }
    reverse(right.begin(), right.end());
    solve(left, right, bp);
  }
  long long ans = 0, ansCnt = 0;
  for (auto& v : mp) {
    ans = v.first, ansCnt = v.second;
  }
  if (ans == 0) {
    cout << 0 << ' ' << 1 << endl;
  } else {
    cout << ans << ' ' << ansCnt << endl;
  }
}
