import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) def popcount(n): c = (n & 0x5555555555555555) + ((n >> 1) & 0x5555555555555555) c = (c & 0x3333333333333333) + ((c >> 2) & 0x3333333333333333) c = (c & 0x0f0f0f0f0f0f0f0f) + ((c >> 4) & 0x0f0f0f0f0f0f0f0f) c = (c & 0x00ff00ff00ff00ff) + ((c >> 8) & 0x00ff00ff00ff00ff) c = (c & 0x0000ffff0000ffff) + ((c >> 16) & 0x0000ffff0000ffff) c = (c & 0x00000000ffffffff) + ((c >> 32) & 0x00000000ffffffff) return c def factorial(N,p): # 0!~N!(mod p) のリスト res = [0]*(N+1) res[0] = 1 for i in range(1,N+1): res[i] = (res[i-1]*i) % p return res def factorial_inverse(N,p): # 0!~N!(mod p) の逆元のリスト fac = factorial(N,p) res = [0]*(N+1) a = pow(fac[-1],p-2,p) for i in range(N,-1,-1): res[i] = a a *= i a %= p return fac,res N = I() B = [0]+LI() mod = 10**9+7 X = [] for i,b in enumerate(B): if b != -1: X.append((i,b)) fac,fac_inv = factorial_inverse(30,mod) def nCr(n,r): if n < r: return 0 return (fac[n]*fac_inv[r]*fac_inv[n-r]) % mod ans = 1 for i in range(len(X)-1): i0,b0 = X[i] i1,b1 = X[i+1] k = i1-i0 # 考える個数 if b0 & b1 != b0: ans = 0 break p0 = popcount(b0) p1 = popcount(b1) d = p1-p0 # 新しく加わるbitの個数 a = 0 for t in range(d+1): x = nCr(d,t)*pow(pow(2,p1-t,mod)-1,k,mod) if t % 2 == 0: a += x else: a -= x a %= mod ans *= a ans %= mod print(ans)