#!/usr/bin/perl -w

use v5.36;
use strict;
use feature 'multidimensional';

my $depth = 11739;
my ($tx, $ty) = (11,718);

my %gi;
sub gi2el($gi) { ($gi + $depth) % 20183 }
my %el;

my $sum = 0;
for my $n (1 .. $tx + $ty) {
	my $y = 0;
	my $x = $n;
	for my $i (0 .. $n) {
		if (($x == 0 && $y == 0) || ($x == $tx && $y == $ty)) {
			$gi{$x,$y} = 0;
		} elsif ($y == 0) {
			$gi{$x,$y} = 16807*$x;
		} elsif ($x == 0) {
			$gi{$x,$y} = 48271*$y;
		} else {
			$gi{$x,$y} = $el{$x-1,$y} * $el{$x,$y-1};
		}
		$el{$x,$y} = gi2el($gi{$x,$y});
		$sum += $el{$x,$y} % 3
			if $x <= $tx && $y <= $ty;
		$y++;
		$x--;
	}
}

say $sum;


