Count subsequences doubt

Can you please dry run on ABCDEFG . I could neither understand why 1 + dp(i-1) and what it indicates ?

Following are the recursive cases:

  1. dp[i] = 2 * dp[i - 1];
  2. if (last[str[i - 1]] != -1)
    dp[i] = dp[i] - dp[last[str[i - 1]]];

there are two options:
you take the element or u do not take the element. Now, think if u come at a character which has been already there before the current position then, there is a case which u have already calculated previously that u will not take that character. Now, if u are at the same character and u choose that i will not take this character then u need to subtract the previous ways of not selecting this character as this leads to the same result.

If i have ABAB then how will it find when what taken and when what not ?

if we reach at A present at 2 index then we have already counted the possibility of not getting A initially. So we will subtract that possiblitity since we are adding that possiblility again.

Getting a WA

Updated ur code;

but why is that using mod , making the difference ?

In question it is mention that output should be taken mod with 10^9+7

ohhh okayy , thank you so much sir !!