//
// Created by wulang on 10/3/16.
//

#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;

const int N=2000+10;
int a[N], n,m;
int cnt[N];
int main(int argc, char *argv[]) {
    if (argc == 2) {
        freopen(argv[1], "r", stdin);
    }

    while(cin>>n>>m) {
        for(int i=0;i<n;i++) cin>>a[i];
        for(int i=1;i<=m;i++) cnt[i]=0;
        for(int i=0;i<n;i++) {
            if(a[i]<=m) cnt[a[i]]++;
        }

        if(n<m) {
            int ans=0,change=0;
            bool flag=0;
            for(int i=0;i<n;i++) {
                if(a[i]>m) {
                    a[i]=1;
                    change++;
                }
            }
            cout<<ans<<" "<<change<<endl;
            for(int i=0;i<n;i++) {
                printf("%d%c",a[i], i==n-1?'\n':' ');
            }
            continue;
        }

        int ans= n/m;

        int change=0;
        for(int i=1;i<=m;i++) {
            if(cnt[i]>=ans) continue;
            for(int j=0; j<n && cnt[i]<ans; j++) {
                if(a[j]>m) {
                    a[j] = i;
                    cnt[i]++;
                    change++;
                }
            }

            for(int j=0; j<n && cnt[i]<ans;j++) {
                if(cnt[a[j]]>ans+1) {
                    cnt[a[j]]--;
                    a[j] = i;
                    cnt[i]++;
                    change++;
                }
            }
            for(int j=0; j<n && cnt[i]<ans; j++) {
                if(cnt[a[j]]>ans) {
                    cnt[a[j]]--;
                    a[j]=i;
                    cnt[i]++;
                    change++;
                }
            }
        }

        for(int j=0;j<n;j++) {
            if(a[j]>m) {
                a[j]=1;
            }
        }

        cout<<ans<<" "<<change<<endl;
        for(int i=0;i<n;i++) {
            printf("%d%c",a[i], i==n-1?'\n':' ');
        }

    }

    return 0;
}


//
// Created by wulang on 10/3/16.
//

#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;

const int N=2000+10;
int a[N], n,m;
int cnt[N];
int main(int argc, char *argv[]) {
    if (argc == 2) {
        freopen(argv[1], "r", stdin);
    }

    while(cin>>n>>m) {
        for(int i=0;i<n;i++) cin>>a[i];
        int ans= n/m;

        for(int i=1;i<=m;i++) cnt[i]=0;
        for(int i=0;i<n;i++) {
            if(a[i]<=m) cnt[a[i]]++;
        }

        int change=0;
        for(int i=1;i<=m;i++) {
            if(cnt[i]>=ans) continue;
            for(int j=0; j<n && cnt[i]<ans; j++) {
                if(a[j]>m) {
                    a[j] = i;
                    cnt[i]++;
                    change++;
                }
            }

            for(int j=0; j<n && cnt[i]<ans;j++) {
                if(cnt[a[j]]>ans+1) {
                    cnt[a[j]]--;
                    a[j] = i;
                    cnt[i]++;
                    change++;
                }
            }
            for(int j=0; j<n && cnt[i]<ans; j++) {
                if(cnt[a[j]]>ans) {
                    cnt[a[j]]--;
                    a[j]=i;
                    cnt[i]++;
                    change++;
                }
            }
        }

        for(int j=0;j<n;j++) {
            if(a[j]>m) {
                for(int i=1;i<=m;i++) {
                    if(cnt[i]<ans+1) {
                        a[j]=i;
                        cnt[i]++;
                        change++;
                    }
                }
            }
        }

        cout<<ans<<" "<<change<<endl;
        for(int i=0;i<n;i++) {
            printf("%d%c",a[i], i==n-1?'\n':' ');
        }

    }

    return 0;
}


//
// Created by wulang on 10/3/16.
//

#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;

const int N=2000+10;
int a[N], n,m;
int cnt[N];
int main(int argc, char *argv[]) {
    if (argc == 2) {
        freopen(argv[1], "r", stdin);
    }

    while(cin>>n>>m) {
        for(int i=0;i<n;i++) cin>>a[i];
        for(int i=1;i<=m;i++) cnt[i]=0;
        for(int i=0;i<n;i++) {
            if(a[i]<=m) cnt[a[i]]++;
        }

        if(n<m) {
            int ans=1,change=0;
            bool flag=0;
            for(int i=0;i<n;i++) {
                if(a[i]>m) {
                    flag=1;
                    for(int j=1;j<=m;j++) {
                        if(cnt[j]==0) {
                            a[i]=j;
                            cnt[j]++;
                            change++;
                            break;
                        }
                    }
                }
            }

            for(int i=1;i<=m && !flag;i++) {
                if(cnt[i]>=1) continue;
                for(int j=0;j<n && cnt[i]<1; j++) {
                    if (cnt[a[j]] > ans) {
                        cnt[a[j]]--;
                        a[j] = i;
                        change++;
                        cnt[i]++;
                        flag = 1;
                    }
                }
            }

            cout<<ans<<" "<<change<<endl;
            for(int i=0;i<n;i++) {
                printf("%d%c",a[i], i==n-1?'\n':' ');
            }
            continue;
        }

        int ans= n/m;

        int change=0;
        for(int i=1;i<=m;i++) {
            if(cnt[i]>=ans) continue;
            for(int j=0; j<n && cnt[i]<ans; j++) {
                if(a[j]>m) {
                    a[j] = i;
                    cnt[i]++;
                    change++;
                }
            }

            for(int j=0; j<n && cnt[i]<ans;j++) {
                if(cnt[a[j]]>ans+1) {
                    cnt[a[j]]--;
                    a[j] = i;
                    cnt[i]++;
                    change++;
                }
            }
            for(int j=0; j<n && cnt[i]<ans; j++) {
                if(cnt[a[j]]>ans) {
                    cnt[a[j]]--;
                    a[j]=i;
                    cnt[i]++;
                    change++;
                }
            }
        }

        for(int j=0;j<n;j++) {
            if(a[j]>m) {
                a[j]=1;
            }
        }

        cout<<ans<<" "<<change<<endl;
        for(int i=0;i<n;i++) {
            printf("%d%c",a[i], i==n-1?'\n':' ');
        }

    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define sq(x) ((x)*(x))
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mem(x, y) memset(x, y, sizeof(x))
#define _c1(x) cout<<(x)<<endl;
#define _c2(x, y) cout<<(x)<<" "<<(y)<<endl;
#define _c3(x, y, z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl;
#define _c4(x, n) do{for(int _=0;_<n;cout<<(x)[_++]<<" ");puts("");}while(0)
#define _c5(x, n, m) do{for(int __=0;__<n;__++)_c4((x)[__],m);puts("");}while(0)
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned int U;
typedef pair<int, int> PII;

const int N = 2e3 + 100;
int n, m;
int a[N], b[N], res;
int FindLow() {
    for (int i = 0; i < m; i++) {
        if (a[i] < 0) return i;
    }
    return -1;
}
int FindHigh() {
    for (int i = 0; i < m; i++) {
        if (a[i] > 0) return i;
    }
    return -1;
}

int main(void){
#ifdef QT_QML_DEBUG
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif

    scanf("%d%d", &n, &m);
    int tmp, ta = n / m; res = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &tmp);
        b[i] = tmp;
        if (tmp <= m) a[tmp-1] ++;
        else res ++;
    }

    int ans = 0;
    for (int i = 0; i < m; i++) {
        if (a[i] < ta) ans += ta - a[i];
        a[i] = ta - a[i];
    }
    printf("%d %d\n", ta, ans);
    for (int i = 0; i < n; i++) {
        if (b[i] > m) {
            int k = FindHigh();
            if (k != -1) {
                b[i] = k + 1;
                a[k] --;
            } else b[i] = 1;
        }
    }
    for (int i = 0; i < n; i++) {
        if (a[b[i]-1] < 0) {
            int k = FindHigh();
            if (k != -1) {
                a[k] --;
                a[b[i]-1] ++;
                b[i] = k + 1;
            }
        }

        printf("%d", b[i]);
        if (i == n-1) printf("\n");
        else printf(" ");
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define sq(x) ((x)*(x))
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mem(x, y) memset(x, y, sizeof(x))
#define _c1(x) cout<<(x)<<endl;
#define _c2(x, y) cout<<(x)<<" "<<(y)<<endl;
#define _c3(x, y, z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl;
#define _c4(x, n) do{for(int _=0;_<n;cout<<(x)[_++]<<" ");puts("");}while(0)
#define _c5(x, n, m) do{for(int __=0;__<n;__++)_c4((x)[__],m);puts("");}while(0)
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned int U;
typedef pair<int, int> PII;

const int N = 2e3 + 100;
int n, m;
int a[N], b[N], res;
int FindLow() {
    for (int i = 0; i < m; i++) {
        if (a[i] < 0) return i;
    }
    return -1;
}
int FindHigh() {
    for (int i = 0; i < m; i++) {
        if (a[i] > 0) return i;
    }
    return -1;
}

int main(void){
#ifdef QT_QML_DEBUG
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif

    scanf("%d%d", &n, &m);
    int tmp, ta = n / m; res = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &tmp);
        b[i] = tmp;
        if (tmp <= m) a[tmp-1] ++;
        else res ++;
    }

    int ans = 0;
    for (int i = 0; i < m; i++) {
        if (a[i] < ta) ans += ta - a[i];
        a[i] = ta - a[i];
    }
    printf("%d %d\n", ta, ans);
    for (int i = 0; i < n; i++) {
        if (b[i] <= m) {
            if (a[b[i]-1] < 0) {
                int k = FindHigh();
                if (k != -1) {
                    a[b[i]-1] ++;
                    a[k] --; b[i] = k+1;
                }
            }
        } else {
            int k = FindHigh();
            a[k] --;
            b[i] = k+1;
        }

        printf("%d", b[i]);
        if (i == n-1) printf("\n");
        else printf(" ");
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define sq(x) ((x)*(x))
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mem(x, y) memset(x, y, sizeof(x))
#define _c1(x) cout<<(x)<<endl;
#define _c2(x, y) cout<<(x)<<" "<<(y)<<endl;
#define _c3(x, y, z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl;
#define _c4(x, n) do{for(int _=0;_<n;cout<<(x)[_++]<<" ");puts("");}while(0)
#define _c5(x, n, m) do{for(int __=0;__<n;__++)_c4((x)[__],m);puts("");}while(0)
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned int U;
typedef pair<int, int> PII;

const int N = 2e3 + 100;
int n, m;
int a[N], b[N], res;
int FindLow() {
    for (int i = 0; i < m; i++) {
        if (a[i] < 0) return i;
    }
    return -1;
}
int FindHigh() {
    for (int i = 0; i < m; i++) {
        if (a[i] > 0) return i;
    }
    return -1;
}

int main(void){
#ifdef QT_QML_DEBUG
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif

    scanf("%d%d", &n, &m);
    int tmp, ta = n / m; res = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &tmp);
        b[i] = tmp;
        if (tmp <= m) a[tmp-1] ++;
        else res ++;
    }

    int ans = 0;
    for (int i = 0; i < m; i++) {
        if (a[i] < ta) ans += ta - a[i];
        a[i] = ta - a[i];
    }
    printf("%d %d\n", ta, ans);
    for (int i = 0; i < n; i++) {
        if (b[i] > m) {
            int k = FindHigh();
            if (k != -1) {
                b[i] = k + 1;
                a[k] --;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        if (b[i] <= m) {
            if (a[b[i]-1] < 0) {
                int k = FindHigh();
                if (k != -1) {
                    a[k] --;
                    a[b[i]-1] ++;
                    b[i] = k + 1;
                }
            }
        }

        printf("%d", b[i]);
        if (i == n-1) printf("\n");
        else printf(" ");
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define sq(x) ((x)*(x))
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mem(x, y) memset(x, y, sizeof(x))
#define _c1(x) cout<<(x)<<endl;
#define _c2(x, y) cout<<(x)<<" "<<(y)<<endl;
#define _c3(x, y, z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl;
#define _c4(x, n) do{for(int _=0;_<n;cout<<(x)[_++]<<" ");puts("");}while(0)
#define _c5(x, n, m) do{for(int __=0;__<n;__++)_c4((x)[__],m);puts("");}while(0)
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned int U;
typedef pair<int, int> PII;

const int N = 2e3 + 100;
int n, m;
int a[N], b[N], res;
int FindLow() {
    for (int i = 0; i < m; i++) {
        if (a[i] < 0) return i;
    }
    return -1;
}
int FindHigh() {
    for (int i = 0; i < m; i++) {
        if (a[i] > 0) return i;
    }
    return -1;
}

int main(void){
#ifdef QT_QML_DEBUG
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif

    scanf("%d%d", &n, &m);
    int tmp, ta = n / m; res = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &tmp);
        b[i] = tmp;
        if (tmp <= m) a[tmp-1] ++;
        else res ++;
    }

    int ans = 0;
    for (int i = 0; i < m; i++) {
        if (a[i] < ta) ans += ta - a[i];
        a[i] = ta - a[i];
    }
    printf("%d %d\n", ta, ans);
    for (int i = 0; i < n; i++) {
        if (b[i] > m) {
            int k = FindHigh();
            if (k != -1) {
                b[i] = k + 1;
                a[k] --;
            }
        }
    }
    for (int i = 0; i < n; i++) {
        if (a[b[i]-1] < 0) {
            int k = FindHigh();
            if (k != -1) {
                a[k] --;
                a[b[i]-1] ++;
                b[i] = k + 1;
            }
        }

        printf("%d", b[i]);
        if (i == n-1) printf("\n");
        else printf(" ");
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define sq(x) ((x)*(x))
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mem(x, y) memset(x, y, sizeof(x))
#define _c1(x) cout<<(x)<<endl;
#define _c2(x, y) cout<<(x)<<" "<<(y)<<endl;
#define _c3(x, y, z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl;
#define _c4(x, n) do{for(int _=0;_<n;cout<<(x)[_++]<<" ");puts("");}while(0)
#define _c5(x, n, m) do{for(int __=0;__<n;__++)_c4((x)[__],m);puts("");}while(0)
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned int U;
typedef pair<int, int> PII;

const int N = 2e3 + 100;
int n, m;
int a[N], b[N], res;
int FindLow() {
    for (int i = 0; i < m; i++) {
        if (a[i] < 0) return i;
    }
    return -1;
}
int FindHigh() {
    for (int i = 0; i < m; i++) {
        if (a[i] > 0) return i;
    }
    return -1;
}

int main(void){
#ifdef QT_QML_DEBUG
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif

    scanf("%d%d", &n, &m);
    int tmp, ta = n / m; res = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &tmp);
        b[i] = tmp;
        if (tmp <= m) a[tmp-1] ++;
        else res ++;
    }

    int ans = 0;
    for (int i = 0; i < m; i++) {
        if (a[i] < ta) ans += ta - a[i];
        a[i] = ta - a[i];
    }
    printf("%d %d\n", ta, ans);
    for (int i = 0; i < n; i++) {
        if (b[i] > m) {
            int k = FindHigh();
            b[i] = k + 1;
            a[k] --;
        }
    }
    for (int i = 0; i < n; i++) {
        if (a[b[i]-1] < 0) {
            int k = FindHigh();
            if (k != -1) {
                a[k] --;
                a[b[i]-1] ++;
                b[i] = k + 1;
            }
        }

        printf("%d", b[i]);
        if (i == n-1) printf("\n");
        else printf(" ");
    }

    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define sq(x) ((x)*(x))
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mem(x, y) memset(x, y, sizeof(x))
#define _c1(x) cout<<(x)<<endl;
#define _c2(x, y) cout<<(x)<<" "<<(y)<<endl;
#define _c3(x, y, z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl;
#define _c4(x, n) do{for(int _=0;_<n;cout<<(x)[_++]<<" ");puts("");}while(0)
#define _c5(x, n, m) do{for(int __=0;__<n;__++)_c4((x)[__],m);puts("");}while(0)
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned int U;
typedef pair<int, int> PII;

const int N = 2e3 + 100;
int n, m;
int a[N], b[N], res;
int FindLow() {
    for (int i = 0; i < m; i++) {
        if (a[i] < 0) return i;
    }
    return -1;
}
int FindHigh() {
    for (int i = 0; i < m; i++) {
        if (a[i] > 0) return i;
    }
    return -1;
}

int main(void){
#ifdef QT_QML_DEBUG
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif

    scanf("%d%d", &n, &m);
    int tmp, ta = n / m; res = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", &tmp);
        b[i] = tmp;
        if (tmp <= m) a[tmp-1] ++;
        else res ++;
    }

    int ans = 0;
    for (int i = 0; i < m; i++) {
        if (a[i] < ta) ans += ta - a[i];
        a[i] = ta - a[i];
    }
    printf("%d %d\n", ta, ans);
    for (int i = 0; i < n; i++) {
        if (b[i] <= m) {
            if (a[b[i]-1] < 0) {
                int k = FindHigh();
                if (k != -1) {
                    a[b[i]-1] ++;
                    a[k] --; b[i] = k+1;
                }
            }
        } else {
            int k = FindHigh();
            if (k != -1) {
                a[k] --;
                b[i] = k+1;
            } else {
                b[i] = 1;
            }
        }

        printf("%d", b[i]);
        if (i == n-1) printf("\n");
        else printf(" ");
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> par;

const int MAXN = 4000;

int a[MAXN];
int sol[MAXN];
int cnt[MAXN];

int n, m, k;

int getmn(){
    int mn = 1;
    for( int i = 1; i <= m; i++ ){
        if( cnt[i] <= cnt[mn] ){
            mn = i;
        }
    }

    return mn;
}

int getmx(){
    int mx = -1;
    for( int i = 0; i < n; i++ ){
        if( (sol[i] > m || cnt[ sol[i] ] >= k+1) && ( mx == -1 || sol[mx]>m || sol[mx] == a[mx] ) ){
            mx = i;
        }
    }

    return mx;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    //freopen("dat.txt","r",stdin);

    cin >> n >> m;

    k = n / m;

    for( int i = 0; i < n; i++ ){
        cin >> a[i];
        sol[i] = a[i];

        if( sol[i] <= m ){
            cnt[ sol[i] ]++;
        }
    }

    /*for( int i = 0; i < n; i++ ){
        if( sol[i] > m ){
            sol[i] = getmn();
            cnt[ sol[i] ]++;
        }
    }*/

    int mn;
    while( cnt[ (mn = getmn()) ] < k ){
        int mx = getmx();

        //cerr << mn << ' ' << mx << '\n';

        if( sol[mx] <= m ){
            cnt[sol[mx]]--;
        }
        sol[mx] = mn;
        cnt[mn]++;

        //cerr << "------\n";
    }

    int ch = 0;
    for( int i = 0; i < n; i++ ){
        if( sol[i] != a[i] ){
            ch++;
        }
    }

    cout << k << ' ' << ch << '\n';
    for( int i = 0; i < n; i++ ){
        cout << sol[i] << " \n"[i+1==n];
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> par;

const int MAXN = 4000;

int a[MAXN];
int sol[MAXN];
map<int,int> dic;

int cnt[MAXN];

int n, m;

int getmn(){
    int mn = 1;
    for( int i = 1; i <= m; i++ ){
        if( cnt[i] <= cnt[mn] ){
            mn = i;
        }
    }

    return mn;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    //freopen("dat.txt","r",stdin);

    cin >> n >> m;

    int k = n / m;

    for( int i = 0; i < n; i++ ){
        cin >> a[i];

        sol[i] = a[i];

        if( sol[i] <= m ){
            cnt[ sol[i] ]++;
        }
    }

    for( int i = 0; i < n; i++ ){
        if( sol[i] > m ){
            sol[i] = getmn();
            cnt[ sol[i] ]++;
        }
    }

    bool ok = true;

    while( ok && cnt[ getmn() ] < k ){
        ok = false;

        for( int i = 0; i < n; i++ ){
            int mn = getmn();

            if( cnt[mn] >= k ){
                break;
            }

            if( cnt[ sol[i] ] > k+1 ){
                ok = true;
                cnt[sol[i]]--;
                sol[i] = mn;
                cnt[sol[i]]++;
            }
        }
    }

    ok = true;

    while( ok && cnt[ getmn() ] < k ){
        ok = false;

        for( int i = 0; i < n; i++ ){
            int mn = getmn();

            if( cnt[mn] >= k ){
                break;
            }

            if( cnt[ sol[i] ] > k ){
                ok = true;
                cnt[sol[i]]--;
                sol[i] = mn;
                cnt[sol[i]]++;
            }
        }
    }

    int changes = 0;
    for( int i = 0; i < n; i++ ){
        if( a[i] != sol[i] ){
            changes++;
        }
    }

    cout << k << ' ' << changes << '\n';
    for( int i = 0; i < n; i++ ){
        if( i > 0 ){
            cout << ' ';
        }

        cout << sol[i];
    }

    cout << '\n';
}


#include<bits/stdc++.h>
using namespace std;

const int N=30010;
int n,ha,block,hs;
int a[N],Hash[N];
int size[N],cnt[N],ans[N];
struct Qry{int l,r,id;} q[N];

bool operator < (const Qry &a,const Qry &b){return a.l/block<b.l/block||a.l/block==b.l/block&&a.r<b.r;}

struct Matrix
{
	int a[3][3],m,n;
	Matrix(){memset(a,0,sizeof(a));m=2;n=1;}
	int* operator [] (const int &x){return a[x];}

	friend Matrix operator * (Matrix A,Matrix B)
	{
		Matrix C;C.m=A.m;C.n=B.n;
		for(int i=1;i<=A.m;i++)
			for(int j=1;j<=B.n;j++)
				for(int k=1;k<=A.n;k++)
					C[i][j]=(C[i][j]+A[i][k]*B[k][j])%ha;
		return C;
	}

	friend Matrix operator + (Matrix A,Matrix B)
	{
		Matrix C;C.m=A.m;C.n=A.n;
		for(int i=1;i<=C.m;i++)
			for(int j=1;j<=C.n;j++)
				C[i][j]=(A[i][j]+B[i][j])%ha;
		return C;
	}
} f[N],sum[N<<2];

void Modify(int o,int l,int r,int k,int v)
{
	if(n==9426){printf("Rumtime Error! [%d, %d]  k=%d\n",l,r,k);}
	if(l==r)
	{
		size[o]=v;
		sum[o][2][1]=0;
		sum[o][1][1]=Hash[k]*v%ha;
		return;
	}
	int mid=(l+r)/2;
	if(k<=mid) Modify(o<<1,l,mid,k,v);
	else Modify(o<<1|1,mid+1,r,k,v);
	size[o]=size[o<<1]+size[o<<1|1];
	sum[o]=sum[o<<1]+f[size[o<<1]]*sum[o<<1|1];
}

void Add(int x){if(++cnt[x]==1) Modify(1,1,hs,x,1);}
void Del(int x){if(--cnt[x]==0) Modify(1,1,hs,x,0);}

int main()
{
	scanf("%d%d",&n,&ha);
	for(int i=1;i<=n;i++) scanf("%d",a+i),Hash[i]=a[i];
	sort(Hash+1,Hash+1+n);
	hs=unique(Hash+1,Hash+1+n)-(Hash+1);
	for(int i=1;i<=n;i++) a[i]=lower_bound(Hash+1,Hash+1+hs,a[i])-Hash;
	f[0].m=f[0].n=f[1].m=f[1].n=2;
	f[0][1][1]=f[0][2][2]=1;
	f[1][1][1]=f[1][2][1]=f[1][1][2]=1;
	for(int i=2;i<=hs;i++) f[i]=f[i-1]*f[1];
	int Q,pl=0,pr=0;
	scanf("%d",&Q);block=sqrt(Q);
	for(int i=1;i<=Q;i++) scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
	sort(q+1,q+1+Q);
	for(int i=1;i<=Q;i++)
	{
		if(n==9426) cout<<i<<endl;
		if(n==9426){printf("RE:: %d  %d\n",pl,pr);}
		while(pl<q[i].l) Del(a[pl++]);
		while(pl>q[i].l) Add(a[--pl]);
		while(pr>q[i].r) Del(a[pr--]);
		while(pr<q[i].r) Add(a[++pr]);
		ans[q[i].id]=sum[1][1][1];
	}
	for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=30010;
int n,ha,block,hs;
int a[N],Hash[N];
int size[N],cnt[N],ans[N];
struct Qry{int l,r,id;} q[N];

bool operator < (const Qry &a,const Qry &b){return a.l/block<b.l/block||a.l/block==b.l/block&&a.r<b.r;}

struct Matrix
{
	int a[3][3],m,n;
	Matrix(){memset(a,0,sizeof(a));m=2;n=1;}
	int* operator [] (const int &x){return a[x];}

	friend Matrix operator * (Matrix A,Matrix B)
	{
		Matrix C;C.m=A.m;C.n=B.n;
		for(int i=1;i<=A.m;i++)
			for(int j=1;j<=B.n;j++)
				for(int k=1;k<=A.n;k++)
					C[i][j]=(C[i][j]+A[i][k]*B[k][j])%ha;
		return C;
	}

	friend Matrix operator + (Matrix A,Matrix B)
	{
		Matrix C;C.m=A.m;C.n=A.n;
		for(int i=1;i<=C.m;i++)
			for(int j=1;j<=C.n;j++)
				C[i][j]=(A[i][j]+B[i][j])%ha;
		return C;
	}
} f[N],sum[N<<2];

void Modify(int o,int l,int r,int k,int v)
{
	if(o>=(N<<2)){printf("Rumtime Error! [%d, %d]  k=%d\n",l,r,k);exit(0);}
	if(l==r)
	{
		size[o]=v;
		sum[o][2][1]=0;
		sum[o][1][1]=Hash[k]*v%ha;
		return;
	}
	int mid=(l+r)/2;
	if(k<=mid) Modify(o<<1,l,mid,k,v);
	else Modify(o<<1|1,mid+1,r,k,v);
	size[o]=size[o<<1]+size[o<<1|1];
	sum[o]=sum[o<<1]+f[size[o<<1]]*sum[o<<1|1];
}

void Add(int x){if(++cnt[x]==1) Modify(1,1,hs,x,1);}
void Del(int x){if(--cnt[x]==0) Modify(1,1,hs,x,0);}

int main()
{
	scanf("%d%d",&n,&ha);
	for(int i=1;i<=n;i++) scanf("%d",a+i),Hash[i]=a[i];
	sort(Hash+1,Hash+1+n);
	hs=unique(Hash+1,Hash+1+n)-(Hash+1);
	for(int i=1;i<=n;i++) a[i]=lower_bound(Hash+1,Hash+1+hs,a[i])-Hash;
	f[0].m=f[0].n=f[1].m=f[1].n=2;
	f[0][1][1]=f[0][2][2]=1;
	f[1][1][1]=f[1][2][1]=f[1][1][2]=1;
	for(int i=2;i<=hs;i++) f[i]=f[i-1]*f[1];
	int Q,pl=0,pr=0;
	scanf("%d",&Q);block=sqrt(Q);
	for(int i=1;i<=Q;i++) scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
	sort(q+1,q+1+Q);
	for(int i=1;i<=Q;i++)
	{
		while(pl<q[i].l) Del(a[pl++]);
		while(pl>q[i].l) Add(a[--pl]);
		while(pr>q[i].r) Del(a[pr--]);
		while(pr<q[i].r) Add(a[++pr]);
		ans[q[i].id]=sum[1][1][1];
	}
	for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=30010;
int n,ha,block,hs;
int a[N],Hash[N];
int size[N],cnt[N],ans[N];
struct Qry{int l,r,id;} q[N];

bool operator < (const Qry &a,const Qry &b){return a.l/block<b.l/block||a.l/block==b.l/block&&a.r<b.r;}

struct Matrix
{
	int a[3][3],m,n;
	Matrix(){memset(a,0,sizeof(a));m=2;n=1;}
	int* operator [] (const int &x){return a[x];}

	friend Matrix operator * (Matrix A,Matrix B)
	{
		Matrix C;C.m=A.m;C.n=B.n;
		for(int i=1;i<=A.m;i++)
			for(int j=1;j<=B.n;j++)
				for(int k=1;k<=A.n;k++)
					C[i][j]=(C[i][j]+A[i][k]*B[k][j])%ha;
		return C;
	}

	friend Matrix operator + (Matrix A,Matrix B)
	{
		Matrix C;C.m=A.m;C.n=A.n;
		for(int i=1;i<=C.m;i++)
			for(int j=1;j<=C.n;j++)
				C[i][j]=(A[i][j]+B[i][j])%ha;
		return C;
	}
} f[N],sum[N<<2];

void Modify(int o,int l,int r,int k,int v)
{
	if(o>=(N<<2)){printf("Rumtime Error! [%d, %d]  k=%d\n",l,r,k);exit(0);}
	if(l==r)
	{
		size[o]=v;
		sum[o][2][1]=0;
		sum[o][1][1]=Hash[k]*v%ha;
		return;
	}
	int mid=(l+r)/2;
	if(k<=mid) Modify(o<<1,l,mid,k,v);
	else Modify(o<<1|1,mid+1,r,k,v);
	size[o]=size[o<<1]+size[o<<1|1];
	sum[o]=sum[o<<1]+f[size[o<<1]]*sum[o<<1|1];
}

void Add(int x){if(++cnt[x]==1) Modify(1,1,hs,x,1);}
void Del(int x){if(--cnt[x]==0) Modify(1,1,hs,x,0);}

int main()
{
	scanf("%d%d",&n,&ha);
	for(int i=1;i<=n;i++) scanf("%d",a+i),Hash[i]=a[i];
	sort(Hash+1,Hash+1+n);
	hs=unique(Hash+1,Hash+1+n)-(Hash+1);
	if(n==9426) printf("1%d\n",hs);
	for(int i=1;i<=n;i++) a[i]=lower_bound(Hash+1,Hash+1+hs,a[i])-Hash;
	if(n==9426) printf("2%d\n",hs);
	f[0].m=f[0].n=f[1].m=f[1].n=2;
	if(n==9426) printf("3%d\n",hs);
	f[0][1][1]=f[0][2][2]=1;
	if(n==9426) printf("4%d\n",hs);
	f[1][1][1]=f[1][2][1]=f[1][1][2]=1;
	if(n==9426) printf("5%d\n",hs);
	for(int i=2;i<=hs;i++) f[i]=f[i-1]*f[1];
	if(n==9426) printf("6%d\n",hs);
	int Q,pl=0,pr=0;
	scanf("%d",&Q);block=sqrt(Q);
	if(n==9426) printf("7%d\n",hs);
	for(int i=1;i<=Q;i++) scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
	if(n==9426) printf("8%d\n",hs);
	sort(q+1,q+1+Q);
	if(n==9426) printf("9%d\n",hs),exit(0);
	for(int i=1;i<=Q;i++)
	{
		while(pl<q[i].l) Del(a[pl++]);
		while(pl>q[i].l) Add(a[--pl]);
		while(pr>q[i].r) Del(a[pr--]);
		while(pr<q[i].r) Add(a[++pr]);
		ans[q[i].id]=sum[1][1][1];
	}
	for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=30010;
int n,ha,block,hs;
int a[N],Hash[N];
int size[N],cnt[N],ans[N];
struct Qry{int l,r,id;} q[N];

bool operator < (const Qry &a,const Qry &b){return a.l/block<b.l/block||a.l/block==b.l/block&&a.r<b.r;}

struct Matrix
{
	int a[3][3],m,n;
	Matrix(){memset(a,0,sizeof(a));m=2;n=1;}
	int* operator [] (const int &x){return a[x];}

	friend Matrix operator * (Matrix A,Matrix B)
	{
		Matrix C;C.m=A.m;C.n=B.n;
		for(int i=1;i<=A.m;i++)
			for(int j=1;j<=B.n;j++)
				for(int k=1;k<=A.n;k++)
					C[i][j]=(C[i][j]+A[i][k]*B[k][j])%ha;
		return C;
	}

	friend Matrix operator + (Matrix A,Matrix B)
	{
		Matrix C;C.m=A.m;C.n=A.n;
		for(int i=1;i<=C.m;i++)
			for(int j=1;j<=C.n;j++)
				C[i][j]=(A[i][j]+B[i][j])%ha;
		return C;
	}
} f[N],sum[N<<2];

void Modify(int o,int l,int r,int k,int v)
{
	if(l==r)
	{
		size[o]=v;
		sum[o][2][1]=0;
		sum[o][1][1]=Hash[k]*v;
		return;
	}
	int mid=(l+r)/2;
	if(k<=mid) Modify(o<<1,l,mid,k,v);
	else Modify(o<<1|1,mid+1,r,k,v);
	size[o]=size[o<<1]+size[o<<1|1];
	sum[o]=sum[o<<1]+f[size[o<<1]]*sum[o<<1|1];
}

void Add(int x){if(++cnt[x]==1) Modify(1,1,hs,x,1);}
void Del(int x){if(--cnt[x]==0) Modify(1,1,hs,x,0);}

int main()
{
	scanf("%d%d",&n,&ha);
	for(int i=1;i<=n;i++) scanf("%d",a+i),Hash[i]=a[i];
	sort(Hash+1,Hash+1+n);
	hs=unique(Hash+1,Hash+1+n)-(Hash+1);
	for(int i=1;i<=n;i++) a[i]=lower_bound(Hash+1,Hash+1+hs,a[i])-Hash;
	f[0].m=f[0].n=f[1].m=f[1].n=2;
	f[0][1][1]=f[0][2][2]=1;
	f[1][1][1]=f[1][2][1]=f[1][1][2]=1;
	for(int i=2;i<=hs;i++) f[i]=f[i-1]*f[1];
	int Q,pl=0,pr=0;
	scanf("%d",&Q);block=sqrt(Q);
	for(int i=1;i<=Q;i++) scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
	sort(q+1,q+1+Q);
	for(int i=1;i<=Q;i++)
	{
		while(pl<q[i].l) Del(a[pl++]);
		while(pl>q[i].l) Add(a[--pl]);
		while(pr>q[i].r) Del(a[pr--]);
		while(pr<q[i].r) Add(a[++pr]);
		ans[q[i].id]=sum[1][1][1];
	}
	for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
	return 0;
}
//我不是，我没有！


#include<bits/stdc++.h>
using namespace std;

const int N=30010;
int n,ha,block,hs;
int a[N],Hash[N];
int size[N<<2],cnt[N],ans[N];
struct Qry{int l,r,id;} q[N];

bool operator < (const Qry &a,const Qry &b){return a.l/block<b.l/block||a.l/block==b.l/block&&a.r<b.r;}

struct Matrix
{
	int a[2][2],m,n;
	Matrix(){memset(a,0,sizeof(a));m=2;n=1;}
	int* operator [] (const int &x){return a[x];}

	friend Matrix operator * (Matrix A,Matrix B)
	{
		Matrix C;C.m=A.m;C.n=B.n;
		for(int i=0;i<A.m;i++)
			for(int j=0;j<B.n;j++)
				for(int k=0;k<A.n;k++)
					C[i][j]=(C[i][j]+A[i][k]*B[k][j])%ha;
		return C;
	}

	friend Matrix operator + (Matrix A,Matrix B)
	{
		Matrix C;C.m=A.m;C.n=A.n;
		for(int i=0;i<C.m;i++)
			for(int j=0;j<C.n;j++)
				C[i][j]=(A[i][j]+B[i][j])%ha;
		return C;
	}
} f[N],sum[N<<2];

void Modify(int o,int l,int r,int k,int v)
{
	if(l==r)
	{
		size[o]=v;
		sum[o][1][0]=0;
		sum[o][0][0]=Hash[k]*v%ha;
		return;
	}
	int mid=(l+r)/2;
	if(k<=mid) Modify(o<<1,l,mid,k,v);
	else Modify(o<<1|1,mid+1,r,k,v);
	size[o]=size[o<<1]+size[o<<1|1];
	sum[o]=sum[o<<1]+f[size[o<<1]]*sum[o<<1|1];
}

void Add(int x){if(++cnt[x]==1) Modify(1,1,hs,x,1);}
void Del(int x){if(--cnt[x]==0) Modify(1,1,hs,x,0);}

int main()
{
	scanf("%d%d",&n,&ha);
	block=sqrt(n);
	for(int i=1;i<=n;i++) scanf("%d",a+i),Hash[i]=a[i];
	sort(Hash+1,Hash+1+n);
	hs=unique(Hash+1,Hash+1+n)-(Hash+1);
	for(int i=1;i<=n;i++) a[i]=lower_bound(Hash+1,Hash+1+hs,a[i])-Hash;
	f[0].m=f[0].n=f[1].m=f[1].n=2;
	f[0][0][0]=f[0][1][1]=1;
	f[1][0][0]=f[1][1][0]=f[1][0][1]=1;
	for(int i=2;i<=hs;i++) f[i]=f[i-1]*f[1];
	int Q,pl=0,pr=0;
	scanf("%d",&Q);
	for(int i=1;i<=Q;i++) scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
	sort(q+1,q+1+Q);
	for(int i=1;i<=Q;i++)
	{
		while(pl<q[i].l) Del(a[pl++]);
		while(pl>q[i].l) Add(a[--pl]);
		while(pr>q[i].r) Del(a[pr--]);
		while(pr<q[i].r) Add(a[++pr]);
		ans[q[i].id]=sum[1][0][0];
	}
	for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;

#define F(i, a, b) for (register int i = (a); i <= (b); i++)

const int N = 40000;

int n, m, a[N], h[N];
int f[N], forw[2][2], back[2][2];

int q, c, ans[N];
struct Query {
	int l, r, id;
	friend inline bool operator < (Query A, Query B) {
		int ownA = A.l / c, ownB = B.l / c;
		return ownA != ownB ? ownA < ownB : A.r < B.r;
	}
}qs[N];

int cnt[N];
struct Tree {
	int l, r, cnt, sum[2], tag[2][2];
}tr[70000];		//65536

inline int rd(void) {
	int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
	int x = 0; for (; isdigit(c); c = getchar()) x = x*10+c-'0'; return x*f;
}

void Build(int x, int L, int R) {
	tr[x].l = L, tr[x].r = R, tr[x].tag[0][0] = tr[x].tag[1][1] = 1;
	if (L != R) {
		int M = (L+R)>>1;
		Build(x<<1, L, M);
		Build(x<<1|1, M+1, R);
	}
}
inline int Cnt(int x, int w) {
	// 求 cnt , 无需 Clear
	if (tr[x].r < w) return tr[x].cnt;
	if (tr[x].l == tr[x].r) return 0;
	int M = (tr[x].l+tr[x].r)>>1;
	int ansL = Cnt(x<<1, w);
	return ansL + Cnt(x<<1|1, w);
}
inline void Push(int x, int tag[2][2]) {
	int f0 = tr[x].sum[0], f1 = tr[x].sum[1];
	tr[x].sum[0] = (tag[0][0] * f0 + tag[0][1] * f1) % m;
	tr[x].sum[1] = (tag[1][0] * f0 + tag[1][1] * f1) % m;

	int f[2][2]; f[0][0] = tr[x].tag[0][0], f[0][1] = tr[x].tag[0][1], f[1][0] = tr[x].tag[1][0], f[1][1] = tr[x].tag[1][1];
	tr[x].tag[0][0] = (tag[0][0] * f[0][0] + tag[0][1] * f[1][0]) % m;
	tr[x].tag[0][1] = (tag[0][0] * f[0][1] + tag[0][1] * f[1][1]) % m;
	tr[x].tag[1][0] = (tag[1][0] * f[0][0] + tag[1][1] * f[1][0]) % m;
	tr[x].tag[1][1] = (tag[1][0] * f[0][1] + tag[1][1] * f[1][1]) % m;
}
inline void Clear(int x) {
	if (tr[x].tag[0][0] == 1 && !tr[x].tag[0][1] && !tr[x].tag[1][0] && tr[x].tag[1][1] == 1) return;
	Push(x<<1, tr[x].tag);
	Push(x<<1|1, tr[x].tag);
	tr[x].tag[0][0] = tr[x].tag[1][1] = 1, tr[x].tag[0][1] = tr[x].tag[1][0] = 0;
}
inline void Modify(int x, int w, int tag[2][2]) {
	if (tr[x].l >= w) { Push(x, tag); return; }
	if (tr[x].l == tr[x].r) return;
	Clear(x);
	Modify(x<<1, w, tag);
	Modify(x<<1|1, w, tag);
	tr[x].sum[0] = tr[x<<1].sum[0] + tr[x<<1|1].sum[0];
	tr[x].sum[1] = tr[x<<1].sum[1] + tr[x<<1|1].sum[1];
	// 处理矩阵, 无需将 cnt 上传.
}
inline void Insert(int x, int w, int f0, int f1, int c) {
	if (tr[x].l == tr[x].r) { tr[x].cnt += c, tr[x].sum[0] = f0, tr[x].sum[1] = f1; return; }
	Clear(x);
	int M = (tr[x].l+tr[x].r)>>1;
	w <= M ? Insert(x<<1, w, f0, f1, c) : Insert(x<<1|1, w, f0, f1, c);
	tr[x].cnt = tr[x<<1].cnt + tr[x<<1|1].cnt;
	tr[x].sum[0] = tr[x<<1].sum[0] + tr[x<<1|1].sum[0];
	tr[x].sum[1] = tr[x<<1].sum[1] + tr[x<<1|1].sum[1];
}

// 单点只有一个值
inline void Add(int w) {
	if (++cnt[w] == 1) {
		int c = Cnt(1, w)+1;
		Insert(1, w, f[c] * h[w] % m, f[c+1] * h[w] % m, 1);
		Modify(1, w+1, back);
	}
}
inline void Erase(int w) {
	if (cnt[w]-- == 1) {
		Insert(1, w, 0, 0, -1);
		Modify(1, w+1, forw);
	}
}

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("xsy2108.in", "r", stdin);
		freopen("xsy2108.out", "w", stdout);
	#endif

	n = rd(), m = rd();
	F(i, 1, n) a[i] = h[i] = rd();
	sort(h+1, h+n+1);
	F(i, 1, n) a[i] = lower_bound(h+1, h+n+1, a[i]) - h;
	F(i, 1, n) h[i] %= m;

	f[1] = f[2] = 1; F(i, 3, n+5) f[i] = (f[i-1] + f[i-2]) % m;
	back[0][1] = back[1][0] = back[1][1] = 1;
	forw[0][0] = -1, forw[0][1] = forw[1][0] = 1;

	q = rd(), c = (int)sqrt(n);
	F(i, 1, q) {
		int l = rd(), r = rd();
		qs[i] = (Query){l, r, i};
	}
	sort(qs+1, qs+q+1);

	int ml = 1, mr = 0;
	Build(1, 1, n);
	F(i, 1, q) {
		while (mr < qs[i].r)
			Add(a[++mr]);
		while (ml > qs[i].l)
			Add(a[--ml]);
		while (mr > qs[i].r)
			Erase(a[mr--]);
		while (ml < qs[i].l)
			Erase(a[ml++]);
		ans[qs[i].id] = tr[1].sum[0];
	}
	F(i, 1, q) printf("%d\n", (ans[i] + m) % m);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define fore(v,u) for (vector<int>::iterator v=g[u].begin();v!=g[u].end();v++)
#define LL long long
#define LD long double
LL rd(void) {
	LL x=0,f=1; char c=getchar();
	for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
	for (;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
char rdc(void) {
	char c=getchar();
	while (!isalpha(c))
		c=getchar();
	return c;
}

const int N=32768;
const int Q=32768;
const int S=65536;

int n,mod,a[N];
int tot,hsh[N];
void Decrete(void) {
	F(i,1,n) hsh[++tot]=a[i];
	sort(hsh+1,hsh+tot+1);
	tot=unique(hsh+1,hsh+tot+1)-hsh-1;
	F(i,1,n) a[i]=lower_bound(hsh+1,hsh+tot+1,a[i])-hsh;
	F(i,1,tot) hsh[i]%=mod;
}

const int M=30002;
int fic[N];
int Prework(void) {
	fic[1]=fic[2]=1;
	F(i,3,M) fic[i]=(fic[i-1]+fic[i-2])%mod;
}

int q,c;
struct Ask {
	int l,r; int id;
	friend int operator < (Ask a,Ask b) {
		int ownA=a.l/c,ownB=b.l/c;
		if (ownA!=ownB) return ownA<ownB; else return a.r<b.r;
	}
}ask[Q];

struct Matrix {
	int h,w; int a[3][3];
	Matrix(int _h=0,int _w=0):h(_h),w(_w) {
		memset(a,0,sizeof a);
	}
	friend Matrix operator * (Matrix ma,Matrix mb) {
		Matrix mc(ma.h,mb.w); int s=ma.w;
		F(i,1,mc.h) F(j,1,mc.w) F(k,1,s)
			(mc.a[i][j]+=(LL)ma.a[i][k]*mb.a[k][j]%mod)%=mod;
		return mc;
	}
	friend Matrix operator + (Matrix ma,Matrix mb) {
		Matrix mc(ma.h,ma.w);
		F(i,1,mc.h) F(j,1,mc.w)
			mc.a[i][j]=(ma.a[i][j]+mb.a[i][j])%mod;
		return mc;
	}
}mv_back,mv_forw,e;
void Init(void) {
	mv_back=Matrix(2,2); mv_back.a[1][2]=mv_back.a[2][1]=mv_back.a[2][2]=1;
	mv_forw=Matrix(2,2); mv_forw.a[1][1]=-1,mv_forw.a[1][2]=mv_forw.a[2][1]=1;
	e=Matrix(2,2); e.a[1][1]=e.a[2][2]=1;
}

Matrix add[S],sum[S]; int cnt[S];
void Build(int x,int l,int r) {
	add[x]=e; sum[x]=Matrix(1,2);
	if (l!=r) Build(x<<1,l,(l+r)>>1),Build(x<<1|1,(l+r+2)>>1,r);
}
void Update(int x) {
	cnt[x]=cnt[x<<1]+cnt[x<<1|1];
	sum[x]=sum[x<<1]+sum[x<<1|1];
}
void Addit(int x,Matrix ad) {
	sum[x]=sum[x]*ad;
	add[x]=add[x]*ad;
}
void Clear(int x) {
	Addit(x<<1,add[x]);
	Addit(x<<1|1,add[x]);
	add[x]=e;
}
int LS(int x,int l,int r,int loc) {
	if (r<=loc) return cnt[x];
	int mid=(l+r)>>1;
	int cnt_all=LS(x<<1,l,mid,loc);
	if (mid+1<=loc) cnt_all+=LS(x<<1|1,mid+1,r,loc);
	return cnt_all;
}
void Modify(int x,int l,int r,int loc,LL v1,LL v2,int kd) {
	if (l==r) {
		cnt[x]+=kd;
		sum[x].a[1][1]=v1;
		sum[x].a[1][2]=v2;
		return;
	}
	Clear(x);
	int mid=(l+r)>>1;
	if (loc<=mid)
		Modify(x<<1,l,mid,loc,v1,v2,kd);
	else Modify(x<<1|1,mid+1,r,loc,v1,v2,kd);
	Update(x);
}
void Interval_Add(int x,int l,int r,int ql,int qr,Matrix ad) {
	if (ql<=l&&r<=qr) {
		Addit(x,ad);
		return;
	}
	Clear(x);
	int mid=(l+r)>>1;
	if (ql<=mid)
		Interval_Add(x<<1,l,mid,ql,qr,ad);
	if (mid+1<=qr)
		Interval_Add(x<<1|1,mid+1,r,ql,qr,ad);
	Update(x);
}
int now_ans(void) {
	int t=sum[1].a[1][1];
	(t+=mod)%=mod;
	return t;
}

int ml,mr;
int ans[Q];
int vis[N];	//每个数出现的次数
void Modify(int x,int kd) {
	int val=a[x]; int real_val=hsh[val];
	if (vis[val]>0) {
		Modify(1,1,tot,val,0,0,-1);
		if (val+1<=tot)
			Interval_Add(1,1,tot,val+1,tot,mv_forw);
	}
	vis[val]+=kd;
	if (vis[val]>0) {
		int cntL=(val==1?0:LS(1,1,tot,val-1));
		Modify(1,1,tot,val,(LL)real_val*fic[cntL+1]%mod,(LL)real_val*fic[cntL+2]%mod,1);
		if (val+1<=tot)
			Interval_Add(1,1,tot,val+1,tot,mv_back);
	}
}

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("sdchr.in","r",stdin);
		freopen("sdchr.out","w",stdout);
	#endif

	n=rd(),mod=rd(); F(i,1,n) a[i]=rd();
	Decrete();

	Prework();

	q=rd(); c=(int)sqrt(q);
	F(c,1,q) {
		int l=rd(),r=rd();
		ask[c].l=l,ask[c].r=r,ask[c].id=c;
	}
	sort(ask+1,ask+q+1);

	Init();
	Build(1,1,tot);
	ml=1,mr=0;
	F(c,1,q) {
		while (mr<ask[c].r)
			Modify(++mr,1);
		while (ml>ask[c].l)
			Modify(--ml,1);
		while (mr>ask[c].r)
			Modify(mr--,-1);
		while (ml<ask[c].l)
			Modify(ml++,-1);
		ans[ask[c].id]=now_ans();
	}
	F(i,1,q) printf("%d\n",ans[i]);

	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;

#define F(i, a, b) for (register int i = (a); i <= (b); i++)

const int N = 35000;
const int S = 70000;	//65536

int n, m, a[N], h[N];

int f[N];
struct Matrix {
	int val[2][2];
	inline Matrix(void) { memset(val, 0, sizeof val); }
	friend inline Matrix operator * (Matrix A, Matrix B) {
		Matrix C; F(i, 0, 1) F(j, 0, 1) F(k, 0, 1) (C.val[i][j] += A.val[i][k] * B.val[k][j]) %= m; return C;
	}
}back[N];

int q, c, ans[N];
struct Query {
	int l, r, id;
	friend bool inline operator < (Query A, Query B) {
		int ownA = A.l / c, ownB = B.l / c;
		return ownA != ownB ? ownA < ownB : A.r < B.r;
	}
}qs[N];

int cnt[N];
int D, siz[S], sum[S][2];

namespace Input {
	const int S = 2000000;
	char s[S], *h = s+S, *t = h;
	inline char getchr(void) { if (h == t) fread(s, 1, S, stdin), h = s; return *h++; }
	inline int rd(void) {
		int f = 1; char c = getchr(); for (; !isdigit(c); c = getchr()) if (c == '-') f = -1;
		int x = 0; for (; isdigit(c); c = getchr()) x = x*10+c-'0'; return x*f;
	}
}
using Input::rd;

namespace Output {
    const int S = 3000000;
    char s[S], *t = s;
    inline void putchr(char c) { *t++ = c; }
    inline void put(LL x) {
        if (x == 0) { putchr('0'); return; }
        if (x < 0) putchr('-'), x = -x;
        static int ret[64]; int n = 0;
        for (; x > 0; x /= 10) ret[++n] = x%10;
        while (n > 0) putchr(ret[n--] + '0');
    }
    inline void flush(void) { fwrite(s, 1, t-s, stdout); }
}
using Output::putchr;
using Output::put;

inline void Modify(int x, int c) {
	int fac = h[x]; x += (1<<D), siz[x] = c, sum[x][0] = sum[x][1] = c * fac;
	for (x >>= 1; x > 0; x >>= 1) {
		int S = siz[x<<1];
		siz[x] = S + siz[x<<1|1];
		sum[x][0] = (sum[x<<1][0] + back[S].val[0][0] * sum[x<<1|1][0] + back[S].val[0][1] * sum[x<<1|1][1]) % m;
		sum[x][1] = (sum[x<<1][1] + back[S].val[1][0] * sum[x<<1|1][0] + back[S].val[1][1] * sum[x<<1|1][1]) % m;
	}
}
inline void Add(int w) { if (++cnt[w] == 1) Modify(w, 1); }
inline void Erase(int w) { if (cnt[w]-- == 1) Modify(w, 0); }

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("xsy2108.in", "r", stdin);
		freopen("xsy2108.out", "w", stdout);
	#endif

	n = rd(), m = rd();
	F(i, 1, n) a[i] = h[i] = rd();
	sort(h+1, h+n+1);
	F(i, 1, n) a[i] = lower_bound(h+1, h+n+1, a[i]) - h;
	F(i, 1, n) h[i] %= m;

	f[1] = f[2] = 1;
	F(i, 3, n+5) f[i] = (f[i-1] + f[i-2]) % m;

	back[0].val[0][0] = back[0].val[1][1] = 1;
	back[1].val[0][1] = back[1].val[1][0] = back[1].val[1][1] = 1;
	F(i, 2, n) back[i] = back[i-1] * back[1];

	q = rd(), c = (int)sqrt(n);
	F(i, 1, q) {
		int l = rd(), r = rd();
		qs[i] = (Query){l, r, i};
	}
	sort(qs+1, qs+q+1);

	int ml = 1, mr = 0;
	for (D = 0; (1 << D) - 1 < n; D++);		// 无需用到开区间, 不用 +1
	F(i, 1, q) {
		while (mr < qs[i].r)
			Add(a[++mr]);
		while (ml > qs[i].l)
			Add(a[--ml]);
		while (mr > qs[i].r)
			Erase(a[mr--]);
		while (ml < qs[i].l)
			Erase(a[ml++]);
		ans[qs[i].id] = sum[1][0];
	}
	F(i, 1, q) printf("%d\n", ans[i]);

	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;

#define F(i, a, b) for (register int i = (a); i <= (b); i++)

const int N = 40000;

int n, m, a[N], h[N], tot;
int f[N], forw[2][2], back[2][2];

int q, c, ans[N];
struct Query {
	int l, r, id;
	friend inline bool operator < (Query A, Query B) {
		int ownA = A.l / c, ownB = B.l / c;
		return ownA != ownB ? ownA < ownB : A.r < B.r;
	}
}qs[N];

int cnt[N];
struct Tree {
	int l, r, cnt, sum[2], tag[2][2];
}tr[70000];		//65536

inline int rd(void) {
	int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
	int x = 0; for (; isdigit(c); c = getchar()) x = x*10+c-'0'; return x*f;
}

void Build(int x, int L, int R) {
	tr[x].l = L, tr[x].r = R, tr[x].tag[0][0] = tr[x].tag[1][1] = 1;
	if (L != R) {
		int M = (L+R)>>1;
		Build(x<<1, L, M);
		Build(x<<1|1, M+1, R);
	}
}
inline int Cnt(int x, int w) {
	// 求 cnt , 无需 Clear
	if (tr[x].r < w) return tr[x].cnt;
	if (tr[x].l == tr[x].r) return 0;
	int ansL = Cnt(x<<1, w);
	return ansL + Cnt(x<<1|1, w);
}
inline void Push(int x, int tag[2][2]) {
	int f0 = tr[x].sum[0], f1 = tr[x].sum[1];
	tr[x].sum[0] = (tag[0][0] * f0 + tag[0][1] * f1) % m;
	tr[x].sum[1] = (tag[1][0] * f0 + tag[1][1] * f1) % m;

	int f[2][2]; f[0][0] = tr[x].tag[0][0], f[0][1] = tr[x].tag[0][1], f[1][0] = tr[x].tag[1][0], f[1][1] = tr[x].tag[1][1];
	tr[x].tag[0][0] = (tag[0][0] * f[0][0] + tag[0][1] * f[1][0]) % m;
	tr[x].tag[0][1] = (tag[0][0] * f[0][1] + tag[0][1] * f[1][1]) % m;
	tr[x].tag[1][0] = (tag[1][0] * f[0][0] + tag[1][1] * f[1][0]) % m;
	tr[x].tag[1][1] = (tag[1][0] * f[0][1] + tag[1][1] * f[1][1]) % m;
}
inline void Clear(int x) {
	if (tr[x].tag[0][0] == 1 && !tr[x].tag[0][1] && !tr[x].tag[1][0] && tr[x].tag[1][1] == 1) return;
	Push(x<<1, tr[x].tag);
	Push(x<<1|1, tr[x].tag);
	tr[x].tag[0][0] = tr[x].tag[1][1] = 1, tr[x].tag[0][1] = tr[x].tag[1][0] = 0;
}
inline void Modify(int x, int w, int tag[2][2]) {
	if (tr[x].l >= w) { Push(x, tag); return; }
	if (tr[x].l == tr[x].r) return;
	Clear(x);
	Modify(x<<1, w, tag);
	Modify(x<<1|1, w, tag);
	tr[x].sum[0] = (tr[x<<1].sum[0] + tr[x<<1|1].sum[0]) % m;
	tr[x].sum[1] = (tr[x<<1].sum[1] + tr[x<<1|1].sum[1]) % m;
	// 处理矩阵, 无需将 cnt 上传.
}
inline void Insert(int x, int w, int f0, int f1, int c) {
	if (tr[x].l == tr[x].r) { tr[x].cnt += c, tr[x].sum[0] = f0, tr[x].sum[1] = f1; return; }
	Clear(x);
	int M = (tr[x].l+tr[x].r)>>1;
	w <= M ? Insert(x<<1, w, f0, f1, c) : Insert(x<<1|1, w, f0, f1, c);
	tr[x].cnt = tr[x<<1].cnt + tr[x<<1|1].cnt;
	tr[x].sum[0] = (tr[x<<1].sum[0] + tr[x<<1|1].sum[0]) % m;
	tr[x].sum[1] = (tr[x<<1].sum[1] + tr[x<<1|1].sum[1]) % m;
}

// 单点只有一个值
inline void Add(int w) {
	if (++cnt[w] == 1) {
		int c = Cnt(1, w)+1;		// 比 w 小的个数 + 1
		Insert(1, w, f[c] * h[w] % m, f[c+1] * h[w] % m, 1);
		Modify(1, w+1, back);
	}
}
inline void Erase(int w) {
	if (cnt[w]-- == 1) {
		Insert(1, w, 0, 0, -1);
		Modify(1, w+1, forw);
	}
}

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("xsy2108.in", "r", stdin);
		freopen("xsy2108.out", "w", stdout);
	#endif

	n = rd(), m = rd();
	F(i, 1, n) a[i] = h[i] = rd();
	sort(h+1, h+n+1);
	tot = unique(h+1, h+n+1) - h - 1;
	F(i, 1, n) a[i] = lower_bound(h+1, h+tot+1, a[i]) - h;
	F(i, 1, tot) h[i] %= m;

	f[1] = f[2] = 1; F(i, 3, n+5) f[i] = (f[i-1] + f[i-2]) % m;
	back[0][1] = back[1][0] = back[1][1] = 1;
	forw[0][0] = -1, forw[0][1] = forw[1][0] = 1;

	q = rd();
	c = (int)sqrt(n);
	F(i, 1, q) {
		int l = rd(), r = rd();
		qs[i] = (Query){l, r, i};
	}
	sort(qs+1, qs+q+1);

	int ml = 1, mr = 0;
	Build(1, 1, tot);
	F(i, 1, q) {
		while (mr < qs[i].r)
			Add(a[++mr]);
		while (ml > qs[i].l)
			Add(a[--ml]);
		while (mr > qs[i].r)
			Erase(a[mr--]);
		while (ml < qs[i].l)
			Erase(a[ml++]);
		ans[qs[i].id] = tr[1].sum[0];
	}
	F(i, 1, q) printf("%d\n", (ans[i] + m) % m);

	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;

#define F(i, a, b) for (register int i = (a); i <= (b); i++)

const int N = 40000;

int n, m, a[N], h[N];
int f[N], forw[2][2], back[2][2];

int q, c, ans[N];
struct Query {
	int l, r, id;
	friend inline bool operator < (Query A, Query B) {
		int ownA = A.l / c, ownB = B.l / c;
		return ownA != ownB ? ownA < ownB : A.r < B.r;
	}
}qs[N];

int cnt[N];
struct Tree {
	int l, r, cnt, sum[2], tag[2][2];
}tr[70000];		//65536

inline int rd(void) {
	int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
	int x = 0; for (; isdigit(c); c = getchar()) x = x*10+c-'0'; return x*f;
}

void Build(int x, int L, int R) {
	tr[x].l = L, tr[x].r = R, tr[x].tag[0][0] = tr[x].tag[1][1] = 1;
	if (L != R) {
		int M = (L+R)>>1;
		Build(x<<1, L, M);
		Build(x<<1|1, M+1, R);
	}
}
inline int Cnt(int x, int w) {
	// 求 cnt , 无需 Clear
	if (tr[x].r < w) return tr[x].cnt;
	if (tr[x].l == tr[x].r) return 0;
	int ansL = Cnt(x<<1, w);
	return ansL + Cnt(x<<1|1, w);
}
inline void Push(int x, int tag[2][2]) {
	int f0 = tr[x].sum[0], f1 = tr[x].sum[1];
	tr[x].sum[0] = (tag[0][0] * f0 + tag[0][1] * f1) % m;
	tr[x].sum[1] = (tag[1][0] * f0 + tag[1][1] * f1) % m;

	int f[2][2]; f[0][0] = tr[x].tag[0][0], f[0][1] = tr[x].tag[0][1], f[1][0] = tr[x].tag[1][0], f[1][1] = tr[x].tag[1][1];
	tr[x].tag[0][0] = (tag[0][0] * f[0][0] + tag[0][1] * f[1][0]) % m;
	tr[x].tag[0][1] = (tag[0][0] * f[0][1] + tag[0][1] * f[1][1]) % m;
	tr[x].tag[1][0] = (tag[1][0] * f[0][0] + tag[1][1] * f[1][0]) % m;
	tr[x].tag[1][1] = (tag[1][0] * f[0][1] + tag[1][1] * f[1][1]) % m;
}
inline void Clear(int x) {
	if (tr[x].tag[0][0] == 1 && !tr[x].tag[0][1] && !tr[x].tag[1][0] && tr[x].tag[1][1] == 1) return;
	Push(x<<1, tr[x].tag);
	Push(x<<1|1, tr[x].tag);
	tr[x].tag[0][0] = tr[x].tag[1][1] = 1, tr[x].tag[0][1] = tr[x].tag[1][0] = 0;
}
inline void Modify(int x, int w, int tag[2][2]) {
	if (tr[x].l >= w) { Push(x, tag); return; }
	if (tr[x].l == tr[x].r) return;
	Clear(x);
	Modify(x<<1, w, tag);
	Modify(x<<1|1, w, tag);
	tr[x].sum[0] = (tr[x<<1].sum[0] + tr[x<<1|1].sum[0]) % m;
	tr[x].sum[1] = (tr[x<<1].sum[1] + tr[x<<1|1].sum[1]) % m;
	// 处理矩阵, 无需将 cnt 上传.
}
inline void Insert(int x, int w, int f0, int f1, int c) {
	if (tr[x].l == tr[x].r) { tr[x].cnt += c, tr[x].sum[0] = f0, tr[x].sum[1] = f1; return; }
	Clear(x);
	int M = (tr[x].l+tr[x].r)>>1;
	w <= M ? Insert(x<<1, w, f0, f1, c) : Insert(x<<1|1, w, f0, f1, c);
	tr[x].cnt = tr[x<<1].cnt + tr[x<<1|1].cnt;
	tr[x].sum[0] = (tr[x<<1].sum[0] + tr[x<<1|1].sum[0]) % m;
	tr[x].sum[1] = (tr[x<<1].sum[1] + tr[x<<1|1].sum[1]) % m;
}

// 单点只有一个值
inline void Add(int w) {
	if (++cnt[w] == 1) {
		int c = Cnt(1, w)+1;		// 比 w 小的个数 + 1
		Insert(1, w, f[c] * h[w] % m, f[c+1] * h[w] % m, 1);
		Modify(1, w+1, back);
	}
}
inline void Erase(int w) {
	if (cnt[w]-- == 1) {
		Insert(1, w, 0, 0, -1);
		Modify(1, w+1, forw);
	}
}

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("xsy2108.in", "r", stdin);
		freopen("xsy2108.out", "w", stdout);
	#endif

	n = rd(), m = rd();
	F(i, 1, n) a[i] = h[i] = rd();
	sort(h+1, h+n+1);
	F(i, 1, n) a[i] = lower_bound(h+1, h+n+1, a[i]) - h;
	F(i, 1, n) h[i] %= m;

	f[1] = f[2] = 1; F(i, 3, n+5) f[i] = (f[i-1] + f[i-2]) % m;
	back[0][1] = back[1][0] = back[1][1] = 1;
	forw[0][0] = -1, forw[0][1] = forw[1][0] = 1;

	q = rd(), c = (int)sqrt(n);
	F(i, 1, q) {
		int l = rd(), r = rd();
		qs[i] = (Query){l, r, i};
	}
	sort(qs+1, qs+q+1);

	int ml = 1, mr = 0;
	Build(1, 1, n);
	F(i, 1, q) {
		while (mr < qs[i].r)
			Add(a[++mr]);
		while (ml > qs[i].l)
			Add(a[--ml]);
		while (mr > qs[i].r)
			Erase(a[mr--]);
		while (ml < qs[i].l)
			Erase(a[ml++]);
		ans[qs[i].id] = tr[1].sum[0];
	}
	F(i, 1, q) printf("%d\n", (ans[i] + m) % m);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define fore(v,u) for (vector<int>::iterator v=g[u].begin();v!=g[u].end();v++)
#define LL long long
#define LD long double
LL rd(void) {
	LL x=0,f=1; char c=getchar();
	for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
	for (;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
char rdc(void) {
	char c=getchar();
	while (!isalpha(c))
		c=getchar();
	return c;
}

const int N=32768;
const int Q=32768;
const int S=65536;

int n,mod,a[N];
int tot,hsh[N];
void Decrete(void) {
	F(i,1,n) hsh[++tot]=a[i];
	sort(hsh+1,hsh+tot+1);
	tot=unique(hsh+1,hsh+tot+1)-hsh-1;
	F(i,1,n) a[i]=lower_bound(hsh+1,hsh+tot+1,a[i])-hsh;
	F(i,1,tot) hsh[i]%=mod;
}

const int M=30002;
int fic[N];
int Prework(void) {
	fic[1]=fic[2]=1; F(i,3,M) fic[i]=(fic[i-1]+fic[i-2])%mod;
}

int q,c;
struct Ask {
	int l,r; int id;
	friend int operator < (Ask a,Ask b) {
		int ownA=a.l/c,ownB=b.l/c;
		if (ownA!=ownB) return ownA<ownB; else return a.r<b.r;
	}
}ask[Q];

struct Matrix {
	int h,w; int a[2][2];
	Matrix(int _h=0,int _w=0):h(_h),w(_w) {
		memset(a,0,sizeof a);
	}
	friend Matrix operator * (Matrix &ma,Matrix &mb) {
		Matrix mc(ma.h,mb.w); int s=ma.w;
		F(i,0,mc.h-1) F(j,0,mc.w-1) F(k,0,s-1)
			(mc.a[i][j]+=(LL)ma.a[i][k]*mb.a[k][j]%mod)%=mod;
		return mc;
	}
	friend Matrix operator + (Matrix &ma,Matrix &mb) {
		Matrix mc(ma.h,ma.w);
		F(i,0,mc.h-1) F(j,0,mc.w-1)
			mc.a[i][j]=(ma.a[i][j]+mb.a[i][j])%mod;
		return mc;
	}
	friend int operator == (Matrix &ma,Matrix &mb) {
		if (ma.h!=mb.h||ma.w!=mb.w) return 0;
		F(i,0,ma.h-1) F(j,0,ma.w-1) if (ma.a[i][j]!=mb.a[i][j]) return 0;
		return 1;
	}
}mv_back,mv_forw,e;
void Init(void) {
	mv_back=Matrix(2,2); mv_back.a[0][1]=mv_back.a[1][0]=mv_back.a[1][1]=1;
	mv_forw=Matrix(2,2); mv_forw.a[0][0]=-1,mv_forw.a[0][1]=mv_forw.a[1][0]=1;
	e=Matrix(2,2); e.a[0][0]=e.a[1][1]=1;
}

Matrix add[S],sum[S]; int cnt[S];
void Build(int x,int l,int r) {
	add[x]=e; sum[x]=Matrix(1,2);
	if (l!=r) Build(x<<1,l,(l+r)>>1),Build(x<<1|1,(l+r+2)>>1,r);
}
void Update(int x) {
	cnt[x]=cnt[x<<1]+cnt[x<<1|1];
	sum[x]=sum[x<<1]+sum[x<<1|1];
}
void Addit(int x,Matrix &ad) {
	sum[x]=sum[x]*ad;
	add[x]=add[x]*ad;
}
void Clear(int x) {
	if (add[x]==e) return;
	Addit(x<<1,add[x]);
	Addit(x<<1|1,add[x]);
	add[x]=e;
}
int LS(int x,int l,int r,int loc) {
	if (r<=loc) return cnt[x];
	int mid=(l+r)>>1;
	int cnt_all=LS(x<<1,l,mid,loc);
	if (mid+1<=loc) cnt_all+=LS(x<<1|1,mid+1,r,loc);
	return cnt_all;
}
void Modify(int x,int l,int r,int loc,LL v1,LL v2,int kd) {
	if (l==r) {
		cnt[x]+=kd;
		sum[x].a[0][0]=v1;
		sum[x].a[0][1]=v2;
		return;
	}
	Clear(x);
	int mid=(l+r)>>1;
	if (loc<=mid)
		Modify(x<<1,l,mid,loc,v1,v2,kd);
	else Modify(x<<1|1,mid+1,r,loc,v1,v2,kd);
	Update(x);
}
void Interval_Add(int x,int l,int r,int ql,int qr,Matrix &ad) {
	if (ql<=l&&r<=qr) {
		Addit(x,ad);
		return;
	}
	Clear(x);
	int mid=(l+r)>>1;
	if (ql<=mid)
		Interval_Add(x<<1,l,mid,ql,qr,ad);
	if (mid+1<=qr)
		Interval_Add(x<<1|1,mid+1,r,ql,qr,ad);
	Update(x);
}
int now_ans(void) {
	int t=sum[1].a[0][0];
	(t+=mod)%=mod;
	return t;
}

int ml,mr;
int ans[Q];
int vis[N];	//每个数出现的次数
void Modify(int x,int kd) {
	int val=a[x]; int real_val=hsh[val];
	if (vis[val]==1&&kd==-1)
	{
		Modify(1,1,tot,val,0,0,-1);
		if (val+1<=tot)
			Interval_Add(1,1,tot,val+1,tot,mv_forw);
	}
	else if (vis[val]==0&&kd==1) {
		int cntL=(val==1?0:LS(1,1,tot,val-1));
		Modify(1,1,tot,val,(LL)real_val*fic[cntL+1]%mod,(LL)real_val*fic[cntL+2]%mod,1);
		if (val+1<=tot)
			Interval_Add(1,1,tot,val+1,tot,mv_back);
	}
	vis[val]+=kd;
}

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("sdchr.in","r",stdin);
		freopen("sdchr.out","w",stdout);
	#endif

	n=rd(),mod=rd(); F(i,1,n) a[i]=rd();
	Decrete();

	Prework();

	q=rd(); c=(int)sqrt(q);
	F(c,1,q) {
		int l=rd(),r=rd();
		ask[c].l=l,ask[c].r=r,ask[c].id=c;
	}
	sort(ask+1,ask+q+1);

	Init();
	Build(1,1,tot);
	ml=1,mr=0;
	F(c,1,q) {
		while (mr<ask[c].r)
			Modify(++mr,1);
		while (ml>ask[c].l)
			Modify(--ml,1);
		while (mr>ask[c].r)
			Modify(mr--,-1);
		while (ml<ask[c].l)
			Modify(ml++,-1);
		ans[ask[c].id]=now_ans();
	}
	F(i,1,q) printf("%d\n",ans[i]);

	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;

#define F(i, a, b) for (register int i = (a); i <= (b); i++)

const int N = 35000;
const int S = 70000;	//65536

int n, m, a[N], h[N];

int f[N];
struct Matrix {
	int val[2][2];
	inline Matrix(void) { memset(val, 0, sizeof val); }
	friend inline Matrix operator * (Matrix A, Matrix B) {
		Matrix C; F(i, 0, 1) F(j, 0, 1) F(k, 0, 1) (C.val[i][j] += A.val[i][k] * B.val[k][j]) %= m; return C;
	}
}back[N];

int q, c, ans[N];
struct Query {
	int l, r, id;
	friend bool inline operator < (Query A, Query B) {
		int ownA = A.l / c, ownB = B.l / c;
		return ownA != ownB ? ownA < ownB : A.r < B.r;
	}
}qs[N];

int cnt[N];
int D, siz[S], sum[S][2];

inline int rd(void) {
	int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
	int x = 0; for (; isdigit(c); c = getchar()) x = x*10+c-'0'; return x*f;
}

inline void Modify(int x, int c) {
	int fac = h[x]; x += (1<<D), siz[x] = c, sum[x][0] = sum[x][1] = c * fac;
	for (x >>= 1; x > 0; x >>= 1) {
		int S = siz[x<<1];
		siz[x] = S + siz[x<<1|1];
		sum[x][0] = (sum[x<<1][0] + back[S].val[0][0] * sum[x<<1|1][0] + back[S].val[0][1] * sum[x<<1|1][1]) % m;
		sum[x][1] = (sum[x<<1][1] + back[S].val[1][0] * sum[x<<1|1][0] + back[S].val[1][1] * sum[x<<1|1][1]) % m;
	}
}
inline void Add(int w) { if (++cnt[w] == 1) Modify(w, 1); }
inline void Erase(int w) { if (cnt[w]-- == 1) Modify(w, 0); }

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("xsy2108.in", "r", stdin);
		freopen("xsy2108.out", "w", stdout);
	#endif

	n = rd(), m = rd();
	F(i, 1, n) a[i] = h[i] = rd();
	sort(h+1, h+n+1);
	F(i, 1, n) a[i] = lower_bound(h+1, h+n+1, a[i]) - h;
	F(i, 1, n) h[i] %= m;

	f[1] = f[2] = 1;
	F(i, 3, n+5) f[i] = (f[i-1] + f[i-2]) % m;

	back[0].val[0][0] = back[0].val[1][1] = 1;
	back[1].val[0][1] = back[1].val[1][0] = back[1].val[1][1] = 1;
	F(i, 2, n) back[i] = back[i-1] * back[1];

	q = rd(), c = (int)sqrt(n);
	F(i, 1, q) {
		int l = rd(), r = rd();
		qs[i] = (Query){l, r, i};
	}
	sort(qs+1, qs+q+1);

	int ml = 1, mr = 0;
	for (D = 0; (1 << D) - 1 < n; D++);		// 无需用到开区间, 不用 +1
	F(i, 1, q) {
		while (mr < qs[i].r)
			Add(a[++mr]);
		while (ml > qs[i].l)
			Add(a[--ml]);
		while (mr > qs[i].r)
			Erase(a[mr--]);
		while (ml < qs[i].l)
			Erase(a[ml++]);
		ans[qs[i].id] = sum[1][0];
	}
	F(i, 1, q) printf("%d\n", ans[i]);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define fore(v,u) for (vector<int>::iterator v=g[u].begin();v!=g[u].end();v++)
#define LL long long
#define LD long double
LL rd(void) {
	LL x=0,f=1; char c=getchar();
	for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
	for (;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
char rdc(void) {
	char c=getchar();
	while (!isalpha(c))
		c=getchar();
	return c;
}

const int N=32768;
const int Q=32768;
const int S=65536;

int n,mod,a[N];
int tot,hsh[N];
void Decrete(void) {
	F(i,1,n) hsh[++tot]=a[i];
	sort(hsh+1,hsh+tot+1);
	tot=unique(hsh+1,hsh+tot+1)-hsh-1;
	F(i,1,n) a[i]=lower_bound(hsh+1,hsh+tot+1,a[i])-hsh;
	F(i,1,tot) hsh[i]%=mod;
}

const int M=30002;
int fic[N];
int Prework(void) {
	fic[1]=fic[2]=1;
	F(i,3,M) fic[i]=(fic[i-1]+fic[i-2])%mod;
}

int q,c;
struct Ask {
	int l,r; int id;
	friend int operator < (Ask a,Ask b) {
		int ownA=a.l/c,ownB=b.l/c;
		if (ownA!=ownB) return ownA<ownB; else return a.r<b.r;
	}
}ask[Q];

struct Matrix {
	int h,w; int a[2][2];
	Matrix(int _h=0,int _w=0):h(_h),w(_w) {
		memset(a,0,sizeof a);
	}
	friend Matrix operator * (Matrix &ma,Matrix &mb) {
		Matrix mc(ma.h,mb.w); int s=ma.w;
		F(i,0,mc.h-1) F(j,0,mc.w-1) F(k,0,s-1)
			(mc.a[i][j]+=(LL)ma.a[i][k]*mb.a[k][j]%mod)%=mod;
		return mc;
	}
	friend Matrix operator + (Matrix &ma,Matrix &mb) {
		Matrix mc(ma.h,ma.w);
		F(i,0,mc.h-1) F(j,0,mc.w-1)
			mc.a[i][j]=(ma.a[i][j]+mb.a[i][j])%mod;
		return mc;
	}
	friend int operator == (Matrix &ma,Matrix &mb) {
		if (ma.h!=mb.h||ma.w!=mb.w) return 0;
		F(i,0,ma.h-1) F(j,0,ma.w-1) if (ma.a[i][j]!=mb.a[i][j]) return 0;
		return 1;
	}
}mv_back,mv_forw,e;
void Init(void) {
	mv_back=Matrix(2,2); mv_back.a[0][1]=mv_back.a[1][0]=mv_back.a[1][1]=1;
	mv_forw=Matrix(2,2); mv_forw.a[0][0]=-1,mv_forw.a[0][1]=mv_forw.a[1][0]=1;
	e=Matrix(2,2); e.a[0][0]=e.a[1][1]=1;
}

Matrix add[S],sum[S]; int cnt[S];
void Build(int x,int l,int r) {
	add[x]=e; sum[x]=Matrix(1,2);
	if (l!=r) Build(x<<1,l,(l+r)>>1),Build(x<<1|1,(l+r+2)>>1,r);
}
void Update(int x) {
	cnt[x]=cnt[x<<1]+cnt[x<<1|1];
	sum[x]=sum[x<<1]+sum[x<<1|1];
}
void Addit(int x,Matrix &ad) {
	sum[x]=sum[x]*ad;
	add[x]=add[x]*ad;
}
void Clear(int x) {
	if (add[x]==e) return;
	Addit(x<<1,add[x]);
	Addit(x<<1|1,add[x]);
	add[x]=e;
}
int LS(int x,int l,int r,int loc) {
	if (r<=loc) return cnt[x];
	int mid=(l+r)>>1;
	int cnt_all=LS(x<<1,l,mid,loc);
	if (mid+1<=loc) cnt_all+=LS(x<<1|1,mid+1,r,loc);
	return cnt_all;
}
void Modify(int x,int l,int r,int loc,LL v1,LL v2,int kd) {
	if (l==r) {
		cnt[x]+=kd;
		sum[x].a[0][0]=v1;
		sum[x].a[0][1]=v2;
		return;
	}
	Clear(x);
	int mid=(l+r)>>1;
	if (loc<=mid)
		Modify(x<<1,l,mid,loc,v1,v2,kd);
	else Modify(x<<1|1,mid+1,r,loc,v1,v2,kd);
	Update(x);
}
void Interval_Add(int x,int l,int r,int ql,int qr,Matrix &ad) {
	if (ql<=l&&r<=qr) {
		Addit(x,ad);
		return;
	}
	Clear(x);
	int mid=(l+r)>>1;
	if (ql<=mid)
		Interval_Add(x<<1,l,mid,ql,qr,ad);
	if (mid+1<=qr)
		Interval_Add(x<<1|1,mid+1,r,ql,qr,ad);
	Update(x);
}
int now_ans(void) {
	int t=sum[1].a[0][0];
	(t+=mod)%=mod;
	return t;
}

int ml,mr;
int ans[Q];
int vis[N];	//每个数出现的次数
void Modify(int x,int kd) {
	int val=a[x]; int real_val=hsh[val];
	if (vis[val]==0&&kd==1) {
		int cntL=(val==1?0:LS(1,1,tot,val-1));
		Modify(1,1,tot,val,(LL)real_val*fic[cntL+1]%mod,(LL)real_val*fic[cntL+2]%mod,1);
		if (val+1<=tot)
			Interval_Add(1,1,tot,val+1,tot,mv_back);
	}
	else if (vis[val]==1&&kd==0) {
		Modify(1,1,tot,val,0,0,-1);
		if (val+1<=tot)
			Interval_Add(1,1,tot,val+1,tot,mv_forw);
	}
	vis[val]+=kd;
}

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("sdchr.in","r",stdin);
		freopen("sdchr.out","w",stdout);
	#endif

	n=rd(),mod=rd(); F(i,1,n) a[i]=rd();
	Decrete();

	Prework();

	q=rd(); c=(int)sqrt(n);
	F(c,1,q) {
		int l=rd(),r=rd();
		ask[c].l=l,ask[c].r=r,ask[c].id=c;
	}
	sort(ask+1,ask+q+1);

	Init();
	Build(1,1,tot);
	ml=1,mr=0;
	F(c,1,q) {
		while (mr<ask[c].r)
			Modify(++mr,1);
		while (ml>ask[c].l)
			Modify(--ml,1);
		while (mr>ask[c].r)
			Modify(mr--,-1);
		while (ml<ask[c].l)
			Modify(ml++,-1);
		ans[ask[c].id]=now_ans();
	}
	F(i,1,q) printf("%d\n",ans[i]);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define fore(v,u) for (vector<int>::iterator v=g[u].begin();v!=g[u].end();v++)
#define LL long long
#define LD long double
LL rd(void) {
	LL x=0,f=1; char c=getchar();
	for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
	for (;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
char rdc(void) {
	char c=getchar();
	while (!isalpha(c))
		c=getchar();
	return c;
}

const int N=32768;
const int Q=32768;
const int S=65536;

int n,mod,a[N];
int tot,hsh[N];
void Decrete(void) {
	F(i,1,n) hsh[++tot]=a[i];
	sort(hsh+1,hsh+tot+1);
	tot=unique(hsh+1,hsh+tot+1)-hsh-1;
	F(i,1,n) a[i]=lower_bound(hsh+1,hsh+tot+1,a[i])-hsh;
	F(i,1,tot) hsh[i]%=mod;
}

const int M=30002;
int fic[N];
int Prework(void) {
	fic[1]=fic[2]=1;
	F(i,3,M) fic[i]=(fic[i-1]+fic[i-2])%mod;
}

int q,c;
struct Ask {
	int l,r; int id;
	friend int operator < (Ask a,Ask b) {
		int ownA=a.l/c,ownB=b.l/c;
		if (ownA!=ownB) return ownA<ownB; else return a.r<b.r;
	}
}ask[Q];

struct Matrix {
	int h,w; int a[3][3];
	Matrix(int _h=0,int _w=0):h(_h),w(_w) {
		memset(a,0,sizeof a);
	}
	friend Matrix operator * (Matrix ma,Matrix mb) {
		Matrix mc(ma.h,mb.w); int s=ma.w;
		F(i,1,mc.h) F(j,1,mc.w) F(k,1,s)
			(mc.a[i][j]+=(LL)ma.a[i][k]*mb.a[k][j]%mod)%=mod;
		return mc;
	}
	friend Matrix operator + (Matrix ma,Matrix mb) {
		Matrix mc(ma.h,ma.w);
		F(i,1,mc.h) F(j,1,mc.w)
			mc.a[i][j]=(ma.a[i][j]+mb.a[i][j])%mod;
		return mc;
	}
}mv_back,mv_forw,e;
void Init(void) {
	mv_back=Matrix(2,2); mv_back.a[1][2]=mv_back.a[2][1]=mv_back.a[2][2]=1;
	mv_forw=Matrix(2,2); mv_forw.a[1][1]=-1,mv_forw.a[1][2]=mv_forw.a[2][1]=1;
	e=Matrix(2,2); e.a[1][1]=e.a[2][2]=1;
}

Matrix add[S],sum[S]; int cnt[S];
void Build(int x,int l,int r) {
	add[x]=e; sum[x]=Matrix(1,2);
	if (l!=r) Build(x<<1,l,(l+r)>>1),Build(x<<1|1,(l+r+2)>>1,r);
}
void Update(int x) {
	cnt[x]=cnt[x<<1]+cnt[x<<1|1];
	sum[x]=sum[x<<1]+sum[x<<1|1];
}
void Addit(int x,Matrix ad) {
	sum[x]=sum[x]*ad;
	add[x]=add[x]*ad;
}
void Clear(int x) {
	Addit(x<<1,add[x]);
	Addit(x<<1|1,add[x]);
	add[x]=e;
}
int LS(int x,int l,int r,int loc) {
	if (r<=loc) return cnt[x];
	int mid=(l+r)>>1;
	int cnt_all=LS(x<<1,l,mid,loc);
	if (mid+1<=loc) cnt_all+=LS(x<<1|1,mid+1,r,loc);
	return cnt_all;
}
void Modify(int x,int l,int r,int loc,LL v1,LL v2,int kd) {
	if (l==r) {
		cnt[x]+=kd;
		sum[x].a[1][1]=v1;
		sum[x].a[1][2]=v2;
		return;
	}
	Clear(x);
	int mid=(l+r)>>1;
	if (loc<=mid)
		Modify(x<<1,l,mid,loc,v1,v2,kd);
	else Modify(x<<1|1,mid+1,r,loc,v1,v2,kd);
	Update(x);
}
void Interval_Add(int x,int l,int r,int ql,int qr,Matrix ad) {
	if (ql<=l&&r<=qr) {
		Addit(x,ad);
		return;
	}
	Clear(x);
	int mid=(l+r)>>1;
	if (ql<=mid)
		Interval_Add(x<<1,l,mid,ql,qr,ad);
	if (mid+1<=qr)
		Interval_Add(x<<1|1,mid+1,r,ql,qr,ad);
	Update(x);
}
int now_ans(void) {
	int t=sum[1].a[1][1];
	(t+=mod)%=mod;
	return t;
}

int ml,mr;
int ans[Q];
int vis[N];	//每个数出现的次数
void Modify(int x,int kd) {
	int val=a[x]; int real_val=hsh[val];
	if (vis[val]>0) {
		Modify(1,1,tot,val,0,0,-1);
		if (val+1<=tot)
			Interval_Add(1,1,tot,val+1,tot,mv_forw);
	}
	vis[val]+=kd;
	if (vis[val]>0) {
		int cntL=(val==1?0:val<=LS(1,1,tot,val-1));
		Modify(1,1,tot,val,(LL)real_val*fic[cntL+1]%mod,(LL)real_val*fic[cntL+2]%mod,1);
		if (val+1<=tot)
			Interval_Add(1,1,tot,val+1,tot,mv_back);
	}
}

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("sdchr.in","r",stdin);
	#endif

	n=rd(),mod=rd(); F(i,1,n) a[i]=rd();
	Decrete();

	Prework();

	q=rd(); c=(int)sqrt(q);
	F(c,1,q) {
		int l=rd(),r=rd();
		ask[c].l=l,ask[c].r=r,ask[c].id=c;
	}
	sort(ask+1,ask+q+1);

	Init();
	Build(1,1,tot);
	ml=1,mr=0;
	F(c,1,q) {
		while (mr<ask[c].r)
			Modify(++mr,1);
		while (ml>ask[c].l)
			Modify(--ml,1);
		while (mr>ask[c].r)
			Modify(mr--,-1);
		while (ml<ask[c].l)
			Modify(ml++,-1);
		ans[ask[c].id]=now_ans();
	}
	F(i,1,q) printf("%d\n",ans[i]);

	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;

#define F(i, a, b) for (register int i = (a); i <= (b); i++)

const int N = 40000;

int n, m, a[N], h[N];
int f[N], forw[2][2], back[2][2];

int q, c, ans[N];
struct Query {
	int l, r, id;
	friend inline bool operator < (Query A, Query B) {
		int ownA = A.l / c, ownB = B.l / c;
		return ownA != ownB ? ownA < ownB : A.r < B.r;
	}
}qs[N];

int cnt[N];
struct Tree {
	int l, r, cnt, sum[2], tag[2][2];
}tr[70000];		//65536

inline int rd(void) {
	int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
	int x = 0; for (; isdigit(c); c = getchar()) x = x*10+c-'0'; return x*f;
}

void Build(int x, int L, int R) {
	tr[x].l = L, tr[x].r = R, tr[x].tag[0][0] = tr[x].tag[1][1] = 1;
	if (L != R) {
		int M = (L+R)>>1;
		Build(x<<1, L, M);
		Build(x<<1|1, M+1, R);
	}
}
inline int Cnt(int x, int w) {
	// 求 cnt , 无需 Clear
	if (tr[x].r < w) return tr[x].cnt;
	if (tr[x].l == tr[x].r) return 0;
	int ansL = Cnt(x<<1, w);
	return ansL + Cnt(x<<1|1, w);
}
inline void Push(int x, int tag[2][2]) {
	int f0 = tr[x].sum[0], f1 = tr[x].sum[1];
	tr[x].sum[0] = (tag[0][0] * f0 + tag[0][1] * f1) % m;
	tr[x].sum[1] = (tag[1][0] * f0 + tag[1][1] * f1) % m;

	int f[2][2]; f[0][0] = tr[x].tag[0][0], f[0][1] = tr[x].tag[0][1], f[1][0] = tr[x].tag[1][0], f[1][1] = tr[x].tag[1][1];
	tr[x].tag[0][0] = (tag[0][0] * f[0][0] + tag[0][1] * f[1][0]) % m;
	tr[x].tag[0][1] = (tag[0][0] * f[0][1] + tag[0][1] * f[1][1]) % m;
	tr[x].tag[1][0] = (tag[1][0] * f[0][0] + tag[1][1] * f[1][0]) % m;
	tr[x].tag[1][1] = (tag[1][0] * f[0][1] + tag[1][1] * f[1][1]) % m;
}
inline void Clear(int x) {
	if (tr[x].tag[0][0] == 1 && !tr[x].tag[0][1] && !tr[x].tag[1][0] && tr[x].tag[1][1] == 1) return;
	Push(x<<1, tr[x].tag);
	Push(x<<1|1, tr[x].tag);
	tr[x].tag[0][0] = tr[x].tag[1][1] = 1, tr[x].tag[0][1] = tr[x].tag[1][0] = 0;
}
inline void Modify(int x, int w, int tag[2][2]) {
	if (tr[x].l >= w) { Push(x, tag); return; }
	if (tr[x].l == tr[x].r) return;
	Clear(x);
	Modify(x<<1, w, tag);
	Modify(x<<1|1, w, tag);
	tr[x].sum[0] = (tr[x<<1].sum[0] + tr[x<<1|1].sum[0]) % m;
	tr[x].sum[1] = (tr[x<<1].sum[1] + tr[x<<1|1].sum[1]) % m;
	// 处理矩阵, 无需将 cnt 上传.
}
inline void Insert(int x, int w, int f0, int f1, int c) {
	if (tr[x].l == tr[x].r) { tr[x].cnt += c, tr[x].sum[0] = f0, tr[x].sum[1] = f1; return; }
	Clear(x);
	int M = (tr[x].l+tr[x].r)>>1;
	w <= M ? Insert(x<<1, w, f0, f1, c) : Insert(x<<1|1, w, f0, f1, c);
	tr[x].cnt = tr[x<<1].cnt + tr[x<<1|1].cnt;
	tr[x].sum[0] = (tr[x<<1].sum[0] + tr[x<<1|1].sum[0]) % m;
	tr[x].sum[1] = (tr[x<<1].sum[1] + tr[x<<1|1].sum[1]) % m;
}

// 单点只有一个值
inline void Add(int w) {
	if (++cnt[w] == 1) {
		int c = Cnt(1, w)+1;		// 比 w 小的个数 + 1
		Insert(1, w, f[c] * h[w] % m, f[c+1] * h[w] % m, 1);
		Modify(1, w+1, back);
	}
}
inline void Erase(int w) {
	if (cnt[w]-- == 1) {
		Insert(1, w, 0, 0, -1);
		Modify(1, w+1, forw);
	}
}

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("xsy2108.in", "r", stdin);
		freopen("xsy2108.out", "w", stdout);
	#endif

	n = rd(), m = rd();
	F(i, 1, n) a[i] = h[i] = rd();
	sort(h+1, h+n+1);
	F(i, 1, n) a[i] = lower_bound(h+1, h+n+1, a[i]) - h;
	F(i, 1, n) h[i] %= m;

	f[1] = f[2] = 1; F(i, 3, n+5) f[i] = (f[i-1] + f[i-2]) % m;
	back[0][1] = back[1][0] = back[1][1] = 1;
	forw[0][0] = -1, forw[0][1] = forw[1][0] = 1;

	q = rd();
	// c = (int)sqrt(n);
	c = 60;
	F(i, 1, q) {
		int l = rd(), r = rd();
		qs[i] = (Query){l, r, i};
	}
	sort(qs+1, qs+q+1);

	int ml = 1, mr = 0;
	Build(1, 1, n);
	F(i, 1, q) {
		while (mr < qs[i].r)
			Add(a[++mr]);
		while (ml > qs[i].l)
			Add(a[--ml]);
		while (mr > qs[i].r)
			Erase(a[mr--]);
		while (ml < qs[i].l)
			Erase(a[ml++]);
		ans[qs[i].id] = tr[1].sum[0];
	}
	F(i, 1, q) printf("%d\n", (ans[i] + m) % m);

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define fore(v,u) for (vector<int>::iterator v=g[u].begin();v!=g[u].end();v++)
#define LL long long
#define LD long double
LL rd(void) {
	LL x=0,f=1; char c=getchar();
	for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
	for (;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
char rdc(void) {
	char c=getchar();
	while (!isalpha(c))
		c=getchar();
	return c;
}

const int N=32768;
const int Q=32768;
const int S=65536;

int n,mod,a[N];
int tot,hsh[N];
void Decrete(void) {
	F(i,1,n) hsh[++tot]=a[i];
	sort(hsh+1,hsh+tot+1);
	tot=unique(hsh+1,hsh+tot+1)-hsh-1;
	F(i,1,n) a[i]=lower_bound(hsh+1,hsh+tot+1,a[i])-hsh;
	F(i,1,tot) hsh[i]%=mod;
}

const int M=30002;
int fic[N];
int Prework(void) {
	fic[1]=fic[2]=1;
	F(i,3,M) fic[i]=(fic[i-1]+fic[i-2])%mod;
}

int q,c;
struct Ask {
	int l,r; int id;
	friend int operator < (Ask a,Ask b) {
		int ownA=a.l/c,ownB=b.l/c;
		if (ownA!=ownB) return ownA<ownB; else return a.r<b.r;
	}
}ask[Q];

struct Matrix {
	int h,w; int a[3][3];
	Matrix(int _h=0,int _w=0):h(_h),w(_w) {
		memset(a,0,sizeof a);
	}
	friend Matrix operator * (Matrix ma,Matrix mb) {
		Matrix mc(ma.h,mb.w); int s=ma.w;
		F(i,1,mc.h) F(j,1,mc.w) F(k,1,s)
			(mc.a[i][j]+=(LL)ma.a[i][k]*mb.a[k][j]%mod)%=mod;
		return mc;
	}
	friend Matrix operator + (Matrix ma,Matrix mb) {
		Matrix mc(ma.h,ma.w);
		F(i,1,mc.h) F(j,1,mc.w)
			mc.a[i][j]=(ma.a[i][j]+mb.a[i][j])%mod;
		return mc;
	}
}mv_back,mv_forw,e;
void Init(void) {
	mv_back=Matrix(2,2); mv_back.a[1][2]=mv_back.a[2][1]=mv_back.a[2][2]=1;
	mv_forw=Matrix(2,2); mv_forw.a[1][1]=-1,mv_forw.a[1][2]=mv_forw.a[2][1]=1;
	e=Matrix(2,2); e.a[1][1]=e.a[2][2]=1;
}

Matrix add[S],sum[S]; int cnt[S];
void Build(int x,int l,int r) {
	add[x]=e; sum[x]=Matrix(1,2);
	if (l!=r) Build(x<<1,l,(l+r)>>1),Build(x<<1|1,(l+r+2)>>1,r);
}
void Update(int x) {
	cnt[x]=cnt[x<<1]+cnt[x<<1|1];
	sum[x]=sum[x<<1]+sum[x<<1|1];
}
void Addit(int x,Matrix ad) {
	sum[x]=sum[x]*ad;
	add[x]=add[x]*ad;
}
void Clear(int x) {
	Addit(x<<1,add[x]);
	Addit(x<<1|1,add[x]);
	add[x]=e;
}
int LS(int x,int l,int r,int loc) {
	if (r<=loc) return cnt[x];
	int mid=(l+r)>>1;
	int cnt_all=LS(x<<1,l,mid,loc);
	if (mid+1<=loc) cnt_all+=LS(x<<1|1,mid+1,r,loc);
	return cnt_all;
}
void Modify(int x,int l,int r,int loc,LL v1,LL v2,int kd) {
	if (l==r) {
		cnt[x]+=kd;
		sum[x].a[1][1]=v1;
		sum[x].a[1][2]=v2;
		return;
	}
	int mid=(l+r)>>1;
	if (loc<=mid)
		Modify(x<<1,l,mid,loc,v1,v2,kd);
	else Modify(x<<1|1,mid+1,r,loc,v1,v2,kd);
	Update(x);
}
void Interval_Add(int x,int l,int r,int ql,int qr,Matrix ad) {
	if (ql<=l&&r<=qr) {
		Addit(x,ad);
		return;
	}
	int mid=(l+r)>>1;
	if (ql<=mid)
		Interval_Add(x<<1,l,mid,ql,qr,ad);
	if (mid+1<=qr)
		Interval_Add(x<<1|1,mid+1,r,ql,qr,ad);
	Update(x);
}
int now_ans(void) {
	int t=sum[1].a[1][1];
	(t+=mod)%=mod;
	return t;
}

int ml,mr;
int ans[Q];
int vis[N];	//每个数出现的次数
void Modify(int x,int kd) {
	int val=a[x]; int real_val=hsh[val];
	if (vis[val]>0) {
		Modify(1,1,tot,val,0,0,-1);
		if (val+1<=tot)
			Interval_Add(1,1,tot,val+1,tot,mv_forw);
	}
	vis[val]+=kd;
	if (vis[val]>0) {
		int cntL=(val==1?0:val<=LS(1,1,tot,val-1));
		Modify(1,1,tot,val,(LL)real_val*fic[cntL+1]%mod,(LL)real_val*fic[cntL+2]%mod,1);
		if (val+1<=tot)
			Interval_Add(1,1,tot,val+1,tot,mv_back);
	}
}

int main(void) {
	#ifndef ONLINE_JUDGE
		freopen("sdchr.in","r",stdin);
	#endif

	n=rd(),mod=rd(); F(i,1,n) a[i]=rd();
	Decrete();

	Prework();

	q=rd(); c=(int)sqrt(q);
	F(c,1,q) {
		int l=rd(),r=rd();
		ask[c].l=l,ask[c].r=r,ask[c].id=c;
	}
	sort(ask+1,ask+q+1);

	Init();
	Build(1,1,tot);
	ml=1,mr=0;
	F(c,1,q) {
		while (mr<ask[c].r)
			Modify(++mr,1);
		while (ml>ask[c].l)
			Modify(--ml,1);
		while (mr>ask[c].r)
			Modify(mr--,-1);
		while (ml<ask[c].l)
			Modify(ml++,-1);
		ans[ask[c].id]=now_ans();
	}
	F(i,1,q) printf("%d\n",ans[i]);

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#include <sstream>
#include <numeric>
#include <cassert>
using namespace std;

#define f first
#define s second
#define mp make_pair
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define forit(it,S) for(__typeof(S.begin()) it = S.begin(); it != S.end(); ++it)
#ifdef WIN32
#define I64d "%I64d"
#else
#define I64d "%lld"
#endif

typedef pair <int, int> pi;
typedef vector <int> vi;
typedef long long ll;

int n, m;
int f[33333];
pi a[33333];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i].f);
    a[i].s = i;
  }
  sort(a, a + n);
  f[0] = f[1] = 1 % m;
  for (int i = 2; i <= n; i++) {
    f[i] = f[i - 1] + f[i - 2];
    if (f[i] >= m) {
      f[i] -= m;
    }
  }

  int l, r, q;
  scanf("%d", &q);
  while (q--) {
    scanf("%d%d", &l, &r);
    --l, --r;
    ll sum = 0, color = -1, i = 0;
    for (int j = 0; j < n; j++) {
      if (color == a[j].f) continue;
      if (l <= a[j].s && a[j].s <= r) {
        color = a[j].f;
        sum += ((ll)f[i++]) * a[j].f;
      }
    }
    printf("%d\n", int(sum % m));
  }
  return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim!=B.l/lim) return A.l<B.l;
 if((A.l/lim)&1)
 	return A.r<B.r;
 else
 	return A.r>B.r;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 	change(p*2,l,(l+r)/2,k);
 else
 	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 t[p].size=t[p*2].size+t[p*2+1].size;
 update(k+1,p*2);
 update(k+t[p*2].size+1,p*2+1);
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
}

void trans(int l,int r)
{int i;
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=a[i],y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=a[i],y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 while(nr<r)
 {	x=a[nr+1],y=1;
 	change(1,1,n,0);
 	nr++;
 }
 while(nr>r)
 {	x=a[nr],y=-1;
 	change(1,1,n,0);
 	nr--;
 }
 while(nl<l)
 {	x=a[nl],y=-1;
 	change(1,1,n,0);
 	nl++;
 }
 while(nl>l)
 {	x=a[nl-1],y=1;
 	change(1,1,n,0);
 	nl--;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 //lim=(int)sqrt(n);
 lim=300;
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=a[i];
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim==B.l/lim) return A.r<B.r;
 return A.l<B.l;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 	change(p*2,l,(l+r)/2,k);
 else
 	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 t[p].size=t[p*2].size+t[p*2+1].size;
 update(k+1,p*2);
 update(k+t[p*2].size+1,p*2+1);
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
}

void trans(int l,int r)
{int i;
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 		y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 		y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 while(nr<r)
 {	x=lower_bound(sa+1,sa+tot+1,a[nr+1])-sa;
 	y=1;
 	change(1,1,n,0);
 	nr++;
 }
 while(nr>r)
 {	x=lower_bound(sa+1,sa+tot+1,a[nr])-sa;
 	y=-1;
 	change(1,1,n,0);
 	nr--;
 }
 while(nl<l)
 {	x=lower_bound(sa+1,sa+tot+1,a[nl])-sa;
 	y=-1;
 	change(1,1,n,0);
 	nl++;
 }
 while(nl>l)
 {	x=lower_bound(sa+1,sa+tot+1,a[nl-1])-sa;
 	y=1;
 	change(1,1,n,0);
 	nl--;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim!=B.l/lim) return A.l<B.l;
 if((A.l/lim)&1)
 	return A.r>B.r;
 else
 	return A.r<B.r;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 //cout<<k<<" "<<p<<" "<<t[p].now<<" "<<t[p].s1<<" "<<t[p].s2<<endl;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 //cout<<t[p].s1<<" "<<t[p].s2<<endl;
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 {	change(p*2,l,(l+r)/2,k);
 	update(k+t[p*2].size+1,p*2+1);
 }
 else
 {	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 	update(k+1,p*2);
 }
 t[p].size=t[p*2].size+t[p*2+1].size;
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
 //cout<<t[p*2].s1<<" "<<t[p*2].s2<<" "<<t[p*2+1].s1<<" "<<t[p*2+1].s2<<endl;
 //cout<<"## "<<p<<" "<<l<<" "<<r<<" "<<t[p].now<<" "<<t[p].s1<<" "<<t[p].s2<<endl;
}

void trans(int l,int r)
{int i;
 /*
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=a[i],y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=a[i],y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 */
 //cout<<l<<" "<<r<<" "<<nl<<" "<<nr<<endl;
 while(nr<r)
 {	x=a[nr+1],y=1;
 	change(1,1,n,0);
 	nr++;
 	//cout<<"now I "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nl>l)
 {	x=a[nl-1],y=1;
 	change(1,1,n,0);
 	nl--;
 	//cout<<"now II "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nr>r)
 {	x=a[nr],y=-1;
 	change(1,1,n,0);
 	nr--;
 	//cout<<"now III "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nl<l)
 {	x=a[nl],y=-1;
 	change(1,1,n,0);
 	nl++;
 	//cout<<"now IIII "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n+5;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n+5;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n+5;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=a[i];
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim!=B.l/lim) return A.l<B.l;
 if((A.l/lim)&1)
 	return A.r<B.r;
 else
 	return A.r>B.r;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 	change(p*2,l,(l+r)/2,k);
 else
 	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 t[p].size=t[p*2].size+t[p*2+1].size;
 update(k+1,p*2);
 update(k+t[p*2].size+1,p*2+1);
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
}

void trans(int l,int r)
{int i;
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=a[i],y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=a[i],y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 while(nr<r)
 {	x=a[nr+1],y=1;
 	change(1,1,n,0);
 	nr++;
 }
 while(nr>r)
 {	x=a[nr],y=-1;
 	change(1,1,n,0);
 	nr--;
 }
 while(nl<l)
 {	x=a[nl],y=-1;
 	change(1,1,n,0);
 	nl++;
 }
 while(nl>l)
 {	x=a[nl-1],y=1;
 	change(1,1,n,0);
 	nl--;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=a[i];
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim==B.l/lim) return A.r<B.r;
 return A.l<B.l;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 	change(p*2,l,(l+r)/2,k);
 else
 	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 t[p].size=t[p*2].size+t[p*2+1].size;
 update(k+1,p*2);
 update(k+t[p*2].size+1,p*2+1);
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
}

void trans(int l,int r)
{int i;
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=i,y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=i,y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 while(nr<r)
 {	x=nr+1,y=1;
 	change(1,1,n,0);
 	nr++;
 }
 while(nr>r)
 {	x=nr,y=-1;
 	change(1,1,n,0);
 	nr--;
 }
 while(nl<l)
 {	x=nl,y=-1;
 	change(1,1,n,0);
 	nl++;
 }
 while(nl>l)
 {	x=nl-1,y=1;
 	change(1,1,n,0);
 	nl--;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=i;
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim==B.l/lim) return A.r<B.r;
 return A.l<B.l;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 	change(p*2,l,(l+r)/2,k);
 else
 	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 t[p].size=t[p*2].size+t[p*2+1].size;
 update(k+1,p*2);
 update(k+t[p*2].size+1,p*2+1);
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
}

void trans(int l,int r)
{int i;
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=a[i],y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=a[i],y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 while(nr<r)
 {	x=a[nr+1],y=1;
 	change(1,1,n,0);
 	nr++;
 }
 while(nr>r)
 {	x=a[nr],y=-1;
 	change(1,1,n,0);
 	nr--;
 }
 while(nl<l)
 {	x=a[nl],y=-1;
 	change(1,1,n,0);
 	nl++;
 }
 while(nl>l)
 {	x=a[nl-1],y=1;
 	change(1,1,n,0);
 	nl--;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 //lim=(int)sqrt(n);
 lim=300;
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=a[i];
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim==B.l/lim) return A.r<B.r;
 return A.l<B.l;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now].x*x%mod+(long long)f1[k-t[p].now].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+1-t[p].now].x*x%mod+(long long)f1[k+1-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].ss=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 	change(p*2,l,(l+r)/2,k);
 else
 	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 t[p].size=t[p*2].size+t[p*2+1].size;
 update(k+1,p*2);
 update(k+t[p*2].size+1,p*2+1);
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
}

void trans(int l,int r)
{int i;
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 		y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 		y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 while(nr<r)
 {	x=lower_bound(sa+1,sa+tot+1,a[nr+1])-sa;
 	y=1;
 	change(1,1,n,0);
 	nr++;
 }
 while(nl<l)
 {	x=lower_bound(sa+1,sa+tot+1,a[nl])-sa;
 	y=-1;
 	change(1,1,n,0);
 	nl++;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=t[1].s1;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}


#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

struct unit
{int l,r,num;
};
struct unit q[60010];

struct fib
{int x,y;
};
struct fib f1[60010],f2[60010];

struct node
{int now,size,ss,s1,s2;
};
struct node t[300010];

int n,nq,mod,lim,x,y,nl,nr,tot;
int a[60010],b[60010],sa[60010],f[60010];

fib operator +(fib A,fib B){return (fib){(A.x+B.x)%mod,(A.y+B.y)%mod};}
fib operator -(fib A,fib B){return (fib){(A.x-B.x)%mod,(A.y-B.y)%mod};}

bool cmp(unit A,unit B)
{if(A.l/lim!=B.l/lim) return A.l<B.l;
 if((A.l/lim)&1)
 	return A.r<B.r;
 else
 	return A.r>B.r;
}

void update(int k,int p)
{int x,y;
 if(t[p].now==k) return;
 x=t[p].s1;
 y=t[p].s2;
 //cout<<k<<" "<<p<<" "<<t[p].now<<" "<<t[p].s1<<" "<<t[p].s2<<endl;
 if(t[p].now<k)
 {	t[p].s1=((long long)f1[k-t[p].now+1].x*x%mod+(long long)f1[k-t[p].now+1].y*y%mod)%mod;
 	t[p].s2=((long long)f1[k+2-t[p].now].x*x%mod+(long long)f1[k+2-t[p].now].y*y%mod)%mod;
 }
 else
 {	t[p].s1=((long long)f2[2+t[p].now-k].x*x%mod+(long long)f2[2+t[p].now-k].y*y%mod)%mod;
 	t[p].s2=((long long)f2[1+t[p].now-k].x*x%mod+(long long)f2[1+t[p].now-k].y*y%mod)%mod;
 }
 //cout<<t[p].s1<<" "<<t[p].s2<<endl;
 t[p].now=k;
}

void change(int p,int l,int r,int k)
{if(l==r)
 {	if((t[p].ss==1 && y==-1) || (t[p].ss==0 && y==1))
	{	if(y==-1)
			t[p].s1=t[p].s2=t[p].size=t[p].now=0;
		else
		{	t[p].now=k+1;
			t[p].s1=(long long)sa[l]*f[k+1]%mod;
			t[p].s2=(long long)sa[l]*f[k+2]%mod;
			t[p].size=1;
		}
	}
	t[p].ss+=y;
 	return;
 }
 if(x<=(l+r)/2)
 {	change(p*2,l,(l+r)/2,k);
 	update(k+t[p*2].size+1,p*2+1);
 }
 else
 {	change(p*2+1,(l+r)/2+1,r,k+t[p*2].size);
 	update(k+1,p*2);
 }
 t[p].size=t[p*2].size+t[p*2+1].size;
 t[p].now=k+1;
 t[p].s1=(t[p*2].s1+t[p*2+1].s1)%mod;
 t[p].s2=(t[p*2].s2+t[p*2+1].s2)%mod;
 //cout<<t[p*2].s1<<" "<<t[p*2].s2<<" "<<t[p*2+1].s1<<" "<<t[p*2+1].s2<<endl;
 //cout<<"## "<<p<<" "<<l<<" "<<r<<" "<<t[p].now<<" "<<t[p].s1<<" "<<t[p].s2<<endl;
}

void trans(int l,int r)
{int i;
 /*
 if(nl>r || nr<l)
 {	for(i=nl;i<=nr;i++)
 	{	x=a[i],y=-1;
 		change(1,1,n,0);
 	}
 	for(i=l;i<=r;i++)
 	{	x=a[i],y=1;
 		change(1,1,n,0);
 	}
 	nl=l;
 	nr=r;
 	return;
 }
 */
 //cout<<l<<" "<<r<<" "<<nl<<" "<<nr<<endl;
 while(nr<r)
 {	x=a[nr+1],y=1;
 	change(1,1,n,0);
 	nr++;
 	//cout<<"now I "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nl>l)
 {	x=a[nl-1],y=1;
 	change(1,1,n,0);
 	nl--;
 	//cout<<"now II "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nr>r)
 {	x=a[nr],y=-1;
 	change(1,1,n,0);
 	nr--;
 	//cout<<"now III "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
 while(nl<l)
 {	x=a[nl],y=-1;
 	change(1,1,n,0);
 	nl++;
 	//cout<<"now IIII "<<nl<<" "<<nr<<" "<<t[1].s1<<endl;
 }
}

int main()
{int i,j;

 scanf("%d%d",&n,&mod);
 lim=(int)sqrt(n);
 for(i=1;i<=n;i++)
 {	scanf("%d",&a[i]);
 	sa[++tot]=a[i];
 }
 sort(sa+1,sa+tot+1);
 for(i=1;i<=n;i++) a[i]=lower_bound(sa+1,sa+tot+1,a[i])-sa;
 scanf("%d",&nq);
 for(i=1;i<=nq;i++)
 {	scanf("%d%d",&q[i].l,&q[i].r);
 	q[i].num=i;
 }
 sort(q+1,q+nq+1,cmp);
 f1[1]=(fib){1,0};
 f1[2]=(fib){0,1};
 for(i=3;i<=n+5;i++) f1[i]=f1[i-1]+f1[i-2];
 f2[1]=(fib){0,1};
 f2[2]=(fib){1,0};
 for(i=3;i<=n+5;i++) f2[i]=f2[i-2]-f2[i-1];
 f[1]=f[2]=1;
 for(i=3;i<=n+5;i++) f[i]=(f[i-1]+f[i-2])%mod;
 for(i=q[1].l;i<=q[1].r;i++)
 {	x=a[i];
 	y=1;
 	change(1,1,n,0);
 }
 nl=q[1].l;
 nr=q[1].r;
 b[q[1].num]=t[1].s1;
 for(i=2;i<=nq;i++)
 {	trans(q[i].l,q[i].r);
 	b[q[i].num]=(t[1].s1+mod)%mod;
 }
 for(i=1;i<=nq;i++)
 	printf("%d\n",b[i]);
 return 0;
}