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

long long int dp[1002][8];

int main() {

    int n;
    string s;

    cin >> n >> s;

   // dp[0][4] = 1;

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

        bool /*y[8] = {false},*/t[8] = {false};//yesterday,today

        if(s[i]=='J'){
            t[4] = t[5] = t[6] = t[7] = true;
        }
        else if(s[i] == 'O'){
            t[2] = t[3] = t[6] = t[7] = true;
        }
        else{
            t[1] = t[3] = t[5] = t[7] = true;
        }

        /*if(s[i]=='J'){
            y[4] = y[5] = y[6] = y[7] = true;
        }
        else if(s[i] == 'O'){
            y[2] = y[3] = y[6] = y[7] = true;
        }
        else{
            y[1] = y[3] = y[5] = y[7] = true;
        }*/


/*
        if(s[i-1]=='J'){
            y[4] = y[5] = y[6] = y[7] = true;
        }
        else if(s[i-1] == 'O'){
            y[2] = y[3] = y[6] = y[7] = true;
        }
        else{
            y[1] = y[3] = y[5] = y[7] = true;
        }
*/
        if(i==0){
            for(int j=4;j<8;j++){//j = 4,5,6,7
                if(t[j]){
                    dp[i+1][j] = 1;
                }
            }
        }
       else {
/*
            if(s[i-1]=='J'){
                y[4] = y[5] = y[6] = y[7] = true;
            }
            else if(s[i-1] == 'O'){
                y[2] = y[3] = y[6] = y[7] = true;
            }
            else {
                y[1] = y[3] = y[5] = y[7] = true;
            }


            cout << "start" << endl;

            for (int j = 0; j < 8; j++) {
                if (t[j]) {
                    cout << 1 << " ";
                }
                else{
                    cout << 0 << " ";
                }
            }
            cout << endl;

            cout << "foal" << endl;

            for (int j = 0; j < 8; j++) {
                if (y[j]) {
                    cout << 1 << " ";
                }
                else{
                    cout << 0 << " ";
                }
            }
            cout << endl << endl;
*/



            dp[i + 1][0] = 0;
            dp[i + 1][1] = dp[i][1] + dp[i][3] + dp[i][5] + dp[i][7];
            dp[i + 1][2] = dp[i][2] + dp[i][3] + dp[i][6] + dp[i][7];
            dp[i + 1][3] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][5] + dp[i][6] + dp[i][7];
            dp[i + 1][4] = dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7];
            dp[i + 1][5] = dp[i][1] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7];
            dp[i + 1][6] = dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7];
            dp[i + 1][7] = dp[i][1] + dp[i][2] + dp[i][3] + dp[i][4] + dp[i][5] + dp[i][6] + dp[i][7];
            for (int k = 0; k < 8; k++) {
                dp[i + 1][k] %= 10007;
            }
            for (int j = 0; j < 8; j++) {
                if (!t[j]){
                    dp[i+1][j] = 0;
                }
            }
            /*for (int k = 0; k < 8; k++) {
                if (t[k]) {
                    dp[i + 1][j] += dp[i][k];
                    dp[i + 1][j] %= 10007;
                }
                if(y[k]){
                    dp[i + 1][j] += dp[i][k];
                    dp[i + 1][j] %= 10007;
                }
            }*/


        }

    }

  /*  for(int i=0;i<=n;i++){
        for (int j = 0; j < 8; j++) {
           cout << dp[i][j] << " ";
        }
        cout << endl;
    }
*/



    for(int i=0;i<7;i++){
        dp[n][7] += dp[n][i];
        dp[n][7] %= 10007;
    }

    cout << dp[n][7] << endl;


    return 0;
}

Measured memory_bytes: 3244