Bouquet
Time limit1sMemory limit1024 MB
Count distinct flower sequences a robot can collect moving left, right, or down, picking at least one flower per floor, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
A robot stands inside a building with floors. Each floor has rooms arranged in a single row, so all rooms of the building form an rectangle. Some rooms each contain one flower. The robot is learning to gather bouquets.
When the robot is in a room, it may:
- If it is not in the leftmost room, move to the adjacent room to the left on the same floor.
- If it is not in the rightmost room, move to the adjacent room to the right on the same floor.
- If it is not on the bottom floor, move to the room directly below the current one, one floor down.
The robot only moves horizontally or downward; it never moves up.
Whenever it enters a room that contains a flower, it always picks that flower and adds it to the bouquet.
All flowers are distinct, and a bouquet's appearance depends on the order in which the flowers were added. Two bouquets are considered different if they consist of different flowers, or if the flowers were added in a different order.
The robot starts in any room of the top floor and finishes in any room of the bottom floor. Furthermore, the robot always chooses a route that picks at least one flower on every floor.
Determine how many different bouquets the robot could end up having gathered. Output the answer modulo .
Input
The first line contains two integers and .
Each of the next lines describes one floor (starting from the top) with characters; the -th character tells whether the -th room from the left on that floor contains a flower:
O— the room has no flower.X— the room has a flower.
It is guaranteed that every floor contains at least one flower.
Output
Output the number of different bouquets the robot can gather, modulo .