You can remap the event on the kernel level using udev rules (modified from here)
This method will remap the buttons at a deeper level meaning it will affect all users on the system and will work after replugging the device and restarting the computer.
Create a file like /etc/udev/hwdb.d/99-my-mouse-test.hwdb with contents like:
# evdev:input:b<bus>v<vendor>p<product>*
evdev:input:b0003v056Ep010C*
 # KEYBOARD_KEY_<value>=<action>
 KEYBOARD_KEY_90005=btn_right
 KEYBOARD_KEY_90004=btn_middle
To get the the required values you can use evtest (sudo apt install evtest)
Running sudo evtest will ask which device to scan, after selecting device it will output something like:
Input driver version is 1.0.1
Input device ID: bus 0x3 vendor 0x56e product 0x10c version 0x111
Input device name: "ELECOM TrackBall Mouse HUGE TrackBall"
Supported events:
...
Grab the bus, vendor and product numbers and convert them to 4 character uppercase hexadecimal numbers: (0x3 -> 0003, 0x56e -> 056E and 0x10c -> 010C)
Add them together with b for bus, v for vendor and p for product: b0003v056Ep010C. Than add an asterisks (*) to match anything after it.
To get the KEYBOARD_KEY_* number click the button and look for the value (90005 below)
Event: time 1725469422.862391, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90005
Event: time 1725469422.862391, type 1 (EV_KEY), code 273 (BTN_RIGHT), value 1
Event: time 1725469422.862391, -------------- SYN_REPORT ------------
Event: time 1725469422.943388, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90005
Event: time 1725469422.943388, type 1 (EV_KEY), code 273 (BTN_RIGHT), value 0
Event: time 1725469422.943388, -------------- SYN_REPORT ------------
There seems to exist different output formats for this, my source had a different one and had to convert the number into a (lowercase) hexadecimal number.
Check input-event-codes.h for what names the things you want to happen might be called, but make sure to make it lower-case.
To activate the new keybindings it needs to be compiled and reloaded:
sudo udevadm hwdb --update
sudo udevadm trigger