#!/usr/bin/perl
use 5.016;
use warnings;
sub f {
my ($filter, $list) = @_;
@{$_[0]} or return ($filter->(@{$_[1]}) ? 1 : 0);
__SUB__->(
[@{$_[0]}[0 .. $_ - 1, $_ + 1 .. $#{$_[0]}]],
[@{$_[1]}, $_[0]->[$_]]
)
} (0 .. $#{$_[0]});
}->($list, []);
}
sub filter {
$_[0] * ($_[1] . $_[2]) + ($_[3] . $_[4] . $_[5]) ==
($_[6] . $_[7] . $_[8] . $_[9])
}
say f(\&filter, [0 .. 9]);
IyEvdXNyL2Jpbi9wZXJsCnVzZSA1LjAxNjsKdXNlIHdhcm5pbmdzOwp1c2UgTGlzdDo6VXRpbCBxdyhzdW0pOwoKc3ViIGYgewoJbXkgKCRmaWx0ZXIsICRsaXN0KSA9IEBfOwoKCXJldHVybiBzdWJ7CgkJQHskX1swXX0gb3IgcmV0dXJuICgkZmlsdGVyLT4oQHskX1sxXX0pID8gMSA6IDApOwoKCQlyZXR1cm4gc3VtIG1hcHsKCQkJX19TVUJfXy0+KAoJCQkJW0B7JF9bMF19WzAgLi4gJF8gLSAxLCAkXyArIDEgLi4gJCN7JF9bMF19XV0sCgkJCQlbQHskX1sxXX0sICRfWzBdLT5bJF9dXQoJCQkpCgkJfSAoMCAuLiAkI3skX1swXX0pOwoJfS0+KCRsaXN0LCBbXSk7Cn0KCnN1YiBmaWx0ZXIgewoJJF9bMF0gKiAoJF9bMV0gLiAkX1syXSkgKyAoJF9bM10gLiAkX1s0XSAuICRfWzVdKSA9PQoJKCRfWzZdIC4gJF9bN10gLiAkX1s4XSAuICRfWzldKQp9CgpzYXkgZihcJmZpbHRlciwgWzAgLi4gOV0pOwpleGl0Owo=