why my second code is showing run time error while first code is accepted ?

#include<bits/stdc++.h>
#define inf 1000000007;
typedef long long int ll;
using namespace std;

int main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

ll n,k,s=0,s2=0;

cin>>n>>k;
ll a[n+1]={0};

for(ll i=1;i<=n;i++)
{  if(i<=k)
{
	a[i]=1;
	s++;
}

else
{
	s2=s2+a[i-k-1];
	a[i]=(s-s2)%inf;
	s=s+a[i];
}

}
cout<<a[n]<<endl;
}

2
#include<bits/stdc++.h>
#define inf 1000000007;
typedef long long int ll;
using namespace std;

int main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

ll n,k,s=0,s2=0;

cin>>n>>k;
ll a[n+1]={0};

for(ll i=1;i<=k;i++)
{
	a[i]=1;
	s++;
}

for(ll i=k+1;i<=n;i++)
{
	s2=s2+a[i-k-1];
	a[i]=(s-s2)%inf;
	s=s+a[i];
}
cout<<a[n]<<endl;

}