program test
implicit none
integer(8) :: d,i,j,k,m,n,abcs(3),temp(3)
integer(8) :: big=1000000000 + 7
character(100000) :: s

read(*,*) s
d = len_trim(s)

abcs(1:3) = 0

do i=1,d

	if(s(i:i) .eq. '?') then
		do j=1,3
		temp(j) = abcs(j)
		enddo
		
		abcs(1) = mod(3 * temp(1) + 1,big)
		abcs(2) = mod(mod(3 * temp(2),big) + temp(1),big)
		abcs(3) = mod(mod(3 * temp(3),big) + temp(2),big)
		
	else if(s(i:i) .eq. 'A') then
		abcs(1) = mod(abcs(1) + 1,big)
		
	else if(s(i:i) .eq. 'B') then
		abcs(2) = mod(abcs(2) + abcs(1),big)
		
		
	else if(s(i:i) .eq. 'C') then
		abcs(3) = mod(abcs(3) + abcs(2),big)
	endif
!	write(*,*) abcs(3)
enddo

write(*,*) abcs(3)

end program