module Memoizable
	def memoize(name)
		original = "__original_#{name}__"
		([Class, Module].include?(self.class) ? self : self.class).class_eval {
			alias_method(original, name)
			private(original)
			cache = {}
			define_method(name){|*args|
				cache[args] ||= send(original, *args)
			}
		}
	end 
end

include Memoizable

$possible_next = {}
(1..7).each{|x|
	prev_attend = (0..2).select{|i| x[i] == 1}
	(0..2).each{|in_charge|
		(1..7).each{|y|
			next_attend = (0..2).select{|i| y[i] == 1}
			if next_attend & prev_attend != [] && next_attend.include?(in_charge)
				($possible_next[[x, in_charge]] ||= []) << y
			end
		}
	}
}

n = gets.to_i
$in_charge = gets.chomp.split("").map{|c|
	case c
		when ?J; 0
		when ?O; 1
		when ?I; 2
	end
}

def f(prev_attend, k)
	in_charge = $in_charge[k-1]
	if k == $in_charge.size
		$possible_next[[prev_attend, in_charge]].size
	else
		sum = 0
		$possible_next[[prev_attend, in_charge]].each{|next_attend|
			sum += f(next_attend, k+1)
		}
		sum % 10007
	end
end

memoize :f

sum = 0
(1..7).each{|x|
	if x[$in_charge[0]] == 1 && x[0] == 1
		sum += f(x, 2)
	end
}
p sum % 10007