#P16906. [CCO 2026] Walking on a Graph

[CCO 2026] Walking on a Graph

Problem Description

There is a graph with NN nodes, numbered from 11 to NN. Each node is coloured either black or white. Additionally, it is known that node 11 is black and node 22 is white. For any ii and jj where iji \ne j, there exists a directed edge from node ii to jj that is either red or blue. Its colour is determined using the following logic:

  • If i<ji < j and the nodes have the same colour, then it is red.
  • If i<ji < j and the nodes have different colours, then it is blue.
  • If i>ji > j and the nodes have the same colour, then it is blue.
  • If i>ji > j and the nodes have different colours, then it is red.

LoBren’s favourite colour is initially blue. He then takes a walk on the graph (note that walks allow for repeated vertices and edges). He uses the following rules when walking:

  • If he is currently on node 11, his favourite colour becomes blue.
  • Otherwise, if he is currently on node 22, his favourite colour becomes red.
  • He then traverses an outgoing edge from his current node with the same colour as his favourite colour. It can be shown that such an edge must exist.
  • Finally, he optionally repeats the process.

By writing down the nodes he visits, in order, he gets a list l1,l2,,lLl_1, l_2, \ldots, l_L. Find the number of possible lists, mod 109+710^9 + 7, that satisfy the following conditions:

  • The list starts at node 11 and ends at node 22.
  • For all ii where 3iN3 \le i \le N, node ii appears at most once in the list.
  • For all jj where 3jL3 \le j \le L, we have lj2ljl_{j-2} \ne l_j.

It is provable that the number of such lists is finite.

It may also be useful to note that “mod” corresponds to the % operator in most programming languages, indicating the remainder after division. For example, 5mod3=25 \bmod 3 = 2 and 17mod4=117 \bmod 4 = 1.

Input Format

The first line of input contains a single integer, NN.

The next line contains a string of length NN, consisting of the characters B\mathrm{B} and W\mathrm{W}. If the iith character is B\mathrm{B}, then node ii is black. Otherwise, it is white. It is guaranteed that node 11 is black and node 22 is white.

Output Format

On a single line, output the number of possible lists, modulo 109+710^9 + 7.

4
BWWB
4
12
BWBWBBBWWBBW
3377552

Hint

Explanation of Output for Sample Input 11

The graph looks like:

:::align{center} :::

The solid lines represent blue edges, while the dashed lines represent red edges. The possible paths are:

  • $\color{blue}{1}\ \color{blue}{\to}\ \color{red}{\underline{2}}$
  • $\color{blue}{1}\ \color{blue}{\to}\ \color{blue}{3}\ \color{blue}{\to}\ \color{red}{\underline{2}}$
  • $\color{blue}{1}\ \color{blue}{\to}\ \color{blue}{3}\ \color{blue}{\to}\ \color{blue}{4}\ \color{blue}{\to}\ \color{red}{\underline{2}}$
  • $\color{blue}{1}\ \color{blue}{\to}\ \color{red}{\underline{2}}\ \color{red}{\dashrightarrow}\ \color{red}{\underline{3}}\ \color{red}{\dashrightarrow}\ \color{blue}{1}\ \color{blue}{\to}\ \color{red}{\underline{2}}$

The favourite colour is red at the underlined nodes, and blue otherwise.

The following table shows how the 2525 available marks are distributed:

Marks Awarded Bounds on NN Additional Constraints
11 mark 3N83 \le N \le 8 None.
33 marks 3N203 \le N \le 20
44 marks 3N503 \le N \le 50 There exists exactly 11 black node.
There exists an integer ii where 2iN2 \le i \le N, such that every node in the range [2,i][2, i] is white, and every other node is black.
66 marks There exist at most 55 black nodes.
77 marks None.