#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e6 + 10, mod = 998244353;
int n;
int fac[maxN + 1], inv[maxN + 1];
inline int ADD(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int SUB(int x, int y) { return x - y < 0 ? x - y + mod : x - y; }
inline int mpow(int a, long long x) {
  int ans = 1;
  while (x) {
    if (x & 1) ans = 1ll * ans * a % mod;
    a = 1ll * a * a % mod;
    x >>= 1;
  }
  return ans;
}
inline int C(int n, int m) {
  if (n < m) return 0;
  return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
  scanf("%d", &n);
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[n] = mpow(fac[n], mod - 2);
  for (int i = n - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  int ans = 0;
  for (int i = 1, fl = 0; i <= n; i++, fl ^= 1) {
    int t = 1ll * C(n, i) * mpow(3, 1ll * n * (n - i) + i) % mod;
    if (fl)
      ans = SUB(ans, t);
    else
      ans = ADD(ans, t);
  }
  ans = 2ll * ans % mod;
  int res = 0;
  for (int i = 1, fl = 1; i <= n; i++, fl ^= 1) {
    int x = SUB(mpow(SUB(1, mpow(mpow(3, n - i), mod - 2)), n), 1);
    int t = 1ll * C(n, i) * mpow(mpow(3, 1ll * n * i), mod - 2) % mod * x % mod;
    if (fl)
      res = SUB(res, t);
    else
      res = ADD(res, t);
  }
  res = 1ll * mpow(3, 1ll * n * n + 1) * res % mod;
  printf("%d\n", SUB(ans, res));
  return 0;
}
