you can use NTH to merge to gw
ip route
add dst-address=0.0.0.0/0 gateway=192.168.40.1 scope=255 target-scope=10 \
routing-mark=wan1 comment="" disabled=no
add dst-address=0.0.0.0/0 gateway=192.168.41.1 scope=255 target-scope=10 \
routing-mark=wan2 comment="" disabled=no
/ ip address
add address=172.16.22.1/16 network=172.16.0.0 broadcast=172.16.255.255 \
interface=ether1 comment="" disabled=no
add address=192.168.41.20/24 network=192.168.41.0 broadcast=192.168.41.255 \
interface=ether3 comment="" disabled=no
add address=192.168.40.20/24 network=192.168.40.0 broadcast=192.168.40.255 \
interface=ether4 comment="" disabled=no
/ ip firewall mangle
add chain=prerouting in-interface=ether1 connection-state=new nth=1,1,0 \
action=mark-connection new-connection-mark=wan2 passthrough=yes comment="" \
disabled=no
add chain=prerouting in-interface=ether1 connection-mark=wan2 \
action=mark-routing new-routing-mark=wan2 passthrough=no comment="" \
disabled=no
add chain=prerouting in-interface=ether1 connection-state=new nth=1,1,1 \
action=mark-connection new-connection-mark=wan1 passthrough=yes comment="" \
disabled=no
add chain=prerouting in-interface=ether1 connection-mark=wan1 \
action=mark-routing new-routing-mark=wan1 passthrough=no comment="" \
disabled=no
/ ip firewall nat
add chain=srcnat connection-mark=wan1 action=src-nat \
to-addresses=192.168.40.20 to-ports=0-65535 comment="" disabled=no
add chain=srcnat connection-mark=wan2 action=src-nat to-addresses=192.168.41.20 \
to-ports=0-65535 comment="" disabled=no