#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
const int N = 1e6 + 5, MOD = 998244353;
using ii = pair<int, int>;
int fast(int b, ll e, int res = 1) {
  for (; e; b = 1ll * b * b % MOD, e >>= 1)
    if (e & 1) res = 1ll * res * b % MOD;
  return res;
}
int F[N], iF[N], n;
int C(int n, int k) { return 1ll * F[n] * iF[n - k] % MOD * iF[k] % MOD; }
inline void fix(int& x) {
  while (x < 0) x += MOD;
  while (x >= MOD) x -= MOD;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  F[0] = iF[0] = 1;
  for (int i = 1; i <= n; i++) {
    F[i] = F[i - 1] * 1ll * i % MOD;
    iF[i] = fast(F[i], MOD - 2);
  }
  int ans = 0, sum[2] = {};
  for (int i = 1; i <= n; i++) {
    ans += 1ll * C(n, i) * fast(3, 1ll * (n - i) * n + i) * ((i & 1) ? 1 : -1) %
           MOD;
    fix(ans);
    ans += (1ll * ((i & 1) ? 1 : -1) * C(n, i) * 3ll % MOD *
                fast(fast(3, n - i) - 1, n) % MOD +
            1ll * ((i & 1) ? 1 : -1) * C(n, i) * (fast(3, i) - 3) % MOD *
                fast(3, 1ll * (n - i) * n) % MOD) %
           MOD;
    fix(ans);
  }
  cout << ans << '\n';
  return 0;
}
