I was doing this Problem : Count all Longest Common Sub sequences

I completely understand the editorial.

But when i went and look at others people solutions **almost every one** has used this approach(Not the one given in editorial)(Which too i understand completely):

1].Dynamic Programming Approach to solve “Length of longest common sub sequence”.

2].Dynamic Programming Approach to solve “No of longest common sub sequences”, using step 1.

Link to one of the sample solution

I am weak at Modular Arithmetic, so i got stuck at this part of above code:

```
if(dp1[i-1][j-1]==dp1[i][j-1])
dp2[i][j]=(dp2[i-1][j]+dp2[i][j-1]-dp2[i-1][j-1]+MOD)%MOD;
```

(I think it’s not hard to find this above piece of code).

Why we are adding MOD?

I know that is due to: (dp2[i][j]=(dp2[i-1][j]+dp2[i][j-1]-dp2[i-1][j-1]) can go negative.(I realized this when i looked at other solutions).

But unable to know How adding the MOD will fix the above issue??(Please Someone help).