#include<bits/stdc++.h>
using std::cout;
using std::cin;
using std::endl;

std::vector<std::string> split(const std::string &str, char sep)
{
    std::vector<std::string> v;
    std::stringstream ss(str);
    std::string buffer;
    while( std::getline(ss, buffer, sep) ) {
        v.push_back(buffer);
    }
    return v;
}

int Stoi(const std::string &str)
{
    int num=0, i=0;
    std::string s = str;
    while (i<s.size())
    {
        if (s[i]<'0'||s[i]>'9')
            break;
        num = num * 10 + (str[i] - '0');
        i++;
    }

    return num;
}

std::string replace(const std::string &str1, int a, int b, const std::string &str2)
{
    std::string s1, s2;
    s1 = str1; s2 = str2;

    s1 = s1.erase(a, b-a+1);
    //cout << s1 << endl;
    s1 = s1.insert(a, s2);
    //cout << s1 << endl;
    return s1;
}

int main(){
    int a, b, count=1;
    long long n;
    std::string str, line, doubt, s;
    cin >> str;
    cin >> n;
    std::getline(cin, doubt);

    std::vector<std::string> Priope, v;
    std::vector<int> num(n);


    for (int i = 0; i < n; i++)
    {
        std::getline(cin, line);
        v = split(line, ' ');

        a = Stoi(v[1]); b = Stoi(v[2]);
        
        if (v[0] == "print")
        {
            s = str.substr(a, b-a+1);
            Priope.push_back(s);
            num.push_back(i);
            //cout << s << endl;
        }
        else if (v[0] == "reverse")
            {std::reverse(str.begin()+a, str.begin()+b+1); }//cout << str << endl;}
        else if(v[0] == "replace")
            str = replace(str, a, b, v[3]);
        else
        {
            cout << "Wrong Command:" << v[0] << endl;
            i--;
        }
         
    }
/*
    for (int i = 0; i < num.size(); i++)
        cout << Priope(i) << endl;
*/
    std::vector<std::string>::iterator itr;
    itr = Priope.begin();
    while (itr != Priope.end())
    {
        cout << *itr << endl;
        itr++;
    }
        
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define MN 200005
#define LL long long
#define mp make_pair
#define fir first
#define sec second
#define pii pair<int,int>
inline void chkmax(int &a,int b){if(a<b)a=b;}
inline void chkmin(int &a,int b){if(a>b)a=b;}
inline int read(){
	   int a=0,fh=1;char c=getchar();
	   while(c>'9'||c<'0'){if(c=='-')fh=-1;c=getchar();}
	   while('0'<=c&&c<='9'){
		   a=a*10+c-48;
		   c=getchar();
	   }
	   return a*fh;
}
const int py=6005;
int f[6005][16005],n,Mod;
void add(int &a,int b){((a+=b)>=Mod)?(a-=Mod):0;}
int main(){
	n=read()*3;Mod=read();
	f[0][py]=1;
	for(int i=0;i<n;++i){
		for(int j=-i;j<=i;++j){
			add(f[i+1][j+py+1],f[i][j+py]);
			add(f[i+2][j+py-1],(LL)f[i][j+py]*(i+1)%Mod);
			add(f[i+3][j+py],(LL)f[i][j+py]*(i+1)*(i+2)%Mod);
		}
	}
	LL ans=0;
	for(int i=0;i<=n;++i)ans+=f[n][i+py];
	printf("%lld\n",ans%Mod);
	return 0;
}















