func_code_string: #include <bits/stdc++.h>   using namespace std;   const int maxn = 100100;   int P, n, m, s[maxn];   long long K;   struct que {     long long x;     int prey, y;     bool operator<(const que &a) const { return x < a.x; }   } q[maxn];   struct Matrix {     int a[2][2];     Matrix() { memset(a, 0, sizeof a); }     Matrix(int a_, int b_) {       a[0][0] = a_, a[0][1] = 1;       a[1][0] = b_, a[1][1] = 0;     }     void init(int x) {       memset(a, 0, sizeof a);       a[0][0] = a[1][1] = x;     }     Matrix operator*(const Matrix &b) {       Matrix c;       c.init(0);       for (int i = 0; i < 2; i++)         for (int j = 0; j < 2; j++)           for (int k = 0; k < 2; k++)             c.a[i][j] = (c.a[i][j] + 1ll * a[i][k] * b.a[k][j] % P) % P;       return c;     }   } tr[maxn * 4];   int le, ri;   void build(int rt, int l, int r) {     if (l == r) {       tr[rt] = Matrix(s[l], s[l - 1]);       return;     }     int mid = (l + r) / 2;     build(rt * 2, l, mid);     build(rt * 2 + 1, mid + 1, r);     tr[rt] = tr[rt * 2] * tr[rt * 2 + 1];   }   Matrix query(int rt, int l, int r) {     if (le <= l && ri >= r) return tr[rt];     int mid = (l + r) / 2;     if (ri <= mid) return query(rt * 2, l, mid);     if (le > mid) return query(rt * 2 + 1, mid + 1, r);     return query(rt * 2, l, mid) * query(rt * 2 + 1, mid + 1, r);   }   Matrix qk_pow(Matrix a, long long b) {     Matrix p;     p.init(1);     for (; b; b /= 2, a = a * a)       if (b & 1) p = p * a;     return p;   }   int get(int x) { return x ? x : n; }   Matrix query(long long l, long long r) {     long long rr = l + n - get((l - 1) % n);     if (rr > r) {       le = get((l - 1) % n), ri = get((r - 1) % n);       return query(1, 1, n);     } else {       Matrix c;       c.init(0);       le = get((l - 1) % n), ri = n;       c = query(1, 1, n);       c = c * qk_pow(tr[1], (r - rr) / n);       if ((r - rr) % n) le = 1, ri = (r - rr) % n, c = c * query(1, 1, n);       return c;     }   }   int main() {     scanf( %lld%d%d , &K, &P, &n);     if (K == 0 || K == 1) {       printf( %lld , K % P);       return 0;     }     for (int i = 0; i < n; i++) scanf( %d , &s[i]);     s[n] = s[0];     build(1, 1, n);     scanf( %d , &m);     for (int i = 1; i <= m; i++) scanf( %lld%d , &q[i].x, &q[i].y);     sort(q + 1, q + m + 1);     while (q[m].x > K) m--;     if (q[m].x != K) q[++m].x = K, q[m].y = s[m % n];     Matrix ans;     ans.a[0][0] = 1;     for (int i = 1; i <= m; i++)       q[i].prey = q[i - 1].x + 1 == q[i].x ? q[i - 1].y : s[(q[i].x - 1) % n];     q[0].x = 1, q[0].y = s[1], q[0].prey = s[0];     for (int i = 1; i <= m; i++) {       ans = ans * Matrix(q[i - 1].y, q[i - 1].prey);       if (q[i].x == q[i - 1].x + 1) continue;       ans = ans * Matrix(s[(q[i - 1].x + 1) % n], q[i - 1].y);       if (q[i].x == q[i - 1].x + 2) continue;       ans = ans * query(q[i - 1].x + 3, q[i].x);     }     printf( %d n , ans.a[0][0]);     fclose(stdin);     fclose(stdout);     return 0;   }   