I was solving a competitive programming question on codeforces. My code got accepted there but it is giving segmentation fault in my local computer. Why is it so?
I also tried other online compilers like ideone , and it was working there too.
My operating system is Ubuntu 20.04
My code :
#include <bits/stdc++.h>
using namespace std;
int M = 1000000007;
int val[1001][1001];
int n,k;
int dp(int cur,int rem)
{
    if(cur<1 || cur>k || rem<0 || rem>n)return 0;
    if(cur==1 || rem==0)return 1;
    if(val[cur][rem]==-1)
    {
        int ans=0;
        ans+=dp(cur,rem-1);
        ans%=M;
        ans+=dp(cur-1,n-rem);
        ans%=M;
        val[cur][rem]=ans;
    }
    return val[cur][rem];
    
}
void solve()
{
    cin>>n>>k;
    for(int i=0;i<=k;i++)for(int j=0;j<=n;j++)val[i][j]=-1;
    cout<<dp(k,n);
    cout<<"\n";
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int _t=1;
    cin>>_t;
    for (int i=1;i<=_t;i++)
    {
        solve();
    }
    return 0;
}
