← Home
package main

import (
	"bufio"
	"fmt"
	"os"
)

const MOD int64 = 1000000007

func main() {
	in := bufio.NewReaderSize(os.Stdin, 1<<20)

	var n, p int
	fmt.Fscan(in, &n, &p)

	c := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &c[i])
	}

	pow2 := make([]int64, n+1)
	pow2[0] = 1
	for i := 1; i <= n; i++ {
		pow2[i] = (pow2[i-1] * 2) % MOD
	}

	var dp [8]int64
	dp[0] = 1

	for i := 1; i <= n; i++ {
		var ndp [8]int64

		choices := [2]int{0, 1}
		cntChoices := 0
		if c[i-1] == -1 {
			cntChoices = 2
		} else {
			choices[0] = c[i-1]
			cntChoices = 1
		}

		forced := pow2[i-1]
		var free int64
		if i >= 2 {
			free = pow2[i-2]
		}

		for st := 0; st < 8; st++ {
			val := dp[st]
			if val == 0 {
				continue
			}

			x0 := st & 1
			x1 := (st >> 1) & 1
			s := (st >> 2) & 1

			for z := 0; z < cntChoices; z++ {
				col := choices[z]
				oppHas := x0
				if col == 0 {
					oppHas = x1
				}

				if oppHas == 0 {
					nx0, nx1 := x0, x1
					if col == 0 {
						nx0 = 1
					} else {
						nx1 = 1
					}
					ns := s ^ 1
					nst := nx0 | (nx1 << 1) | (ns << 2)
					ndp[nst] = (ndp[nst] + val*forced) % MOD
				} else {
					ndp[st] = (ndp[st] + val*free) % MOD

					nx0, nx1 := x0, x1
					if col == 0 {
						nx0 = 1
					} else {
						nx1 = 1
					}
					ns := s ^ 1
					nst := nx0 | (nx1 << 1) | (ns << 2)
					ndp[nst] = (ndp[nst] + val*free) % MOD
				}
			}
		}

		dp = ndp
	}

	var ans int64
	for st := 0; st < 8; st++ {
		if ((st >> 2) & 1) == p {
			ans += dp[st]
			if ans >= MOD {
				ans -= MOD
			}
		}
	}

	out := bufio.NewWriterSize(os.Stdout, 1<<20)
	fmt.Fprintln(out, ans)
	out.Flush()
}