Under construction

Still being built. Things may change

Disbursement on Quarantine Policy

Interview

Time limit2sMemory limit1024 MB

Summary
Given a seat grid with known infections and 50% uncertain ones, find the expected total quarantine days modulo 1e9+7.
Level

Medium5 of 10

Topics
Probability, Brute force, Math
Solved
No attempts yet

Statement

The 2019 novel coronavirus, COVID-19, can be transmitted between humans through water droplets and close contact. The transmission is especially easy and fast in relatively crowded or confined spaces, such as airplanes or trains. If someone is infected with COVID-19, then passengers occupying the adjacent seats will be infected easily.

There is a train with nn rows, and there are mm seats per row. All seats are occupied. For some passengers, we know whether they are infected. For the others, we are not sure, so we assume each of them has a 12\frac{1}{2} chance of being infected, independently of each other.

All infected passengers must be quarantined for d0d_0 days. Passengers who are not infected but are edge-adjacent to an infected passenger must be quarantined for d1d_1 days. Passengers who are not infected, are not edge-adjacent to any infected passenger, but are corner-adjacent to one must be quarantined for d2d_2 days.

Passengers must stay in a hotel during quarantine, and the government pays for the hotel. As an accountant for the government, find the expected total number of quarantine days, which is the expected total hotel cost.

Source: Quarantine Policy, 2023 ICPC Taoyuan Regional Contest, Problem D

Input

The first line contains five integers nn, mm, d0d_0, d1d_1, and d2d_2. The next nn lines each contain mm characters. The jj-th character of the ii-th line describes the passenger in the jj-th seat of the ii-th row. Each character is V, ., or ?. V means the passenger is infected, . means the passenger is not infected, and ? means the passenger has a 12\frac{1}{2} chance of being infected.

Output

Print the expected total number of quarantine days modulo 109+710^9 + 7. The answer can be written as an irreducible fraction pq\frac{p}{q}, where qq is not a multiple of 109+710^9 + 7. Print p×q−1p \times q^{-1} modulo 109+710^9 + 7, where q−1q^{-1} is the multiplicative inverse of qq modulo 109+710^9 + 7. That is, xx is the multiplicative inverse of qq if x×q≡1(mod109+7)x \times q \equiv 1 \pmod{10^9 + 7}.

Constraints

1≤n≤1001 \le n \le 100

1≤m≤1001 \le m \le 100

0≤d2≤d1≤d0≤1000 \le d_2 \le d_1 \le d_0 \le 100

Examples2

  1. Example 1

    Input
    4 4 15 7 3
    .?..
    ....
    ..V.
    ....
    
    Expected output
    73
    
  2. Example 2

    Input
    2 2 1 1 1
    ??
    ??
    
    Expected output
    750000009