#include <bits/stdc++.h>
using namespace std;
template <typename _tp>
inline void read(_tp& x) {
  char ch = getchar(), sgn = 0;
  x = 0;
  while (ch != '-' and !isdigit(ch)) ch = getchar();
  if (ch == '-') ch = getchar(), sgn = 1;
  while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
  sgn && (x = -x);
}
void quit(int x) {
  printf("%d\n", x);
  exit(0);
}
const int N = 50200;
int s[N];
int n, m, p;
struct modfy {
  long long rk;
  int ps, vl;
  inline void in() {
    read(rk);
    ps = rk % n;
    rk /= n;
    read(vl);
  }
  friend inline bool operator<(const modfy& A, const modfy& B) {
    return A.rk == B.rk ? A.ps < B.ps : A.rk < B.rk;
  }
} c[N];
struct mt2 {
  int a0, a1;
};
struct mt4 {
  int a[2][2];
  friend inline mt4 operator*(const mt4& A, const mt4& B) {
    mt4 res;
    res.a[0][0] =
        ((long long)A.a[0][0] * B.a[0][0] + (long long)A.a[0][1] * B.a[1][0]) %
        p,
    res.a[0][1] =
        ((long long)A.a[0][0] * B.a[0][1] + (long long)A.a[0][1] * B.a[1][1]) %
        p,
    res.a[1][0] =
        ((long long)A.a[1][0] * B.a[0][0] + (long long)A.a[1][1] * B.a[1][0]) %
        p,
    res.a[1][1] =
        ((long long)A.a[1][0] * B.a[0][1] + (long long)A.a[1][1] * B.a[1][1]) %
        p;
    return res;
  }
} seg[N << 2];
inline mt2 operator*(const mt2& A, const mt4& B) {
  mt2 res;
  res.a0 = ((long long)A.a0 * B.a[0][0] + (long long)A.a1 * B.a[1][0]) % p,
  res.a1 = ((long long)A.a0 * B.a[0][1] + (long long)A.a1 * B.a[1][1]) % p;
  return res;
}
inline void trans(mt2& A, const int x, const int y) {
  A.a0 = ((long long)A.a0 * x + (long long)A.a1 * y) % p;
  swap(A.a0, A.a1);
}
inline mt4 qpow(mt4 A, long long B) {
  mt4 res;
  res.a[0][0] = res.a[1][1] = 1;
  res.a[0][1] = res.a[1][0] = 0;
  while (B) {
    if (B & 1) res = res * A;
    A = A * A, B >>= 1;
  }
  return res;
}
void build(int l, int r, int x) {
  if (l == r) {
    seg[x].a[0][0] = 0;
    seg[x].a[1][0] = 1;
    seg[x].a[0][1] = s[l];
    seg[x].a[1][1] = s[l + 1];
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, x << 1);
  build(mid + 1, r, x << 1 | 1);
  seg[x] = seg[x << 1] * seg[x << 1 | 1];
}
mt4 qry(int l, int r, int x, int L, int R) {
  if (L <= l and r <= R) return seg[x];
  int mid = l + r >> 1;
  if (R <= mid) return qry(l, mid, x << 1, L, R);
  if (mid < L) return qry(mid + 1, r, x << 1 | 1, L, R);
  return qry(l, mid, x << 1, L, R) * qry(mid + 1, r, x << 1 | 1, L, R);
}
int main() {
  long long T;
  read(T);
  read(p), read(n);
  int PS = T % n;
  T /= n;
  for (int i = 0; i < n; ++i) read(s[i]);
  s[n] = s[0];
  read(m);
  for (int i = 1; i <= m; ++i) c[i].in();
  ++m;
  c[m].rk = T + 1, c[m].ps = n + 1;
  ++m;
  c[m].rk = 0, c[m].ps = 0, c[m].vl = s[0];
  c[0].ps = c[m + 1].ps = -1;
  sort(c + 1, c + m + 1);
  if (n > 1)
    build(0, n - 2, 1);
  else
    seg[1].a[0][0] = seg[1].a[1][1] = 1, seg[1].a[0][1] = seg[1].a[1][0] = 0;
  mt4 cr;
  cr.a[0][0] = 0, cr.a[0][1] = s[n - 1];
  cr.a[1][0] = 1, cr.a[1][1] = s[0];
  mt2 bs;
  bs.a0 = 0, bs.a1 = 1;
  for (int l = 1, r = 1, s1, s0; l <= m; l = r + 1) {
    while (r < m and c[l].rk == c[r + 1].rk) ++r;
    if (T == c[l].rk and PS < c[l].ps) {
      if (PS == 0) quit(bs.a0);
      if (PS == 1) quit(bs.a1);
      bs = bs * qry(0, n - 2, 1, 0, PS - 2);
      quit(bs.a1);
    }
    c[l - 1].ps = -1;
    for (int i = l; i <= r; ++i) {
      if (c[i - 1].ps + 1 <= c[i].ps - 2)
        bs = bs * qry(0, n - 2, 1, c[i - 1].ps + 1, c[i].ps - 2);
      if (c[i - 1].ps <= c[i].ps - 2) trans(bs, s[c[i].ps - 1], c[i].vl);
      if (T == c[i].rk) {
        if (c[i].ps == PS) quit(bs.a0);
        if (c[i].ps + 1 == PS) quit(bs.a1);
      }
      if ((i < r and c[i].ps + 1 == c[i + 1].ps) or
          (i == r and c[i].rk + 1 == c[i + 1].rk and c[i].ps == n - 1 and
           c[i + 1].ps == 0))
        trans(bs, c[i].vl, c[i + 1].vl);
      else
        trans(bs, c[i].vl, s[c[i].ps + 1]);
      if (T == c[i].rk) {
        if (i == r or (i < r and PS < c[i + 1].ps)) {
          bs = bs * qry(0, n - 2, 1, c[i].ps + 1, PS - 1);
          quit(bs.a0);
        }
      }
    }
    if (c[r].ps + 1 <= n - 2) bs = bs * qry(0, n - 2, 1, c[r].ps + 1, n - 2);
    bool chg = false;
    if (c[r].rk + 1 != c[r + 1].rk) {
      if (c[r].ps != n - 1) {
        s1 = s[n - 1], s0 = s[0];
        if (c[r].ps == n - 1) s1 = c[r].vl;
        trans(bs, s1, s0);
      }
      if (T < c[r + 1].rk) {
        bs = bs * qpow(seg[1] * cr, T - c[r].rk - 1);
        if (PS == 0) quit(bs.a0);
        if (PS == 1) quit(bs.a1);
        bs = bs * qry(0, n - 2, 1, 0, PS - 2);
        quit(bs.a1);
      }
      bs = bs * seg[1];
      bs = bs * qpow(cr * seg[1], c[r + 1].rk - c[r].rk - 2);
      chg = true;
    }
    if (chg or c[r].ps != n - 1) {
      s1 = s[n - 1], s0 = s[0];
      if (c[r].rk + 1 == c[r + 1].rk and c[r].ps == n - 1) s1 = c[r].vl;
      if (c[r + 1].ps == 0) s0 = c[r + 1].vl;
      trans(bs, s1, s0);
    }
  }
  puts("ERROR in END");
  return 0;
}
