Nick like string like rotate sort rearrange character string once write random string character b c piece paper begin perform follow operation adjacent character replace second character adjacent character replace character second to understand action well let look stre abc all follow string obtain perform describe operation abc bbc abb acc Lets denote frequency character character b c number occurrence character string for example stre abc 1 b 1 c 1 string bbc 0 b 2 c 1 while perform describe operation Nick get balanced string Lets string balanced frequency character differ 1 that 1ab1 1ac1 и 1bc1 would help Nick find number different balanced string obtain perform operation describe multiple time give stre s this number calculate modulo 51123987