/*配列番号0 = J, 1 = O, 2 = I, 3 = JO, 4 = JI, 5 = OI, 6 = JOI*/
#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
	int n, i;
	long long int **dp, sum = 0;
	char *s;
	cin >> n;
	s = new char[n + 1];
	dp = new long long int*[n + 1];

	cin >> s;

	for (i = 0; i < n+1; i++){
		dp[i] = new long long int[7];
	}

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

	dp[0][0] = 1;

	for (i = 0; s[i] != '\0'; i++){

		if (s[i] == 'J'){

			//J単体の時
				dp[i + 1][0] += dp[i][0];
				dp[i + 1][0] += dp[i][3];
				dp[i + 1][0] += dp[i][4];
				dp[i + 1][0] += dp[i][6];
				dp[i + 1][0] %= 10007;

			//J,Oで来た場合
				dp[i + 1][3] += dp[i][0];
				dp[i + 1][3] += dp[i][1];
				dp[i + 1][3] += dp[i][3];
				dp[i + 1][3] += dp[i][4];
				dp[i + 1][3] += dp[i][5];
				dp[i + 1][3] += dp[i][6];
				dp[i + 1][3] %= 10007;

			//J,iできた場合
				dp[i + 1][4] += dp[i][0];
				dp[i + 1][4] += dp[i][2];
				dp[i + 1][4] += dp[i][3];
				dp[i + 1][4] += dp[i][4];
				dp[i + 1][4] += dp[i][5];
				dp[i + 1][4] += dp[i][6];
				dp[i + 1][4] %= 10007;

			//J,O,Iで来た場合
				dp[i + 1][6] += dp[i][0];
				dp[i + 1][6] += dp[i][1];
				dp[i + 1][6] += dp[i][2];
				dp[i + 1][6] += dp[i][3];
				dp[i + 1][6] += dp[i][4];
				dp[i + 1][6] += dp[i][5];
				dp[i + 1][6] += dp[i][6];
				dp[i + 1][6] %= 10007;

		}

		if (s[i] == 'O'){
			
			//O単体の場合
				dp[i + 1][1] += dp[i][1];
				dp[i + 1][1] += dp[i][3];
				dp[i + 1][1] += dp[i][5];
				dp[i + 1][1] += dp[i][6];
				dp[i + 1][1] %= 10007;
			
			//J,Oの場合
				dp[i + 1][3] += dp[i][0];
				dp[i + 1][3] += dp[i][1];
				dp[i + 1][3] += dp[i][3];
				dp[i + 1][3] += dp[i][4];
				dp[i + 1][3] += dp[i][5];
				dp[i + 1][3] += dp[i][6];
				dp[i + 1][3] %= 10007;

			//O,Iの場合
				dp[i + 1][5] += dp[i][1];
				dp[i + 1][5] += dp[i][2];
				dp[i + 1][5] += dp[i][3];
				dp[i + 1][5] += dp[i][4];
				dp[i + 1][5] += dp[i][5];
				dp[i + 1][5] += dp[i][6];
				dp[i + 1][5] %= 10007;

			//J,O,Iの場合
				dp[i + 1][6] += dp[i][0];
				dp[i + 1][6] += dp[i][1];
				dp[i + 1][6] += dp[i][2];
				dp[i + 1][6] += dp[i][3];
				dp[i + 1][6] += dp[i][4];
				dp[i + 1][6] += dp[i][5];
				dp[i + 1][6] += dp[i][6];
				dp[i + 1][6] %= 10007;
		}

		if (s[i] == 'I'){

			//I単体の場合
				dp[i + 1][2] += dp[i][2];
				dp[i + 1][2] += dp[i][4];
				dp[i + 1][2] += dp[i][5];
				dp[i + 1][2] += dp[i][6];
				dp[i + 1][2] %= 10007;
			
			//J,Iの場合
				dp[i + 1][4] += dp[i][0];
				dp[i + 1][4] += dp[i][2];
				dp[i + 1][4] += dp[i][3];
				dp[i + 1][4] += dp[i][4];
				dp[i + 1][4] += dp[i][5];
				dp[i + 1][4] += dp[i][6];
				dp[i + 1][4] %= 10007;

			//O,Iの場合
				dp[i + 1][5] += dp[i][1];
				dp[i + 1][5] += dp[i][2];
				dp[i + 1][5] += dp[i][3];
				dp[i + 1][5] += dp[i][4];
				dp[i + 1][5] += dp[i][5];
				dp[i + 1][5] += dp[i][6];
				dp[i + 1][5] %= 10007;

			//J,O,Iの場合
				dp[i + 1][6] += dp[i][0];
				dp[i + 1][6] += dp[i][1];
				dp[i + 1][6] += dp[i][2];
				dp[i + 1][6] += dp[i][3];
				dp[i + 1][6] += dp[i][4];
				dp[i + 1][6] += dp[i][5];
				dp[i + 1][6] += dp[i][6];
				dp[i + 1][6] %= 10007;
			}
	}

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

	sum %= 10007;

	cout << sum << endl;

	return 0;
}

Measured memory_bytes: 1256