#P15908. [TOPC 2024] Disbursement on Quarantine Policy

[TOPC 2024] Disbursement on Quarantine Policy

Description

:::epigraph[“Quarantine Policy,” 2023 ICPC Taoyuan Regional Contest, Problem D]

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 they are being infected with COVID-19 or not. However, for other passengers, we are not sure about their status, and we assume each of them has 12\frac{1}{2} chance being infected with COVID-19, independent from each other.

All infected passengers need to be quarantined for d0d_0 days. All passengers that are not infected, but edge-adjacent to any infected passenger, need to be quarantined for d1d_1 days. All passengers that are not infected, not edge-adjacent to any infected passenger, but corner-adjacent to any infected passenger, need to be quarantined for d2d_2 days.

The passengers need to stay in the hotel during quarantine. According to the regulations, the government needs to pay for the hotel. As an accountant of the government, you are asked to evaluate the expected total number of days the passengers need to be quarantined, which indicates the expected total cost on paying for the hotel.

For example, suppose n=4n = 4, m=4m = 4, d0=15d_0 = 15, d1=7d_1 = 7, d2=3d_2 = 3. The third passenger in the third row is infected, and we don’t know whether the second passenger in the first row is infected or not. Other 14 passengers are not infected.

If the second passenger in the first row is infected, then the total number of days of quarantine is 91:

7 15  7 0
3  7  7 3
0  7 15 7
0  3  7 3

If that passenger is not infected, then the total number of days of quarantine is 55:

0 0  0 0
0 3  7 3
0 7 15 7
0 3  7 3

So the expected total number of days of quarantine is 91+552=73\frac{91+55}{2} = 73.

Input Format

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

Output Format

The expected total number of days the passengers need to be quarantined, modulo 109+710^9 + 7. It can be proved that the answer can be represented by a rational number pq\frac{p}{q} where qq is not a multiple of 109+710^9 + 7. Then you need to print p×q1p \times q^{-1} modulo 109+710^9 + 7, where q1q^{-1} means the multiplicative inverse of qq modulo 109+710^9 + 7.

Note: If x×q1mod109+7x \times q \equiv 1 \mod 10^9 + 7, then xx is the multiplicative inverse of qq modulo 109+710^9 + 7.

4 4 15 7 3
.?..
....
..V.
....
73
22111
??
??
750000009

Hint

  • 1n1001 \le n \le 100
  • 1m1001 \le m \le 100
  • 0d2d1d01000 \le d_2 \le d_1 \le d_0 \le 100