This page is still under construction.

Parts of this page are still being built. What you see may change.

Bouquet

Time limit1sMemory limit1024 MB

Summary
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 NN floors. Each floor has MM rooms arranged in a single row, so all rooms of the building form an N×MN \times M 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 109+710^9 + 7.

Input

The first line contains two integers NN and MM.

Each of the next NN lines describes one floor (starting from the top) with MM characters; the ii-th character tells whether the ii-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 109+710^9 + 7.

Constraints

  • 1≤N≤5001 \le N \le 500
  • 1≤M≤3001 \le M \le 300

Examples3

  1. Example 1

    Input
    2 1
    X
    X
    
    Expected output
    1
    
  2. Example 2

    Input
    2 2
    XX
    XO
    
    Expected output
    4
    
  3. Example 3

    Input
    3 3
    XXX
    XXO
    XOO
    
    Expected output
    34